簡體   English   中英

WebTestclient 正在拋出 Null 異常艱難的@Autowire 在那里

[英]WebTestclient is throwing Null Exception tough @Autowire is there

該項目是 spring 啟動應用程序。 當嘗試從測試用例中作為 webtestclient 訪問 api 時,它會拋出空點異常。

class 和 Restcontroller an 和 testclass 如下

這是 class

@Component
public class GetMessage {
@Async
public void fetchData( fileType Type, String requestBody) {

        switch (A) {
            case a:
                operation(requestBody);
             break;
            case b:
                 // do some Operation
            break;
            default:
             break;
         }
 }
}

Rest controller

@RestController()
@RequestMapping("api/school/student-receiver/assignment")
public class  FileReceiverRestController {

@Autowired
    private objectMapper mapper;

@PostMapping(path = "/file", consumes = "application/json")
public ResponseEntity<String> receivefile(@RequestBody String reqBody) {        

    mapper.fetchData(fileType.A ,String reqBody);

    return new ResponseEntity<>(CALL_RESP, HttpStatus.OK);
}

測試 class

@AutoConfigureWebTestClient
public class E2ETest {

  @Autowired
    private WebTestClient webTestClient ;

   String ReceiverUrl="api/school/student-receiver/assignment/file"

     @Test
     pubilic void Testapi()
    {
    webTestClient  //====> facing null exception
            .post().uri(ReceiverUrl)
            //.header("Content-Type", "application/json")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(requestPayload)
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            // then
            .expectStatus().isEqualTo(HttpStatus.OK);
  }
   }

java.lang.NullPointerException at E2ETest.Testapi(E2ETest.java:176) 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(ReflectiveCallabl e.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.junit .runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org. junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.Z93F725A07423FE1C889 F448B33D21F46Z: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.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner. startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at Z4D236D9A2D102C5FE6AD1C50D A4BEC50Z.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

根據我對Spring引導項目的理解,使用@AutoconfigureWebTestClient僅在測試 WebFlux 應用程序時才有效。 當您使用 Spring MVC (Tomcat) 並且只想使用WebClient / WebTestClient時,這可能是原因。

@AutoconfigureWebTestClient的 Javadoc 也對此添加了提示:

/**
 * Annotation that can be applied to a test class to enable a {@link WebTestClient}. At
 * the moment, only WebFlux applications are supported.
 *
 * @author Stephane Nicoll
 * @since 2.0.0
 * @see WebTestClientAutoConfiguration
 */

盡管如此,有效的是使用@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)進行測試。 這將確保在后台自動配置TestRestTemplateWebTestClient

// if you are using JUnit 4 this is also needed
// @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WebTestClientAccessTest {

  @Autowired
  private WebTestClient webTestClient;

  @Autowired
  private TestRestTemplate testRestTemplate;

  @Test
  public void notNull() {
    assertNotNull(webTestClient);
    assertNotNull(testRestTemplate);
  }
}

這將啟動整個 Spring 上下文,您可以使用客戶端從外部訪問您的應用程序。

如果您不想啟動嵌入式 Tomcat 並加載所有 bean,您可以查看@WebMvcTest以專注於測試您的 web 層。 為此,您將使用MockMvc並使用模擬的 servlet 環境。 Spring Boot 的此入門指南提供了很好的介紹。

更多信息可以在Spring 引導文檔中找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM