I want to test a service class.
class Service {
public List<A> mainMethodToTest() {
//calling another method to get some values
List<A> list = getImportantValues();
process list;
return list;
}
public getimportantValues() {
return new List<A>();
}
}
I want to test this service class method 'mainMethodToTest()' using Mockito and mock the call to getimportantValues() method.
Here is the test class I wrote.
class TestService {
private Service service;
@Before
public void setup() {
service = Mockito.spy(new Service);
}
@Test
public void testMainMethodToTest() {
Mockito.doReturn(new List<A>()).when(service).getimportantValues();
Assert.assertNotNull(service.mainMethodToTest()); /// this call throws NullPointerException because somehow it is not treating the service as a real instance of the Service object but a mock.
}
}
The last call in Assert to method 'mainMethodToTest()' throws NullPointer because it takes the service as null and not an instance.
Can someone please help me to understand what am I doing wrong in here.
TIA