1

我有以下代码,并且一直收到 404 not found 错误?任何建议将不胜感激!

我研究了似乎不是问题的相互冲突的依赖关系。我还确保我返回了正确的内容类型。

我不确定我做对的一件事是使用 Bean 和 Autowired 注释进行注释。我现在对那些人的所作所为知之甚少。

下面的路由器类

    @Configuration
@AllArgsConstructor
public class AgencyRouter {

    @Bean
    public RouterFunction<ServerResponse> agencyRoutes(AgencyController agencyController) {
       return RouterFunctions
            .route(RequestPredicates.POST("/agency").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), agencyController::createAgency);
    }
}

下面的控制器/处理程序类

@Component
public class AgencyController {

    public Mono<ServerResponse> createAgency(ServerRequest request){
        return ServerResponse.ok()
//                .contentType(MediaType.APPLICATION_JSON)
                .body(
                        Flux.just("Test", "message")
                                .delayElements(Duration.ofSeconds(1)).log(), String.class
                );
    }
}

测试班

@AutoConfigureWebTestClient
public class AgencyControllerTest {

    @Autowired
    private WebTestClient webClient;

    @Test
    void testCreateAgency() {
        AgencyRequest request = new AgencyRequest(new AgencyRequestFields("TestName", true));
        webClient.post()
                .uri("/agency")
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(request))
                .exchange()
                .expectStatus().is2xxSuccessful();
    }


}

build.gradle 依赖项

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    //implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.flywaydb:flyway-core'
    implementation 'org.springdoc:springdoc-openapi-webflux-ui:1.5.2'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'org.postgresql:postgresql'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
    testImplementation 'org.testcontainers:junit-jupiter'
    testImplementation 'org.testcontainers:postgresql'
}

提前致谢!!!

4

1 回答 1

0

最简单的方法就是添加@SpringBootTest您的AgencyControllerTest课程(以及@AutoConfigureWebTestClient您已经拥有的课程):

@SpringBootTest
@AutoConfigureWebTestClient
public class AgencyControllerTest {

...它与您的 Web 测试客户端一起设置完整的自动配置。然后你不需要做任何其他事情——你所有的控制器、路由等都将可用。如果您刚刚开始,这可能是最容易记住的。

或者,您可以使用@WebFluxTestand@ContextConfiguration仅实例化此特定测试所需的上下文:

@WebFluxTest
@AutoConfigureWebTestClient
@ContextConfiguration(classes = {AgencyController.class, AgencyRouter.class})
public class AgencyControllerTest {

如果您设置了其他路由、控制器、bean 等并且只需要一个小子集,那么这种方法会更有效,因为您不需要为每个测试设置和拆除整个上下文(只需要您需要的)。

我不确定我做对的一件事是使用 Bean 和 Autowired 注释进行注释。我现在对那些人的所作所为知之甚少。

我建议好好看看依赖注入和控制反转(理论上和在 Spring 上下文中)——这几乎是 Spring 的基础,除非你有(在至少)对此有基本的了解。

于 2021-11-10T08:14:26.600 回答