|
1 | 1 | package org.baeldung.web.service; |
2 | 2 |
|
| 3 | +import static io.restassured.RestAssured.given; |
| 4 | +import static org.hamcrest.core.Is.is; |
| 5 | +import static org.hamcrest.core.IsCollectionContaining.hasItems; |
| 6 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 7 | + |
3 | 8 | import org.apache.commons.lang3.RandomStringUtils; |
4 | 9 | import org.baeldung.web.main.Application; |
5 | 10 | import org.junit.Test; |
6 | 11 | import org.junit.runner.RunWith; |
7 | 12 | import org.springframework.boot.test.context.SpringBootTest; |
8 | 13 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 14 | +import org.springframework.boot.web.server.LocalServerPort; |
9 | 15 | import org.springframework.test.context.junit4.SpringRunner; |
10 | 16 |
|
11 | | -import static io.restassured.RestAssured.given; |
12 | | -import static org.hamcrest.core.Is.is; |
13 | | -import static org.hamcrest.core.IsCollectionContaining.hasItems; |
14 | | -import static org.hamcrest.core.IsEqual.equalTo; |
15 | | - |
16 | 17 | @RunWith(SpringRunner.class) |
17 | | -@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT) |
| 18 | +@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT) |
18 | 19 | public class StudentServiceIntegrationTest { |
| 20 | + |
| 21 | + @LocalServerPort |
| 22 | + int port; |
19 | 23 |
|
20 | | - private static final String ENDPOINT = "http://localhost:8080/student/get"; |
| 24 | + private static final String ENDPOINT = "http://localhost:%s/student/get"; |
21 | 25 |
|
22 | 26 | @Test |
23 | 27 | public void givenRequestForStudents_whenPageIsOne_expectContainsNames() { |
24 | | - given().params("page", "0", "size", "2").get(ENDPOINT).then().assertThat() |
| 28 | + given().params("page", "0", "size", "2").get(String.format(ENDPOINT, port)).then().assertThat() |
25 | 29 | .body("content.name", hasItems("Bryan", "Ben")); |
26 | 30 | } |
27 | 31 |
|
28 | 32 | @Test |
29 | 33 | public void givenRequestForStudents_whenSizeIsTwo_expectTwoItems() { |
30 | | - given().params("page", "0", "size", "2").get(ENDPOINT).then().assertThat().body("size", equalTo(2)); |
| 34 | + given().params("page", "0", "size", "2").get(String.format(ENDPOINT, port)).then().assertThat().body("size", equalTo(2)); |
31 | 35 | } |
32 | 36 |
|
33 | 37 | @Test |
34 | 38 | public void givenRequestForStudents_whenSizeIsTwo_expectNumberOfElementsTwo() { |
35 | | - given().params("page", "0", "size", "2").get(ENDPOINT).then().assertThat().body("numberOfElements", equalTo(2)); |
| 39 | + given().params("page", "0", "size", "2").get(String.format(ENDPOINT, port)).then().assertThat().body("numberOfElements", equalTo(2)); |
36 | 40 | } |
37 | 41 |
|
38 | 42 | @Test |
39 | 43 | public void givenRequestForStudents_whenResourcesAreRetrievedPaged_thenExpect200() { |
40 | | - given().params("page", "0", "size", "2").get(ENDPOINT).then().statusCode(200); |
| 44 | + given().params("page", "0", "size", "2").get(String.format(ENDPOINT, port)).then().statusCode(200); |
41 | 45 | } |
42 | 46 |
|
43 | 47 | @Test |
44 | 48 | public void givenRequestForStudents_whenPageOfResourcesAreRetrievedOutOfBounds_thenExpect500() { |
45 | | - given().params("page", "1000", "size", "2").get(ENDPOINT).then().statusCode(500); |
| 49 | + given().params("page", "1000", "size", "2").get(String.format(ENDPOINT, port)).then().statusCode(500); |
46 | 50 | } |
47 | 51 |
|
48 | 52 | @Test |
49 | 53 | public void givenRequestForStudents_whenPageNotValid_thenExpect500() { |
50 | | - given().params("page", RandomStringUtils.randomNumeric(5), "size", "2").get(ENDPOINT).then().statusCode(500); |
| 54 | + given().params("page", RandomStringUtils.randomNumeric(5), "size", "2").get(String.format(ENDPOINT, port)).then().statusCode(500); |
51 | 55 | } |
52 | 56 |
|
53 | 57 | @Test |
54 | 58 | public void givenRequestForStudents_whenPageSizeIsFive_expectFiveItems() { |
55 | | - given().params("page", "0", "size", "5").get(ENDPOINT).then().body("content.size()", is(5)); |
| 59 | + given().params("page", "0", "size", "5").get(String.format(ENDPOINT, port)).then().body("content.size()", is(5)); |
56 | 60 | } |
57 | 61 |
|
58 | 62 | @Test |
59 | 63 | public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() { |
60 | | - given().params("page", "0", "size", "2").get(ENDPOINT).then().assertThat().body("first", equalTo(true)); |
| 64 | + given().params("page", "0", "size", "2").get(String.format(ENDPOINT, port)).then().assertThat().body("first", equalTo(true)); |
61 | 65 | } |
62 | 66 | } |
0 commit comments