NullPointerException when using Mockito

I am trying to write a unit test in JUnit with Mockito however am geting a NPE when I inject-mocks…here is the Unit test and here are the errors Any pointers on what i should do? @dkayiwa

Am just thinking , do you have atom feeds started ?

@tendomart I dont think it needs any class from atomfeed mocked since I am not even using it in the class that am testing

Alright thought the class you’re mocking had dependencies on the former.

1 Like

Can you do a draft pull request and we take a look?

here is the PR

I have left comments on your draft PR. I hope it helps.

@willa tried what you suggested but still have the NPE

Did you manage to resolve this? If not share your stack trace as well as the code you are running.


First you don’t need both @RunWith(MockitoJUnitRunner.class) and MockitoAnnotations.initMocks(this); at the same time. Use one or the other, in this case since you are using annotations, the former would suffice. The NPE happens at @InjectMocks annotation which means the mock framework is not able to find some mocks to inject during initialization. The stack trace which is in surefire reports would show exactly what other mocks you need. I will be happy to look at the stack trace if you share it. It is stored in surefire reports directory. When you run mvn test maven will report the location of this directory.

@willa I have made these changes to the test but wondering if this truely tests what the method does…

The test method name says the method should not sync if pull is disabled which I suppose it means an exception will be thrown. However, I don’t see anywhere where you are disabling pull. Second, if you want to test whether the method will throw an exception then don’t tell the mock framework to do it for you because you won’ t be testing the behavior of the method anymore. In short in semi-pseudocode it should look like the following;

public void shouldThrowWhenPullIsDisabled() {
   setPullDisabled(true);           // You should find an appropriated way to do this (i.e. I just made this up)

   // The following line which is present in your code should not be there because the mock framework
   // will simply throw the expected exception when you make the call.

   pullAndProcessAllfeeds();   // If it works as expected this will throw.