【问题标题】:Getting NPE on ControllerTest using SpringBoot WebFluxTest and JUnit4使用 Spring Boot WebFlux 测试和 JUnit 4 在控制器测试上获取 NPE
【发布时间】:2021-07-23 15:33:15
【问题描述】:

我有这个项目结构:

我正在尝试在控制器上测试一个虚拟方法。功能非常简单。您通过 POST 发送一个字符串,并返回一个 + "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);
    }
}

还有控制器测试类: 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"));
    }
}

然后,当我测试 exepectSatus().isOk() 时,测试通过了:

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

但是,如果我添加其余的功能,我会在“c”lambda 变量上得到一个 NPE,作为 Customer 对象。我是新来做这种测试的,所以我不知道发生了什么。

@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)

【问题讨论】:

  • 您能检查一下该测试中返回的响应内容吗?

标签: java spring-boot junit spring-test


【解决方案1】:

该示例模拟了CustomerServiceImpl,但没有对customerService.dummyEndpoint() 方法调用存根。

默认情况下,Mockito 将为非存根方法调用返回 null。这就是为什么只检查状态通过。由于值为 null,value(c -> c.equals("hello123") 将因 NPE 失败。

您需要存根方法调用:

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

当然这现在不测试真正的服务,但它不应该在@WebFluxTest 中测试。

显然这是一个虚拟示例,但如果您想测试服务功能,您可以编写一个简单的单元测试来验证调用服务方法是否返回所需的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 2018-10-17
    • 2018-01-02
    • 2021-02-08
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多