Created
July 4, 2016 15:11
-
-
Save takaki/c0f7e3561719fb2f84c2b172d22d70f3 to your computer and use it in GitHub Desktop.
JAX-RSのBean Validationの基礎 ref: http://qiita.com/takaki@github/items/40f8bc02dc06a0e2848b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compile group: 'org.glassfish.jersey.ext', name: 'jersey-bean-validation', version: '2.23.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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