Próbuję użyć Mockito do stworzenia obiektu Mock, który zostanie zwrócony z obiektu Mock. W szczególności próbuję wykpić obiekt PlayerConnection
, którego mój program może użyć do pobrania adresu IP.Mockito, gdy(). ThenReturn() wyrzuca nullpointerExceptions
Możesz znaleźć więcej informacji na temat tego PlayerConnection object
here. Zwraca ona wartość InetSocketAddress
, która może następnie zwrócić wartość InetAddress
, która może zwrócić wartość String
z adresem IP odtwarzacza. Ale nie mogę zajść tak daleko, ponieważ mój pierwszy when(class.function()).thenReturn(returnVariable)
rzuca NullPointerException
. Oto mój kod:
/**
* Creates a partial mock of a connection that can return an ip address.
*
* @param String
* The IP to return when the connection gets asked.
* @return
*/
private PlayerConnection newConnection(String ipString)
{
PlayerConnection playerConnection = mock(PlayerConnection.class);
InetSocketAddress inetSocketAddress = mock(InetSocketAddress.class);
InetAddress inetAddress = mock(InetAddress.class);
when(playerConnection.getAddress()).thenReturn(inetSocketAddress);
when(inetSocketAddress.getAddress()).thenReturn(inetAddress);
when(inetAddress.getHostAddress()).thenReturn(ipString);
return playerConnection;
}
A oto ślad stosu, występujących w when(playerConnection.getAddress()).thenReturn(inetSocketAddress)
:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.001 sec <<< FAILURE!
ruleResponseTest(com.github.heartsemma.communitywall.ConnectionChecks.RuleManagerTest) Time elapsed: 0.001 sec <<< ERROR!
java.lang.NullPointerException
at java.net.InetSocketAddress$InetSocketAddressHolder.access$500(InetSocketAddress.java:56)
at java.net.InetSocketAddress.getAddress(InetSocketAddress.java:334)
at com.github.heartsemma.communitywall.ConnectionChecks.RuleManagerTest.newConnection(RuleManagerTest.java:99)
at com.github.heartsemma.communitywall.ConnectionChecks.RuleManagerTest.ruleResponseTest(RuleManagerTest.java:44)
Edit:
ja przestawił moje odcinki do doReturn().when().function()
zamiast when().thenReturn()
zatrzymać NullPointerExceptions
i tak, ale teraz dostaję zwyczaj UnfinishedStubbingExceptions
od Mockito.
Pomocny kod błędu mówi, że mam gdzieś niedokończony skrót, ale nie widzę, gdzie on jest. Błąd występuje w drugiej metodzie doReturn()
.
/**
* Creates a partial mock of a connection that can return an ip address.
*
* @param ipString The IP to return.
*
* @return A PlayerConnection object that can return a Host Address of the ipString but nothing else.
*/
private PlayerConnection newConnection(String ipString)
{
PlayerConnection playerConnection = mock(PlayerConnection.class);
InetSocketAddress inetSocketAddress = mock(InetSocketAddress.class);
InetAddress inetAddress = mock(InetAddress.class);
doReturn(inetSocketAddress).when(playerConnection).getAddress();
doReturn(inetAddress).when(inetSocketAddress).getAddress();
doReturn(ipString).when(inetAddress).getHostAddress();
return playerConnection;
}
Zobacz http: // stackoverflow.com/questions/34308877/how-to-mock-method-call-and-return-value-without-running-the-method. – Tunaki
Nie rozumiem. Masz ten sam błąd, a ta sama odpowiedź to użycie 'doReturn (inetSocketAddress) .when (playerConnection) .getAddress()' jak wspomniano, a ty tego nie robisz (więc jak możesz powiedzieć, że to nie pomogło?). – Tunaki
Przepraszam; Chciałem powiedzieć, że spowodował kolejny błąd. Edycja oryginalnego wpisu. –