Skip to content

Getting NPE on ControllerTest using SpringBoot WebFluxTest and JUnit4

I’ve this project structure:

enter image description here

And I am trying to test a dummy method on the Controller. The funcionality is very simple. You send a String by POST and is returned with a + “123”

CustomerServiceImpl.java

package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Override
    public String dummyEndpoint(String str) {
        return str + "123";
    }

}

CustomerController.java

package com.example.demo.controller;

import com.example.demo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/customers")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @PostMapping(value = {"/dummy"})
    @ResponseStatus(HttpStatus.OK)
    public String postDummy(@RequestBody String str) {
        return customerService.dummyEndpoint(str);
    }
}

And the controller test class: CustomerControllerTest.java

package com.example.demo.controller;

import com.example.demo.service.CustomerServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;

@WebFluxTest(controllers = CustomerController.class)
@RunWith(SpringRunner.class)
public class CustomerControllerTest {

    @Autowired
    WebTestClient webTestClient;

    @MockBean
    CustomerServiceImpl customerService;

    @Test
    public void dummyTest() {
        this.webTestClient.post().uri("/customers/dummy")
            .syncBody("hello")
            .exchange()
            .expectStatus().isOk()
            .expectBody(String.class)
            .value(c -> c.equals("hello123"));
    }
}

Then, when I test the exepectSatus().isOk() the test is passed:

@Test
public void dummyTest() {
    this.webTestClient.post().uri("/customers/dummy")
        .syncBody("hello")
        .exchange()
        .expectStatus().isOk();
}

enter image description here

But if I add the rest of funcionality I get a NPE on the ‘c’ lambda variable as a Customer object. I am new doing this kind of testing so I don’t know what is happening.

@Test
public void dummyTest() {
    this.webTestClient.post().uri("/customers/dummy")
        .syncBody("hello")
        .exchange()
        .expectStatus().isOk()
        .expectBody(String.class)
        .value(c -> c.equals("hello123"));
}

NPE:

java.lang.NullPointerException
    at com.example.demo.controller.CustomerControllerTest.lambda$dummyTest$0(CustomerControllerTest.java:29)
    at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodySpec.lambda$value$3(DefaultWebTestClient.java:407)
    at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:197)
    at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodySpec.value(DefaultWebTestClient.java:407)
    at com.example.demo.controller.CustomerControllerTest.dummyTest(CustomerControllerTest.java:29)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

Answer

The example is mocking CustomerServiceImpl but doesn’t stub the customerService.dummyEndpoint() method call.

By default Mockito will return null for a non-stubbed method call. This is why just checking the status passes. Since the value is null, value(c -> c.equals("hello123") will fail with NPE.

You’ll need to stub the method call:

when(customerService.dummyEndpoint("hello")).thenReturn("hello123");

Of course this doesn’t now test the real service, but it’s something that should not be tested in a @WebFluxTest.

Obviously this is a dummy example, but if you want to test the service functionality, you can write a plain unit test that verifies that calling the service method returns what is wanted.