Add a few more tests

This commit is contained in:
Alex Theedom 2016-08-08 20:42:44 +01:00
parent 2d2cefa4bf
commit 6e08121f61
3 changed files with 77 additions and 28 deletions

View File

@ -66,6 +66,7 @@
<build> <build>
<finalName>angular-spring-rest-sample</finalName> <finalName>angular-spring-rest-sample</finalName>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
@ -74,6 +75,7 @@
<target>1.8</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId> <artifactId>maven-war-plugin</artifactId>
@ -81,6 +83,7 @@
<failOnMissingWebXml>false</failOnMissingWebXml> <failOnMissingWebXml>false</failOnMissingWebXml>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -4,18 +4,21 @@ import org.baeldung.web.service.StudentService;
import org.baeldung.web.vo.Student; import org.baeldung.web.vo.Student;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import static org.springframework.http.MediaType.APPLICATION_JSON;
@RestController @RestController
public class StudentDirectoryRestController { public class StudentDirectoryRestController {
@Autowired @Autowired
private StudentService service; 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){ public Page<Student> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size){
Page<Student> resultPage = service.findPaginated(page, size); Page<Student> resultPage = service.findPaginated(page, size);

View File

@ -1,7 +1,14 @@
package org.baeldung.web.service; 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.assertFalse;
import static org.junit.Assert.assertTrue; 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.apache.commons.lang3.RandomStringUtils;
import org.baeldung.web.main.Application; import org.baeldung.web.main.Application;
@ -9,41 +16,77 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration; 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.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import io.restassured.RestAssured; import io.restassured.RestAssured;
import io.restassured.response.Response; import io.restassured.response.Response;
import org.springframework.web.client.RestTemplate;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class) @SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration @WebAppConfiguration
@IntegrationTest("server.port:8080") @IntegrationTest("server.port:8888")
public class StudentServiceTest{ public class StudentServiceTest {
private String getURL() { private static final String ENDPOINT = "http://localhost:8080/StudentDirectory/student/get";
return "/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() );
}
} }