使用 SpringBoot WebFluxTest 和 JUnit4 在 ControllerTest 上获取 NPE
Getting NPE on ControllerTest using SpringBoot WebFluxTest and JUnit4
我有这个项目结构:
我正在尝试在控制器上测试一个虚拟方法。功能非常简单。您通过 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);
}
}
和控制器测试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"));
}
}
然后,当我测试 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[=15=](CustomerControllerTest.java:29)
at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodySpec.lambda$value(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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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)
该示例是在模拟 CustomerServiceImpl
但不会对 customerService.dummyEndpoint()
方法调用进行存根。
默认情况下,Mockito 将 return null
用于非存根方法调用。这就是为什么只检查状态通过。由于该值为空,value(c -> c.equals("hello123")
将因 NPE 而失败。
您需要存根方法调用:
when(customerService.dummyEndpoint("hello")).thenReturn("hello123");
当然这现在不测试真正的服务,但它不应该在 @WebFluxTest
.
中测试。
显然这是一个虚拟示例,但如果您想测试服务功能,您可以编写一个简单的单元测试来验证调用服务方法return是想要的。
我有这个项目结构:
我正在尝试在控制器上测试一个虚拟方法。功能非常简单。您通过 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);
}
}
和控制器测试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"));
}
}
然后,当我测试 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[=15=](CustomerControllerTest.java:29)
at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodySpec.lambda$value(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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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)
该示例是在模拟 CustomerServiceImpl
但不会对 customerService.dummyEndpoint()
方法调用进行存根。
默认情况下,Mockito 将 return null
用于非存根方法调用。这就是为什么只检查状态通过。由于该值为空,value(c -> c.equals("hello123")
将因 NPE 而失败。
您需要存根方法调用:
when(customerService.dummyEndpoint("hello")).thenReturn("hello123");
当然这现在不测试真正的服务,但它不应该在 @WebFluxTest
.
显然这是一个虚拟示例,但如果您想测试服务功能,您可以编写一个简单的单元测试来验证调用服务方法return是想要的。