4

I'm trying to write some tests and I facing the following exception, when I try to post a byte array body:

error

java.lang.AssertionError: Status expected:<201> but was:<404>

> POST /api/foo
> WebTestClient-Request-Id: [1]
> Content-Length: [246444]

Content not available yet

< 404 Not Found
< 

Content not available yet

My Test class is the following:

@AutoConfigureWebTestClient
@RunWith(SpringRunner.class)
@FixMethodOrder(NAME_ASCENDING)
@SpringBootTest(classes = Application.class)
public class ControllerTest {

  @Inject
  protected WebTestClient webClient;

  @Test
  public void testPostFile() throws Exception {

    byte[] bytes;

    try (InputStream is = getClass().getClassLoader().getResourceAsStream("static/binary-file.docx")) {
      bytes = IOUtils.toByteArray(is);
    }

    webClient
      .post()
      .uri("/api/foo")
      .body(BodyInserters.fromResource(new ByteArrayResource(bytes))))
      .exchange()
      .expectStatus().isCreated();

  }

}

Is anyone facing the same problem, it seems it cannot load the resource properly

EDIT:

My Application Bootstrap class

@EnableWebFlux
@Configuration
@ComponentScan(basePackageClasses = SBApplication.class)
@EnableAutoConfiguration
public class SBApplication {

    /**
     * Spring Boot Main Entry
     *
     * @param args command line arguments
     * @throws Exception on failure
     */
    public static void main(String[] args) throws Exception {

        ConfigurableApplicationContext ctx = new SpringApplicationBuilder()
                .sources(SBApplication.class)
                .run(args);
    }
}

My Controller Class:

@RestController
@RequestMapping("/api")
public class SBController {

  // ...

  @RequestMapping(
    method = POST,
    path = "{name}"
  )
  public Mono<ResponseEntity> store(
            @PathVariable("name") String name,
            @RequestBody byte[] body) {

    // do stuff

    return Mono.just(
      ResponseEntity
          .status(HttpStatus.CREATED)
          .build()
    );

  }

}
tbo
  • 9,398
  • 8
  • 40
  • 51
  • maybe this is just a 404 as suggested by the logs? Please provide more information about the controller (like a code snippet). Did you try debugging the controller? Is it called? – Brian Clozel Mar 28 '18 at 12:12
  • hi @BrianClozel thanks for your comment, yes I did try to perform a debug and the data (byte array) are actually there. The problem is that at some point when I set it as body the problems start to occur. I cannot find as well a clean example of byte array as body – tbo Mar 28 '18 at 13:03

1 Answers1

2

Eventually, the issues was that my Controller is configured via annotations. WebTestClient can only recognize mapping endpoints that are routed via the RouterFunction.

In order to test a Controller which is configured with annotations such as my example you will need to initialize a MockMvc and with the corresponding annotation @AutoConfigureMockMvc

Finally, since reactor-core is used we need to utilize MockMvcRequestBuilders.asyncDispatch as follows:

@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
@FixMethodOrder(NAME_ASCENDING)
@SpringBootTest(classes = Application.class)
public class ControllerTest {

  @Inject
  protected MockMvc mockMvc;

  @Test
  public void testPostFile() throws Exception {

    byte[] bytes;

    try (InputStream is = getClass().getClassLoader().getResourceAsStream("static/binary-file.docx")) {
      bytes = IOUtils.toByteArray(is);
    }

    MvcResult result = mockMvc.perform(
                post("/api/foo")
                        .content(bytes)
        )
                .andExpect(request().asyncStarted())
                .andReturn();

        mockMvc.perform(asyncDispatch(result))
                .andExpect(status().isCreated());

  }

}

And finally everything works as expected!

tbo
  • 9,398
  • 8
  • 40
  • 51