Skip to content

Commit 6e08121

Browse files
committed
Add a few more tests
1 parent 2d2cefa commit 6e08121

3 files changed

Lines changed: 79 additions & 30 deletions

File tree

‎spring-rest-angular-pagination/StudentDirectory/pom.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<build>
6767
<finalName>angular-spring-rest-sample</finalName>
6868
<plugins>
69+
6970
<plugin>
7071
<groupId>org.apache.maven.plugins</groupId>
7172
<artifactId>maven-compiler-plugin</artifactId>
@@ -74,13 +75,15 @@
7475
<target>1.8</target>
7576
</configuration>
7677
</plugin>
78+
7779
<plugin>
7880
<groupId>org.apache.maven.plugins</groupId>
7981
<artifactId>maven-war-plugin</artifactId>
8082
<configuration>
8183
<failOnMissingWebXml>false</failOnMissingWebXml>
8284
</configuration>
8385
</plugin>
86+
8487
</plugins>
8588
</build>
8689
</project>

‎spring-rest-angular-pagination/StudentDirectory/src/main/java/org/baeldung/web/rest/StudentDirectoryRestController.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
import org.baeldung.web.vo.Student;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.data.domain.Page;
7+
import org.springframework.http.MediaType;
78
import org.springframework.web.bind.annotation.RequestMapping;
89
import org.springframework.web.bind.annotation.RequestMethod;
910
import org.springframework.web.bind.annotation.RequestParam;
1011
import org.springframework.web.bind.annotation.RestController;
1112

13+
import static org.springframework.http.MediaType.APPLICATION_JSON;
14+
1215
@RestController
1316
public class StudentDirectoryRestController {
1417

1518
@Autowired
1619
private StudentService service;
1720

18-
@RequestMapping(value = "/student/get", params = { "page", "size" }, method = RequestMethod.GET)
21+
@RequestMapping(value = "/student/get", params = { "page", "size" }, method = RequestMethod.GET, produces = "application/json")
1922
public Page<Student> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size){
2023

2124
Page<Student> resultPage = service.findPaginated(page, size);
Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,92 @@
11
package org.baeldung.web.service;
22

3+
import static io.restassured.RestAssured.given;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
import static org.hamcrest.core.IsCollectionContaining.hasItems;
6+
import static org.hamcrest.core.IsEqual.equalTo;
7+
import static org.hamcrest.core.StringContains.containsString;
38
import static org.junit.Assert.assertFalse;
49
import static org.junit.Assert.assertTrue;
10+
import static org.springframework.http.MediaType.APPLICATION_JSON;
11+
import static org.springframework.util.Assert.isTrue;
512

613
import org.apache.commons.lang3.RandomStringUtils;
714
import org.baeldung.web.main.Application;
815
import org.junit.Test;
916
import org.junit.runner.RunWith;
1017
import org.springframework.boot.test.IntegrationTest;
1118
import org.springframework.boot.test.SpringApplicationConfiguration;
19+
import org.springframework.boot.test.TestRestTemplate;
20+
import org.springframework.http.HttpHeaders;
21+
import org.springframework.http.ResponseEntity;
1222
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1323
import org.springframework.test.context.web.WebAppConfiguration;
1424

1525
import io.restassured.RestAssured;
1626
import io.restassured.response.Response;
27+
import org.springframework.web.client.RestTemplate;
1728

1829
@RunWith(SpringJUnit4ClassRunner.class)
1930
@SpringApplicationConfiguration(classes = Application.class)
20-
@WebAppConfiguration
21-
@IntegrationTest("server.port:8080")
22-
public class StudentServiceTest{
23-
24-
private String getURL() {
25-
return "/StudentDirectory/student/get";
26-
}
27-
28-
@Test
29-
public void whenResourcesAreRetrievedPaged_then200IsReceived(){
30-
Response response = RestAssured.given().get(getURL()+ "?page=0&size=2").andReturn();
31-
32-
assertTrue(response.getStatusCode() == 200 );
33-
}
34-
35-
@Test
36-
public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived(){
37-
String url = getURL()+ "?page=" + RandomStringUtils.randomNumeric(5) + "&size=2";
38-
Response response = RestAssured.given().get(url);
39-
40-
assertTrue(response.getStatusCode() == 500 );
41-
}
42-
43-
@Test
44-
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources(){
45-
Response response = RestAssured.given().get(getURL() + "?page=1&size=2" );
46-
assertFalse(response.getBody().jsonPath().getList("content").isEmpty() );
47-
}
48-
31+
@WebAppConfiguration
32+
@IntegrationTest("server.port:8888")
33+
public class StudentServiceTest {
34+
35+
private static final String ENDPOINT = "http://localhost:8080/StudentDirectory/student/get";
36+
37+
@Test
38+
public void givenRequestForStudents_whenPageIsOne_expectContainsNames() {
39+
given().params("page", "1", "size", "2").get(ENDPOINT)
40+
.then()
41+
.assertThat().body("content.name", hasItems("Bryan", "Ben"));
42+
}
43+
44+
@Test
45+
public void givenRequestForStudents_whenSizeIsTwo_expectTwoItems() {
46+
given().params("page", "1", "size", "2").get(ENDPOINT)
47+
.then()
48+
.assertThat().body("size", equalTo(2));
49+
}
50+
51+
@Test
52+
public void givenRequestForStudents_whenSizeIsTwo_expectNumberOfElementsTwo() {
53+
given().params("page", "1", "size", "2").get(ENDPOINT)
54+
.then()
55+
.assertThat().body("numberOfElements", equalTo(2));
56+
}
57+
58+
@Test
59+
public void givenRequestForStudents_whenResourcesAreRetrievedPaged_thenExpect200() {
60+
given().params("page", "1", "size", "2").get(ENDPOINT)
61+
.then()
62+
.statusCode(200);
63+
}
64+
65+
@Test
66+
public void givenRequestForStudents_whenPageOfResourcesAreRetrievedOutOfBounds_thenExpect500() {
67+
given().params("page", "1000", "size", "2").get(ENDPOINT)
68+
.then()
69+
.statusCode(500);
70+
}
71+
72+
@Test
73+
public void givenRequestForStudents_whenPageNotValid_thenExpect500() {
74+
given().params("page", RandomStringUtils.randomNumeric(5), "size", "2").get(ENDPOINT)
75+
.then()
76+
.statusCode(500);
77+
}
78+
79+
@Test
80+
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
81+
given().params("page", "1", "size", "2").get(ENDPOINT)
82+
.then()
83+
.assertThat().body("first", equalTo(true));
84+
}
85+
86+
@Test
87+
public void givenRequestForStudents_whenPageIsFive_expectFiveItems() {
88+
given().params("page", "1", "size", "5").get(ENDPOINT)
89+
.then()
90+
.body("content.studentId.max()", equalTo("5"));
91+
}
4992
}

0 commit comments

Comments
 (0)