Advertisement
Guest User

Untitled

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