Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit for method in controller layer

My rest endpoint looks like:


@GetMapping("/mango/{id}")
public Mono<Mango> getMango(@PathVariable("id") final String id){

validateId(id);
return serviceLayer.someMonoData();
}

My Junit looks like:


@Test
public void method(){

when(serviceLayer.someMonoData()).thenReturn(someResponse);

webTestClient.get()
   .uri(ENDPOINT_URL)
   .headers(headers)
   .exchange()
   .expectStatus()
   .isBadRequest();
}

The validateId(id); method is supposed to throw custom bad request exception for an invalid id. This method is not reactive.

But the Junit seems to bypass the validate() method and proceeds with the service call stub..

What am i missing?

like image 484
user1354825 Avatar asked May 31 '26 19:05

user1354825


1 Answers

In reactive you need to construct the flow using reactive operators that will be executed when web flux subscribes to this flow. In you case validateId is not part of the flow.

Not sure what is the contract of the validateId() but, assuming it's not reactive, you can use

return Mono.fromRunnable(() -> validateId(id))
    .then(serviceLayer.someMonoData());
like image 150
Alex Avatar answered Jun 02 '26 08:06

Alex