# NullPointerException when using Mockito

**URL:** <https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209>\
**Category:** Uncategorized\
**Created:** [October 24, 2019, 11:36am UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209 "2019-10-24T11:36:30Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![reagan](https://talk.openmrs.org/user_avatar/talk.openmrs.org/reagan/32/10652_2.png) [@reagan](https://talk.openmrs.org/u/reagan)\
**Post date:** [October 24, 2019, 11:36am UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/1 "2019-10-24T11:36:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tendomart](https://talk.openmrs.org/user_avatar/talk.openmrs.org/tendomart/32/6753_2.png) [@tendomart](https://talk.openmrs.org/u/tendomart)\
**Post date:** [October 24, 2019, 3:26pm UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/2 "2019-10-24T15:26:50Z")

</div>

Am just thinking , do you have atom feeds started ?

---

<div class="post-metadata">

**Author:** ![reagan](https://talk.openmrs.org/user_avatar/talk.openmrs.org/reagan/32/10652_2.png) [@reagan](https://talk.openmrs.org/u/reagan)\
**Post date:** [October 24, 2019, 3:31pm UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/3 "2019-10-24T15:31:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tendomart](https://talk.openmrs.org/user_avatar/talk.openmrs.org/tendomart/32/6753_2.png) [@tendomart](https://talk.openmrs.org/u/tendomart)\
**Post date:** [October 25, 2019, 5:08am UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/4 "2019-10-25T05:08:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)\
**Post date:** [October 25, 2019, 12:50pm UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/5 "2019-10-25T12:50:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reagan](https://talk.openmrs.org/user_avatar/talk.openmrs.org/reagan/32/10652_2.png) [@reagan](https://talk.openmrs.org/u/reagan)\
**Post date:** [October 26, 2019, 5:11pm UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/6 "2019-10-26T17:11:23Z")

</div>

> [@reagan](#):
>
> [https://github.com/openmrs/openmrs-module-sync2/commit/3dec2022a0d5058c45fce3f9abdc106ce0b8c833](https://github.com/openmrs/openmrs-module-sync2/commit/3dec2022a0d5058c45fce3f9abdc106ce0b8c833)

here is the PR

---

<div class="post-metadata">

**Author:** ![willa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/willa/32/256_2.png) [@willa](https://talk.openmrs.org/u/willa)\
**Post date:** [October 28, 2019, 3:34pm UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/7 "2019-10-28T15:34:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reagan](https://talk.openmrs.org/user_avatar/talk.openmrs.org/reagan/32/10652_2.png) [@reagan](https://talk.openmrs.org/u/reagan)\
**Post date:** [October 29, 2019, 12:15pm UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/8 "2019-10-29T12:15:46Z")

</div>

@willa tried what you suggested but still have the NPE

---

<div class="post-metadata">

**Author:** ![willa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/willa/32/256_2.png) [@willa](https://talk.openmrs.org/u/willa)\
**Post date:** [October 31, 2019, 6:26pm UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/9 "2019-10-31T18:26:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reagan](https://talk.openmrs.org/user_avatar/talk.openmrs.org/reagan/32/10652_2.png) [@reagan](https://talk.openmrs.org/u/reagan)\
**Post date:** [November 1, 2019, 11:47am UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/10 "2019-11-01T11:47:39Z")

</div>

@willa[https://travis-ci.org/openmrs/openmrs-module-sync2/builds/604393280?utm\_source=github\_status&utm\_medium=notification](https://travis-ci.org/openmrs/openmrs-module-sync2/builds/604393280?utm_source=github_status&utm_medium=notification)

---

<div class="post-metadata">

**Author:** ![willa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/willa/32/256_2.png) [@willa](https://talk.openmrs.org/u/willa)\
**Post date:** [November 1, 2019, 3:09pm UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/11 "2019-11-01T15:09:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![reagan](https://talk.openmrs.org/user_avatar/talk.openmrs.org/reagan/32/10652_2.png) [@reagan](https://talk.openmrs.org/u/reagan)\
**Post date:** [November 3, 2019, 11:04am UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/12 "2019-11-03T11:04:44Z")

</div>

@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](https://github.com/openmrs/openmrs-module-sync2/pull/149)

---

<div class="post-metadata">

**Author:** ![willa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/willa/32/256_2.png) [@willa](https://talk.openmrs.org/u/willa)\
**Post date:** [November 4, 2019, 4:08pm UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/13 "2019-11-04T16:08:33Z")

</div>

> [@reagan](#):
>
> [SYNCT-327:Enable disabling of Manual Sync to Parent and Manual Pull t… by reagan-meant · Pull Request #149 · openmrs/openmrs-module-sync2 · GitHub](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;

```auto
@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.
}
```

---

<div class="post-metadata">

**Author:** ![reagan](https://talk.openmrs.org/user_avatar/talk.openmrs.org/reagan/32/10652_2.png) [@reagan](https://talk.openmrs.org/u/reagan)\
**Post date:** [November 21, 2019, 6:27pm UTC](https://talk.openmrs.org/t/nullpointerexception-when-using-mockito/25209/14 "2019-11-21T18:27:30Z")

</div>

@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](https://github.com/openmrs/openmrs-module-sync2/pull/149). Otherwise thanx for your input.
