I am struggling to mock the below code:
Client client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
My approach:
Settings.Builder settingsBuilder = mock(Settings.Builder.class);
when(settingsBuilder.put(new String("test"), new String("test"))).thenReturn(settings.settingsBuilder());
when(settingsBuilder.build()).thenReturn(settings);
TransportClient.Builder clientBuilder = mock(TransportClient.Builder.class);
when(clientBuilder.settings(settings)).thenReturn(clientBuilder);
Up until this point the test works fine but when trying to mock the other classes used on the client, Mockito throws the following exception:
Error:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
Question:
Now, I undertand that this could be thrown because I am not mocking something correctly but I can't figure out what is wrong with my mocking this time. How can I mock the client correctly so all dependent classes are mocked and a new mocked client is returned?
Thanks.
Aucun commentaire:
Enregistrer un commentaire