Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestAuthenticate {
- Service service = new Service();
- String username = "user";
- String password = "password";
- String token;
- @Test(expected = Exception.class)
- public final void testAuthenticateInputs() {
- password = "pass";
- service.authenticate(username, password);
- }
- @Test(expected = Exception.class)
- public final void testAuthenticateException(){
- username = null;
- String token = service.authenticate(username, password);
- assertNotNull(token);
- }
- @Test
- public final void testAuthenticateResult() {
- String token = service.authenticate(username, password);
- assertNotNull(token);
- }
- }
- public class MyResourceTest {
- public static final String BASE_URI = "http://localhost:8080/api/"
- private HttpServer server;
- @Before
- public void setUp() throws Exception {
- final ResourceConfig rc = new ResourceConfig(Service.class);
- server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
- }
- @After
- public void tearDown() throws Exception {
- server.stop();
- }
- @Test
- public void testService() {
- Client client = ClientBuilder.newClient();
- WebTarget target = client.target(BASE_URI).path("service");
- ...
- }
- }
- <dependency>
- <groupId>org.glassfish.jersey.containers</groupId>
- <artifactId>jersey-container-grizzly2-http</artifactId>
- <version>${jersey2.version}</version>
- </dependency>
- public class SimpleTest extends JerseyTest {
- @Override
- protected Application configure() {
- return new ResourceConfig(Service.class);
- }
- @Test
- public void test() {
- String hello = target("service").request().get(String.class);
- }
- }
- <dependency>
- <groupId>org.glassfish.jersey.test-framework.providers</groupId>
- <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
- <version>${jersey2.version}</version>
- <scope>test</scope>
- </dependency>
- import com.sun.jersey.api.client.WebResource;
- import com.sun.jersey.api.core.DefaultResourceConfig;
- import com.sun.jersey.spi.container.servlet.WebComponent;
- import com.sun.jersey.test.framework.JerseyTest;
- import com.sun.jersey.test.framework.WebAppDescriptor;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import junit.framework.Assert;
- import org.junit.Test;
- public class SimpleTest extends JerseyTest {
- @Path("service")
- public static class Service {
- @GET
- public String getTest() { return "Hello World!"; }
- }
- public static class AppConfig extends DefaultResourceConfig {
- public AppConfig() {
- super(Service.class);
- }
- }
- @Override
- public WebAppDescriptor configure() {
- return new WebAppDescriptor.Builder()
- .initParam(WebComponent.RESOURCE_CONFIG_CLASS,
- AppConfig.class.getName())
- .build();
- }
- @Test
- public void doTest() {
- WebResource resource = resource().path("service");
- String result = resource.get(String.class);
- Assert.assertEquals("Hello World!", result);
- System.out.println(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement