testing work

This commit is contained in:
eugenp 2014-01-05 17:11:38 +02:00
parent 33fee76a32
commit c7ce725cf6
4 changed files with 346 additions and 257 deletions

View File

@ -1,5 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId> <groupId>org.baeldung</groupId>
<artifactId>spring-security-rest-full</artifactId> <artifactId>spring-security-rest-full</artifactId>
@ -187,6 +186,13 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -68,7 +68,6 @@ public class FooController {
@RequestMapping(params = { "page", "size" }, method = RequestMethod.GET) @RequestMapping(params = { "page", "size" }, method = RequestMethod.GET)
@ResponseBody @ResponseBody
public List<Foo> findPaginated(@RequestParam("page") final int page, @RequestParam("size") final int size, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) { public List<Foo> findPaginated(@RequestParam("page") final int page, @RequestParam("size") final int size, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) {
final Page<Foo> resultPage = service.findPaginated(page, size); final Page<Foo> resultPage = service.findPaginated(page, size);
if (page > resultPage.getTotalPages()) { if (page > resultPage.getTotalPages()) {
throw new MyResourceNotFoundException(); throw new MyResourceNotFoundException();

View File

@ -0,0 +1,72 @@
package org.baeldung.common.web;
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import java.io.Serializable;
import java.util.List;
import org.junit.Test;
import com.google.common.base.Preconditions;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;
public abstract class AbstractLiveTest<T extends Serializable> {
protected final Class<T> clazz;
public AbstractLiveTest(final Class<T> clazzToSet) {
super();
Preconditions.checkNotNull(clazzToSet);
clazz = clazzToSet;
}
// tests
// find - one
// find - all
// find - all - paginated
@Test
public void whenResourcesAreRetrievedPaged_then200IsReceived() {
final Response response = givenAuth().get(getFooURL() + "?page=1&size=10");
assertThat(response.getStatusCode(), is(200));
}
@Test
public void whenPageOfResourcesAreRetrievedOutOfBounds_then404IsReceived() {
final Response response = givenAuth().get(getFooURL() + "?page=" + randomNumeric(5) + "&size=10");
assertThat(response.getStatusCode(), is(404));
}
@Test
public void givenResourcesExist_whenFirstPageIsRetrieved_thenPageContainsResources() {
// restTemplate.createResource();
final Response response = givenAuth().get(getFooURL() + "?page=1&size=10");
assertFalse(response.body().as(List.class).isEmpty());
}
// count
// template method
private String getFooURL() {
return "http://localhost:8080/spring-security-rest-full/foos";
}
protected final RequestSpecification givenAuth() {
return RestAssured.given().auth().preemptive().basic("user1", "user1Pass");
}
}

View File

@ -0,0 +1,12 @@
package org.baeldung.web;
import org.baeldung.common.web.AbstractLiveTest;
import org.baeldung.persistence.model.Foo;
public class FooLiveTest extends AbstractLiveTest<Foo> {
public FooLiveTest() {
super(Foo.class);
}
}