Q:

Null after @InjectMocks

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
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

A:

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() {
    ....
}

A:

After debugging I found a reason. This is because of the collection org.powermock.core.MockRepository#instanceMocks doesn't contain a mock for a field with @InjectMocks annotation (Controller controller in your case).
To solve it try to use the @Spy annotation in the field declaration with initializing of them and @PrepareForTest above the class declaration:
@PrepareForTest(Controller.class)
@RunWith(MockitoJUnitRunner.class)
public class ControllerTest {

    @Mock
    FastPowering fastPower;

    @Spy
    @InjectMocks    
    Controller controller = new Controller();

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

In my case it helped. Using of the method Mockitoannotations.initMocks(this) is not required, it doesn't affect the result.

