vendredi 27 mars 2020

Mocking InetSocketAddress.getAddress().getHostAddress() fails

Here's what I tried:

@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"));
}

I also mocked the intermediate step, i.e., InetAddress, but it complains as shown below:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mockito.internal.creation.cglib.ClassImposterizer (file:/Users/iluvtrollhd/.m2/repository/org/mockito/mockito-all/1.10.19/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

What did I wrong?

Aucun commentaire:

Enregistrer un commentaire