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 https://github.com/openmrs/openmrs-module-sync2/commit/3dec2022a0d5058c45fce3f9abdc106ce0b8c833 and here are the errors https://travis-ci.org/openmrs/openmrs-module-sync2/builds/602230608?utm_source=github_status&utm_medium=notification 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.
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.
@willa https://travis-ci.org/openmrs/openmrs-module-sync2/builds/604393280?utm_source=github_status&utm_medium=notification
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…https://github.com/openmrs/openmrs-module-sync2/pull/149
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;
@Test(expected=SyncException.class)
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.
//doThrow(SyncException.class).when(parentFeedReader).pullAndProcessAllFeeds();
pullAndProcessAllfeeds(); // If it works as expected this will throw.
}
@willa I finally figured out a way by mocking the class plus mocking the some methods and then calling the real method. Something like " @Test(expected = SyncException.class) public void pullAndProcessAllFeeds_shouldNotSyncIfPullIsDisabled() { Mockito.when(parentFeedReader.enabled()).thenReturn(false); doCallRealMethod().when(parentFeedReader).pullAndProcessAllFeeds(); parentFeedReader.pullAndProcessAllFeeds(); } "
However I discarded my changes after noticing i was implementing something already present…I corrected it and PR is https://github.com/openmrs/openmrs-module-sync2/pull/149. Otherwise thanx for your input.