PowerMockito using InjectMocks error
<p>I have a very complicated class to write Junit test case. I decided to use PowerMockito since my class for which the test is to be run has a constructor initialization.</p>

<p>My main class is like this:</p>

<pre><code>public class MainClass extends BaseClass{

MainClass(SomeClass class){
    super(class);
}

public void methodToBeTested(){
some code here
}

..few other methods which I am not going to test.
}
</code></pre>

<p>Now I have the test case written like this:</p>

<pre><code>@RunWith(PowerMockRunner.class)
public class TestClass{

@Mock
OtherClassUsedInMainClass mock1;

@Mock
OtherClassUsedInMainClass mock2;

@InjectMocks
MainClass mainClass;

@Before
public void setUp() throws Exception{
    MockitoAnnotations.initMocks(this);
    PowerMockito.whenNew(MainClass.class).withArguments(Mockito.any(SomeClass.class))
            .thenReturn(mainClass);)
}

@Test
public void testMethodtobeTested(){
    ...I am using the other objects to mock data and test if this method works fine

    mainClass.methodtobeTested();
    \\This method will increment a value. I am just asserting if that value is right. 
    Assert.assertEquals(mainClass.checkCount(),RequiredCount)

}

}
</code></pre>

<p>I am getting a <strong>null pointer exception</strong> when running the Testcase since it tries to initialize the <strong>mainClass</strong>. It does not get mocked. I know I am doing something wrong. But I just don't know what it is.</p>

<p>Error:</p>

<pre><code>org.mockito.exceptions.base.MockitoException: 
Cannot instantiate @InjectMocks field named 'mainClass' of type 'class com.main.MainClass'.
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

Caused by: java.lang.NullPointerException
This null pointer exception is thrown from a the constructor of the BaseClass when it tries to initialize another class.
</code></pre>