我在工作中编写了一个 Web 服务,现在我想编写一些测试。我不确定如何测试它们,但我知道我需要哪些工具:JUnit 4,也许还有一个嵌入式应用程序容器,例如 Tomcat,以及 Jersey Client。不过,这是我的方法:
创建一个测试类,在其中启动嵌入式应用程序容器 (Tomcat)。然后,将 Web 服务部署到它并使用 Jersey Client 进行测试调用。这是我到目前为止所做的方式。问题来了:要部署 Web 服务,我必须事先创建 WAR。这是一个问题,因为在使用 Maven 构建应用程序时,首先执行测试,然后构建 WAR;我认为这实际上是一个循环。是否可以在不需要 war 的情况下部署我的 webapp?这是我的测试类:
public class TestAutenticacionService { private final String mWorkingDir = "C://Users//Asasa//workspace//myWebApp//"; private Tomcat tomcat = null; private WebTarget webTarget = null; private ObjectMapper mapper = new ObjectMapper(); private Long idClienteCorrecto = 787538L; @Before public void setUp() throws Exception { tomcat = new Tomcat(); tomcat.setPort(9090); tomcat.setBaseDir(mWorkingDir); tomcat.getHost().setAppBase(mWorkingDir); tomcat.getHost().setAutoDeploy(true); tomcat.getHost().setDeployOnStartup(true); tomcat.getServer().addLifecycleListener(new VersionLoggerListener()); tomcat.getHost().addLifecycleListener(new HostConfig()); try { tomcat.addWebapp("/bidegiWsCli", "C:/Users/Asasa/workspace/myWebApp/myWebApp.war"); tomcat.start(); System.out.println("Tomcat iniciado en " + tomcat.getHost()); webTarget = buildClientWs(); } catch (LifecycleException e) { System.err.println("Tomcat no se pudo iniciar."); e.printStackTrace(); } } @After public void tearDown() throws Exception { if(tomcat != null){ try { tomcat.stop(); System.out.println("Tomcat parado."); } catch (LifecycleException e) { System.err.println("Error al intentar parar Tomcat."); e.printStackTrace(); } } } @Test public void test() { WebTarget loginTgt = webTarget.path("login"); // Probamos un login correcto: WebTarget loginTgtOk = loginTgt.queryParam("Aus", "764577676t").queryParam("Pass", "****"); Response respOk = loginTgtOk.request(MediaType.APPLICATION_JSON).get(); String strLrOk = (String) respOk.readEntity(String.class); try { LoginResponse lrOk = mapper.readValue(strLrOk, LoginResponse.class); Long idClienteOk = lrOk.getIdCliente(); assertEquals(idClienteOk, idClienteCorrecto); } catch (Exception e) { try { mapper.readValue(strLrOk, ExceptionResponse.class); assertTrue(true); } catch (JsonParseException e1) { System.err.println("Error JsonParseException: " + e1.getMessage()); e1.printStackTrace(); } catch (JsonMappingException e1) { System.err.println("Error JsonMappingException: " + e1.getMessage()); e1.printStackTrace(); } catch (IOException e1) { System.err.println("Error IOException: " + e1.getMessage()); e1.printStackTrace(); } } // Probamos un login incorrecto: WebTarget loginTgtWrong = loginTgt.queryParam("Aus", "764577676t").queryParam("Password", "***"); Response respWrong = loginTgtWrong.request(MediaType.APPLICATION_JSON).get(); String strLrWrong = (String) respWrong.readEntity(String.class); try { LoginResponse lrWrong = mapper.readValue(strLrWrong, LoginResponse.class); Long idClienteWrong = lrWrong.getIdCliente(); assertNotEquals(idClienteWrong, idClienteCorrecto); } catch (Exception e) { try { mapper.readValue(strLrWrong, ExceptionResponse.class); assertTrue(true); } catch (JsonParseException e1) { System.err.println("Error JsonParseException: " + e1.getMessage()); e1.printStackTrace(); } catch (JsonMappingException e1) { System.err.println("Error JsonMappingException: " + e1.getMessage()); e1.printStackTrace(); } catch (IOException e1) { System.err.println("Error IOException: " + e1.getMessage()); e1.printStackTrace(); } } } private WebTarget buildClientWs(){ Client client = ClientBuilder.newClient(); return client.target("http://localhost:9090").path("myWebApp").path("resources").path("Auth"); } }
另一种方法是直接执行 Web 服务方法,而不需要应用程序容器。我不确定这一点,因为我不会测试 Web 服务本身。
对此有什么想法吗?
提前致谢
最佳答案
如果您使用的是 Jersey,那么您可以使用内置的测试框架。
参见 https://jersey.java.net/documentation/latest/test-framework.html
关于java - 如何在 Java 中测试 Restful Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37879454/