Add a few more tests
This commit is contained in:
parent
2d2cefa4bf
commit
6e08121f61
|
@ -66,6 +66,7 @@
|
|||
<build>
|
||||
<finalName>angular-spring-rest-sample</finalName>
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
|
@ -74,6 +75,7 @@
|
|||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
|
@ -81,6 +83,7 @@
|
|||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
|
|
@ -4,18 +4,21 @@ import org.baeldung.web.service.StudentService;
|
|||
import org.baeldung.web.vo.Student;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||
|
||||
@RestController
|
||||
public class StudentDirectoryRestController {
|
||||
|
||||
@Autowired
|
||||
private StudentService service;
|
||||
|
||||
@RequestMapping(value = "/student/get", params = { "page", "size" }, method = RequestMethod.GET)
|
||||
@RequestMapping(value = "/student/get", params = { "page", "size" }, method = RequestMethod.GET, produces = "application/json")
|
||||
public Page<Student> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size){
|
||||
|
||||
Page<Student> resultPage = service.findPaginated(page, size);
|
||||
|
|
|
@ -1,7 +1,14 @@
|
|||
package org.baeldung.web.service;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.IsCollectionContaining.hasItems;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.hamcrest.core.StringContains.containsString;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||
import static org.springframework.util.Assert.isTrue;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.baeldung.web.main.Application;
|
||||
|
@ -9,41 +16,77 @@ import org.junit.Test;
|
|||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.IntegrationTest;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.TestRestTemplate;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@WebAppConfiguration
|
||||
@IntegrationTest("server.port:8080")
|
||||
public class StudentServiceTest{
|
||||
@WebAppConfiguration
|
||||
@IntegrationTest("server.port:8888")
|
||||
public class StudentServiceTest {
|
||||
|
||||
private String getURL() {
|
||||
return "/StudentDirectory/student/get";
|
||||
private static final String ENDPOINT = "http://localhost:8080/StudentDirectory/student/get";
|
||||
|
||||
@Test
|
||||
public void givenRequestForStudents_whenPageIsOne_expectContainsNames() {
|
||||
given().params("page", "1", "size", "2").get(ENDPOINT)
|
||||
.then()
|
||||
.assertThat().body("content.name", hasItems("Bryan", "Ben"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestForStudents_whenSizeIsTwo_expectTwoItems() {
|
||||
given().params("page", "1", "size", "2").get(ENDPOINT)
|
||||
.then()
|
||||
.assertThat().body("size", equalTo(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestForStudents_whenSizeIsTwo_expectNumberOfElementsTwo() {
|
||||
given().params("page", "1", "size", "2").get(ENDPOINT)
|
||||
.then()
|
||||
.assertThat().body("numberOfElements", equalTo(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestForStudents_whenResourcesAreRetrievedPaged_thenExpect200() {
|
||||
given().params("page", "1", "size", "2").get(ENDPOINT)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestForStudents_whenPageOfResourcesAreRetrievedOutOfBounds_thenExpect500() {
|
||||
given().params("page", "1000", "size", "2").get(ENDPOINT)
|
||||
.then()
|
||||
.statusCode(500);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestForStudents_whenPageNotValid_thenExpect500() {
|
||||
given().params("page", RandomStringUtils.randomNumeric(5), "size", "2").get(ENDPOINT)
|
||||
.then()
|
||||
.statusCode(500);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
|
||||
given().params("page", "1", "size", "2").get(ENDPOINT)
|
||||
.then()
|
||||
.assertThat().body("first", equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequestForStudents_whenPageIsFive_expectFiveItems() {
|
||||
given().params("page", "1", "size", "5").get(ENDPOINT)
|
||||
.then()
|
||||
.body("content.studentId.max()", equalTo("5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenResourcesAreRetrievedPaged_then200IsReceived(){
|
||||
Response response = RestAssured.given().get(getURL()+ "?page=0&size=2").andReturn();
|
||||
|
||||
assertTrue(response.getStatusCode() == 200 );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived(){
|
||||
String url = getURL()+ "?page=" + RandomStringUtils.randomNumeric(5) + "&size=2";
|
||||
Response response = RestAssured.given().get(url);
|
||||
|
||||
assertTrue(response.getStatusCode() == 500 );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources(){
|
||||
Response response = RestAssured.given().get(getURL() + "?page=1&size=2" );
|
||||
assertFalse(response.getBody().jsonPath().getList("content").isEmpty() );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue