Null after @InjectMocks

radpet picture radpet · Nov 21, 2014 · Viewed 25k times · Source

I am having some troubles passing a dependency while unit testing with JUnit.

Consider these pieces of code:

This is the dependacy injecton into the class which i want to test, lets call it Controller.

@Inject private FastPowering fastPowering;  

And this is the unit test:

@RunWith(MockitoJUnitRunner.class)
public class ControllerTest {

@Mock
FastPowering fastPower;

@InjectMocks
Controller controller;

@Test
public void test() {
    assertEquals(
            (controller.computeAnswer(new BigDecimal(2), 2)).longValue(),
            (long) Math.pow(2, 2));
    }
}

It seems that fastPower is null, please explain how to fix that. Null pointer exception , because of calling the @injected field (fastPower) inside the .computeAnswer method)

Edit:

Solved i should have read about the difference between @Mock and @Spy...

Due to a lot of comments I am adding some more context to the solution

The difference is that in mock, you are creating a complete mock or fake object while in spy, there is the real object and you just spying or stubbing specific methods of it. While in spy objects, of course, since it is a real method, when you are not stubbing the method, then it will call the real method behavior.

If fastPower is annotated as @Mock it's methods are dummy, yet controller.computeAnswer depends on them to compute. One must provide behaviour.

If spy is used without stubbing then the real implementation of fastPower is being executed which eventually returns desired value.

Another option is to use a real FastPowering instance

https://github.com/mockito/mockito/wiki/Using-Spies-(and-Fakes) https://github.com/mockito/mockito/wiki/Mocking-Object-Creation

And some stackoverflow thread outlining the difference Mocking vs. Spying in mocking frameworks

Short Answer: Replace @Mock with @Spy and should be working fine

Answer

Khalid picture Khalid · Nov 21, 2014

Use MockitoAnnotations.initMocks to initiate the @Mock and @InjectMocks objects. Your test would look something like:

@Mock
FastPowering fastPower;

@InjectMocks
Controller controller;

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

@Test
public void test() {
    ....
}