0

I am trying to write unit tests using WebTestClient in my Spring boot application. But when tried to run, all controller test cases passed including some negative test cases.

Below is my MainApplication, Controller and ControllerTest code for your reference:

@SpringBootApplication
@EnableWebFlux
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}



@RunWith( SpringRunner.class )
@WebFluxTest(controllers = {MyController.class})
@AutoConfigureWebTestClient
public class MyControllerTests {

   @MockBean
   MyService service;

   @Autowired
   private WebTestClient webTestClient;


   @Test
   public void test() throws Exception {

       webTestClient
               .get()
               .uri(uriBuilder ->
                       uriBuilder
                              .path("/my-controller/test")
                               .build())
               .exchange()
               .expectBody(String.class)
               .equals("Hii");
   }
}



@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/my-controller")
public class MyController {
    private final MyService myService;

    @GetMapping("/test")
    public String test() throws Exception {
        System.out.println(">>>>>>>>>>>>>>>>>");
        return "Hello :)";
    }
}

1 Answers1

0

You are not actually asserting whether the the response body is equal or not. Try something like this:

boolean result = webTestClient
    .get()
    .uri(uriBuilder -> uriBuilder.path("/my-controller/test").build())
    .exchange()
    .expectBody(String.class)
    .equals("Hii");
Assertions.assertFalse(result); // this should pass
Rashin
  • 726
  • 6
  • 16