gpt4 book ai didi

java - 模拟 InetSocketAddress.getAddress().getHostAddress() 失败

转载 作者:行者123 更新时间:2023-12-01 17:44:25 24 4
gpt4 key购买 nike

这是我尝试过的:

@Test
public void local_host_test() {

InetAddress inetAddress = Mockito.mock(InetAddress.class);
Mockito.when(inetAddress.getHostAddress()).thenReturn("127.0.0.1");
InetSocketAddress inetSocketAddress = Mockito.mock(InetSocketAddress.class);
Mockito.when(inetSocketAddress.getAddress()).thenReturn(inetAddress);
Mockito.when(inetSocketAddress.getPort()).thenReturn(22);

Assert.assertThat(inetSocketAddress.getAddress().getHostAddress(), Is.is("127.0.0.1"));
}

我还 mock 了中间步骤,即 InetAddress,但它提示如下:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mockito.internal.creation.cglib.ClassImposterizer (file:mockito-all-1.10.19.jar) to constructor java.net.InetAddress()
WARNING: Please consider reporting this to the maintainers of org.mockito.internal.creation.cglib.ClassImposterizer
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

java.lang.NullPointerException
at java.base/java.net.InetSocketAddress.getAddress(InetSocketAddress.java:325)
at InetSocketAddressTest.local_host_test(InetSocketAddressTest.java:17)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)


Process finished with exit code 255

我做错了什么?

最佳答案

查看 Java 文档

/**
* Gets the port number.
*
* @return the port number.
*/
public final int getPort() {
return holder.getPort();
}

/**
*
* Gets the {@code InetAddress}.
*
* @return the InetAdress or {@code null} if it is unresolved.
*/
public final InetAddress getAddress() {
return holder.getAddress();
}

和 Mockito 约束
Mockito 不支持模拟 final方法。 Mockito 和 PowerMock 之间的比较将详细解释事情。无法模拟 final方法 - 它们的真实行为执行时没有任何异常
因此,它正在调用实际的方法,并且您得到NPE

使用PowerMockito

关于java - 模拟 InetSocketAddress.getAddress().getHostAddress() 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60885294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com