1

I have the following code, and am consistently receiving 404 not found errors? Any advice would be much appreciated!

I've researched conflicting dependencies which does not seem to be the problem. I've also ensured that I am returning the correct content type.

One thing that I am not sure I'm doing correctly is annotating with the Bean and Autowired annotations. I have little understanding of what those do at the moment.

Router class below

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

Controller/Handler class below

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

Test 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

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'
}

Thanks in advance!!!

Aluxxen
  • 63
  • 6

1 Answers1

0

The easiest way is just to add @SpringBootTest on your AgencyControllerTest class (along with @AutoConfigureWebTestClient which you already have):

@SpringBootTest
@AutoConfigureWebTestClient
public class AgencyControllerTest {

...which sets up full auto-configuration along with your web test client. Then you don't need to do anything else - all your controllers, routes etc. will be available. This is probably easiest to remember if you're just getting started.

Alternatively, you can use @WebFluxTest and @ContextConfiguration to just instantiate the context you need for this particular test:

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

If you have other routes, controllers, beans, etc. set up and only need a small subset then this approach is more efficient, as you're not setting up and tearing down the entire context for each test (only what you need.)

One thing that I am not sure I'm doing correctly is annotating with the Bean and Autowired annotations. I have little understanding of what those do at the moment.

I'd recommend taking a good look at dependency injection & inversion of control (both theoretically and in a Spring context) - this is pretty much the foundation of Spring, and you'll almost certainly come unstuck at some point unless you have (at least) a base level understanding of this.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216