Null pointer exception when stubbing a method
/#-----------Null pointer exception when stubbing a method. check the syntax of your stub
//# for me it was doReturn(responseEntity).when(rhpp.invokeDownStreamService(apiRequestService, traceId));
// this variable needs some other values to be set like url and good status ....use reflection api to set this field variables.
Follow this link to understand partial Mocking.
https://stackoverflow.com/questions/14970516/use-mockito-to-mock-some-methods-but-not-others
Also understand difference between doReturn.when vs When.thenReturn.
To directly answer your question, yes, you can mock some methods without mocking others. This is called a partial mock. See the Mockito documentation on partial mocks for more information.
For your example, you can do something like the following, in your test:
Stock stock = mock(Stock.class);
when(stock.getPrice()).thenReturn(100.00); // Mock implementation
when(stock.getQuantity()).thenReturn(200); // Mock implementation
when(stock.getValue()).thenCallRealMethod(); // Real implementation
In that case, each method implementation is mocked, unless specify thenCallRealMethod() in the when(..) clause.
There is also a possibility the other way around with spy instead of mock:
Stock stock = spy(Stock.class);
when(stock.getPrice()).thenReturn(100.00); // Mock implementation
when(stock.getQuantity()).thenReturn(200); // Mock implementation
// All other method call will use the real implementations
In that case, all method implementation are the real one, except if you have defined a mocked behaviour with when(..).
There is one important pitfall when you use when(Object) with spy like in the previous example. The real method will be called (because stock.getPrice() is evaluated before when(..) at runtime). This can be a problem if your method contains logic that should not be called. You can write the previous example like this:
Stock stock = spy(Stock.class);
doReturn(100.00).when(stock).getPrice(); // Mock implementation
doReturn(200).when(stock).getQuantity(); // Mock implementation
// All other method call will use the real implementations
Another possibility may be to use org.mockito.Mockito.CALLS_REAL_METHODS, such as:
Stock MOCK_STOCK = Mockito.mock( Stock.class, CALLS_REAL_METHODS );
This delegates unstubbed calls to real implementations.
Comments
Post a Comment