Advertisement
Guest User

Untitled

a guest
Jun 8th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. public class TestAuthenticate {
  2. Service service = new Service();
  3. String username = "user";
  4. String password = "password";
  5. String token;
  6.  
  7. @Test(expected = Exception.class)
  8. public final void testAuthenticateInputs() {
  9. password = "pass";
  10. service.authenticate(username, password);
  11. }
  12.  
  13. @Test(expected = Exception.class)
  14. public final void testAuthenticateException(){
  15. username = null;
  16. String token = service.authenticate(username, password);
  17. assertNotNull(token);
  18. }
  19.  
  20. @Test
  21. public final void testAuthenticateResult() {
  22. String token = service.authenticate(username, password);
  23. assertNotNull(token);
  24. }
  25.  
  26. public class MyResourceTest {
  27.  
  28. public static final String BASE_URI = "http://localhost:8080/api/"
  29. private HttpServer server;
  30.  
  31. @Before
  32. public void setUp() throws Exception {
  33. final ResourceConfig rc = new ResourceConfig(Service.class);
  34. server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
  35. }
  36.  
  37. @After
  38. public void tearDown() throws Exception {
  39. server.stop();
  40. }
  41.  
  42. @Test
  43. public void testService() {
  44. Client client = ClientBuilder.newClient();
  45. WebTarget target = client.target(BASE_URI).path("service");
  46. ...
  47. }
  48. }
  49.  
  50. <dependency>
  51. <groupId>org.glassfish.jersey.containers</groupId>
  52. <artifactId>jersey-container-grizzly2-http</artifactId>
  53. <version>${jersey2.version}</version>
  54. </dependency>
  55.  
  56. public class SimpleTest extends JerseyTest {
  57.  
  58. @Override
  59. protected Application configure() {
  60. return new ResourceConfig(Service.class);
  61. }
  62.  
  63. @Test
  64. public void test() {
  65. String hello = target("service").request().get(String.class);
  66. }
  67. }
  68.  
  69. <dependency>
  70. <groupId>org.glassfish.jersey.test-framework.providers</groupId>
  71. <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
  72. <version>${jersey2.version}</version>
  73. <scope>test</scope>
  74. </dependency>
  75.  
  76. import com.sun.jersey.api.client.WebResource;
  77. import com.sun.jersey.api.core.DefaultResourceConfig;
  78. import com.sun.jersey.spi.container.servlet.WebComponent;
  79. import com.sun.jersey.test.framework.JerseyTest;
  80. import com.sun.jersey.test.framework.WebAppDescriptor;
  81. import javax.ws.rs.GET;
  82. import javax.ws.rs.Path;
  83. import junit.framework.Assert;
  84. import org.junit.Test;
  85.  
  86. public class SimpleTest extends JerseyTest {
  87.  
  88. @Path("service")
  89. public static class Service {
  90. @GET
  91. public String getTest() { return "Hello World!"; }
  92. }
  93.  
  94. public static class AppConfig extends DefaultResourceConfig {
  95. public AppConfig() {
  96. super(Service.class);
  97. }
  98. }
  99.  
  100. @Override
  101. public WebAppDescriptor configure() {
  102. return new WebAppDescriptor.Builder()
  103. .initParam(WebComponent.RESOURCE_CONFIG_CLASS,
  104. AppConfig.class.getName())
  105. .build();
  106. }
  107.  
  108. @Test
  109. public void doTest() {
  110. WebResource resource = resource().path("service");
  111. String result = resource.get(String.class);
  112. Assert.assertEquals("Hello World!", result);
  113. System.out.println(result);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement