Skip to content

Instantly share code, notes, and snippets.

@takaki
Created July 4, 2016 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takaki/c0f7e3561719fb2f84c2b172d22d70f3 to your computer and use it in GitHub Desktop.
Save takaki/c0f7e3561719fb2f84c2b172d22d70f3 to your computer and use it in GitHub Desktop.
JAX-RSのBean Validationの基礎 ref: http://qiita.com/takaki@github/items/40f8bc02dc06a0e2848b
compile group: 'org.glassfish.jersey.ext', name: 'jersey-bean-validation', version: '2.23.1'
package example.jaxrs;
import javax.validation.constraints.NotNull;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
@Path("/")
public class HelloValidateResource {
@GET
public String index(@NotNull @QueryParam("name") String name) {
return "Hello " + name;
}
}
package example.jaxrs
import org.glassfish.jersey.server.ResourceConfig
import org.glassfish.jersey.test.JerseyTest
import spock.lang.Shared
import spock.lang.Specification
import javax.ws.rs.core.Application
class HelloValidateResourceTest extends Specification {
@Shared
def jerseyTest = new JerseyTest() {
@Override
protected Application configure() {
return new ResourceConfig(HelloValidateResource.class)
}
}
def setupSpec() {
jerseyTest.setUp()
}
def cleanupSpec() {
jerseyTest.tearDown()
}
def "test index"() {
when:
def response = jerseyTest.target("/").queryParam("name", "World").request().get()
then:
response.getStatus() == 200
response.readEntity(String.class) == "Hello World"
}
def "test fail"() {
when:
def response = jerseyTest.target("/").request().get()
then:
response.getStatus() != 200
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment