0

I am using mockito core4.4.0 and JUnit5 and have test:

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class ElasticSearchLoggerTest {

    private CloseableHttpClient client;
    private CloseableHttpResponse response;

    @BeforeEach
    public void setUp() {
        response = Mockito.mock(CloseableHttpResponse.class);
        client = Mockito.mock(CloseableHttpClient.class);
    }
   ....
}

However, this mocking results in NPE, i tried to use @Mock annotation instead of Mocking with Mockito.mock() but the result is the same.

Why is this null?

Johnyb
  • 980
  • 1
  • 12
  • 27
  • Are response or client really `null` or is NPE thrown during usage? – Christoph Dahlen Nov 23 '22 at 09:36
  • @ChristophDahlen i checked , right after mocking in debugger it says `Method threw 'java.lang.NullPointerException' exception. Cannot evaluate org.apache.http.impl.client.CloseableHttpClient$MockitoMock$268347003.toString()` – Johnyb Nov 23 '22 at 09:38
  • @ChristophDahlen however when i run the tests using maven it behaves correctly for some reason – Johnyb Nov 23 '22 at 09:39
  • Please post entire stack trace. Note that debugging mockito code has some tricky parts: https://stackoverflow.com/questions/69626405/mockito-unfinished-stubbing-exception-detected-only-in-intellij-debugger/69628236 – Lesiak Nov 23 '22 at 10:54
  • thats it, only NPE – Johnyb Nov 23 '22 at 12:02

0 Answers0