org.mockito.exceptions.misusing.InjectMocksException: Cannot instantiate @InjectMocks

Viewed 2576

I am using @InjectMocks to inject Repository Implementation into my Test class, but it throws InjectMocksException. Below is my code and Error, please help how to resolve this error?

Error:

org.mockito.exceptions.misusing.InjectMocksException: 
Cannot instantiate @InjectMocks field named 'muRepository' of type 'class com.example.MyrRepositoryImpl'.
You haven't provided the instance at field declaration so I tried to construct the instance.
However the constructor or the initialization block threw an exception : null


    at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:77)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:83)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.NullPointerException
    at com.example.MyRepositoryImpl.<init>(MyRepositoryImpl.java:27)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

Injecting:

@InjectMocks
private MyRepositoryImpl myRepository;

Test:

@Test (expected = NullPointerException.class )
public void procTest( ) throws Exception
{
        when( addProc.execute( null,
                               null,
                               null
                              )
            ).thenThrow( new NullPointerException() );

        myRepository.addUser( null );
}

Repository:

public class MyRepositoryImpl implements MyRepository
{
    private final AddProc addProc;

    @Autowired
    public MyRepositoryImpl( final ProcFactory procFactory )
    {
        this.addProc = procFactory.create( AddProc.class ); //this is line 27 referenced in error
    }

    @Override
    public User addUser( User user) 
    {
            return addProc.execute(
                                     user.getUserName( ),
                                     user.getFirstName( ),
                                     user.getLastName( )
                                   );


        }
}
2 Answers

Mockito uses the constroctor injection but you didnt use mock object for ProcFactory class. So null pointer exception occurs. Either you have to use ProcFactory as mock in test class or, you have to add null check.

@Mock
private ProcFactory  procFactory;

Regards

Having seen that you are running tests on JUnit 4, in order to initialize and inject mocks through @InjectMocks needs to use @RunWith(MockitoJUnitRunner.class) as test runner or Mockito.initMocks(this) in the setup step.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
}
Related