Testing controllers

You should test the controller for successful business logic execution and response generation. The following test case is written for MovieController, which is annotated with @WebFluxTest and has an autowired WebTestClient, which will be used to invoke the endpoints:

@RunWith(SpringRunner::class)
@WebFluxTest(MovieController::class)
@ActiveProfiles("dev")
class MovieControllerTest {

@MockBean
lateinit var movieService: MovieService;

@MockBean
lateinit var movieRepository: MovieRepository;

@Autowired
lateinit var webTestClient: WebTestClient;

The preceding code snippet performs creates mocking beans and the testing utility:

    @Test
fun `List Movies - Happy Path`() {
// Given
var movieFlux: Flux<Movie> = Flux.fromIterable(listOf(getMovie()));
`when`(movieService.findAll()).thenReturn(movieFlux);

// When
webTestClient.get().uri("/movies")
.header(HttpHeaders.CONTENT_TYPE, "application/json")
.exchange()
.expectStatus().isOk()
.expectBody()
.json("""
[{"id":1,"title":"Avengers","year":2018,"genre":
{"name":"Action","description":"Action"},"ratings":[],
"cast":[]}]"""
);

// Then
verify(movieService, times(1)).findAll();
}

The preceding code snippet performs the controller testing for the List Movies scenario's happy path:

    @Test
fun `Rate Movie - Happy Path`() {
// Given
var movie = getMovie();
movie.ratings.add(MovieRating("Great", 5, Date()))
`when`(movieService.rate(1, "Great",
5)).thenReturn(Mono.just(movie));

// When
webTestClient.put().uri("/movies/1/rate?
comment=Great&rating=5"
)
.exchange()
.expectStatus().isOk()
.expectBody()
.json("""
{"id":1,"title":"Avengers","year":2018,"genre":
{"name":"Action","description":"Action"},"ratings":
[{"comment":"Great","rating":5}],"cast":[]}"""
)

// Then
verify(movieService, times(1)).rate(1, "Great", 5)
}

The preceding code snippet performs the controller testing for the Rate Movie scenario's happy path:

    @Test
fun `Rate Movie - Failure Path`() {
// Given
`when`(movieService.rate(2, "Great",
5)).thenReturn(Mono.error(MovieNotFoundException.create(2)));

// When
webTestClient.put().uri("/movies/2/rate?
comment=Great&rating=5"
)
.exchange()
.expectStatus().isBadRequest()
.expectBody()
.json("""{"code":400,"message":"Movie by id 2, not
found"}"""
)

// Then
verify(movieService, times(1)).rate(2, "Great", 5)
}

The preceding code snippet performs the controller testing of the Rate Movie scenario's failure path:


@Test
fun `Get Movie by Id - Happy Path`() {
// Given
`when`(movieService.findOne(1))
.thenReturn(Mono.just(getMovie()));

// When
webTestClient.get().uri("/movies/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.json("""
{"id":1,"title":"Avengers","year":2018,"genre":
{"name":"Action","description":"Action"},
"ratings":[],"cast":[]}"""
)

// Then
verify(movieService, times(1)).findOne(1);
}

The preceding code snippet does the controller testing of the Get Movie by Id scenario's happy path:

    fun getMovie() : Movie {
return Movie(1, "Avengers", 2018, MovieGenre("Action",
"Action"), ArrayList<MovieRating>(), ArrayList<Actor>())
}
}

The preceding code snippet creates mock Movie data for testing.

In the preceding controller test case, WebTestClient is used to hit particular endpoints of the controller and verify whether it returns the correct status codes, bodies, and so on. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.108.86