0

In my springboot project, I am testing some POJOs using Mockito.

I simply mock instance & stub function:

public class MyCarTest {
   @Mock
   Car car;

   @Mock
   Manufacture manufacture;

   @Test
   public void testGetManufacture() {
      // NullInsteadOfMockException
      doReturn(manufacture).when(car).getManufacture();

   }
}

if I run the test, I get error:

org.mockito.exceptions.misusing.NullInsteadOfMockException: 
Argument passed to when() is null!

Where am I wrong?

Leem
  • 17,220
  • 36
  • 109
  • 159
  • That is because the mock object `car` is not initialized. Have a look at this [answer](https://stackoverflow.com/questions/60950079/org-mockito-exceptions-misusing-missingmethodinvocationexception-in-mockito/61033150#61033150) of mine and see if it helps. – Madhu Bhat Apr 18 '20 at 07:01
  • Basically you need `MockitoAnnotations.initMocks(this)` or the test class annotated with a runner for initializing the mock object. – Madhu Bhat Apr 18 '20 at 07:03
  • did the above help? – Madhu Bhat Apr 20 '20 at 03:28

0 Answers0