To verify the everything works as expected in a real world scenario, an integration test can be written that, which puts everything together and tests each endpoint. The following is an integration test for the Moviee web service:
@RunWith(SpringRunner::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class SpringBoot2MovieRatingApplicationTests {
@Autowired
lateinit var webTestClient : WebTestClient;
@Test
fun `Get Movies - Happy Path`() {
webTestClient
.get()
.uri("/movies")
.header("Authorization", getBasicAuthorization())
.exchange()
.expectStatus()
.isOk()
.expectBody()
.json("""[{"id":1,"title":"Titanic","year":1999,"genre":
{"name":"Romantic","description":"Romantic"},"cast":
[{"name":"Lionardo Dicaprio","inAs":"Jack","noOfAwards":1},
{"name":"Kate Winslet","inAs":"Rose","noOfAwards":2}]}]""");
}
The preceding code snippet performs the integration testing for the Get Movies scenario's happy path:
@Test
fun `Get Movies - Failure Path (No Authorization)`() {
webTestClient
.get()
.uri("/movies")
.exchange()
.expectStatus()
.isUnauthorized()
}
The preceding code snippet performs the integration testing for the Get Movies scenario's failure path:
@Test
fun `Get Movie - Happy Path`() {
webTestClient
.get()
.uri("/movies/1")
.header("Authorization", getBasicAuthorization())
.exchange()
.expectStatus()
.isOk()
.expectBody()
.json("""{"id":1,"title":"Titanic","year":1999,"genre":
{"name":"Romantic","description":"Romantic"},"cast":
[{"name":"Lionardo Dicaprio","inAs":"Jack","noOfAwards":1},
{"name":"Kate Winslet","inAs":"Rose","noOfAwards":2}]}""")
}
The preceding code snippet performs the integration testing for the Get Movie scenario's happy path:
@Test
fun `Rate Movie - Happy Path`() {
webTestClient
.put()
.uri("/movies/1/rate?comment=Ok Movie&rating=3")
.header("Authorization", getBasicAuthorization())
.exchange()
.expectStatus()
.isOk()
.expectBody()
.json("""{"id":1,"title":"Titanic","year":1999,"genre":
{"name":"Romantic","description":"Romantic"},"ratings":
[{"comment":"Good Movie","rating":5},{"comment":"Ok
Movie","rating":3}],"cast":[{"name":"Lionardo
Dicaprio","inAs":"Jack","noOfAwards":1},{"name":"Kate
Winslet","inAs":"Rose","noOfAwards":2}]}""")
}
The preceding code snippet performs the integration testing for the Rate Movie scenario's happy path:
fun getBasicAuthorization() : String {
val plainCreds = "user:password"
val plainCredsBytes = plainCreds.toByteArray()
val base64CredsBytes =
Base64.getEncoder().encode(plainCredsBytes);
val base64Creds = String(base64CredsBytes)
return "Basic $base64Creds";
}
}
The preceding code snippet returns the Base64 encoded username and password to be sent as part of the authorization header.
The preceding integration test class is annotated with @SpringBootTest, which configures a webEnvironment that will start on a random port. By using WebTestClient, each particular endpoint can be invoked with a proper basic authentication header and verified responses. Integration tests are used to test from end to end before deploying to production.