Mockito Inject mock into Spy object

Viewed 51556

I'm writing a test case for a Class which has a 2 level of dependency injection. I use @Spy annotation for the 1 level dependency injection object, and I would like to Mock the 2nd level of injection. However, I kept getting null pointer exception on the 2nd level. Is there any way that I inject the mock into the @Spy object?

public class CarTestCase{
    @Mock
    private Configuration configuration;

    @Spy 
    private Engine engine;

    @InjectMocks 
    private Car car;

    @Test
    public void test(){

       Mockito.when(configuration.getProperties("")).return("Something");
       car.drive();
    }

}

public class Car{
    @Inject
    private Engine engine;

    public void drive(){
        engine.start();
    }
}

public class Engine{
    @Inject 
    private Configuration configuration;

    public void start(){
        configuration.getProperties();   // null pointer exception
    }

}
5 Answers

I've also wandered how to inject a mock into a spy.

The following approach will not work:

@Spy
@InjectMocks
private MySpy spy;

But the desired behavior can be achieved by a "hybrid" approach, when using both annotation and manual mocking. The following works perfectly:

@Mock
private NeedToBeMocked needToBeMocked;

@InjectMocks
private MySpy mySpy;

@InjectMocks
private SubjectUnderTest sut;

@BeforeMethod
public void setUp() {
    mySpy = Mockito.spy(new MySpy());
    MockitoAnnotations.initMocks(this);
}

(SubjectUnderTest here depends on MySpy, and MySpy in its turn depends on NeedToBeMocked).

UPD: Personally, I think that if you have to do such a magic too often, it might be a sign that there is something wrong with dependenicies between your classes and it is worth to perform a little bit of refactoring to improve your code.

The (simplest) solution that worked for me.

@InjectMocks
private MySpy spy = Mockito.spy(new MySpy());

No need for MockitoAnnotations.initMocks(this) in this case, as long as test class is annotated with @RunWith(MockitoJUnitRunner.class).

I also met this issue during the unit testing with Spring boot framework, but I found one solution for using both @Spy and @InjectMocks

Previous answer from Yoory N.

@Spy
@InjectMocks
private MySpy spy;

Because InjectMocks need to have instance created, so the solution works for me is at below,

@Spy
@InjectMocks
private MySpy spy = new MySpy();

I think I just found the definitive answer. I tried Yoory approach but changed the order of the annotations :

@InjectMocks
@Spy
private MySpy spy;

I assume that Mockito first creates the mock, and adds a spy on top of that. So there is no need to instantiate the MySpy object.

Related