简体   繁体   English

从 NetBeans 对 web 服务客户端进行单元测试

[英]Unit testing a web service client from NetBeans

I have a project that contains both the ws client and service (JAX-WS).我有一个包含 ws 客户端和服务(JAX-WS)的项目。 Is there a way of having the ws deploy during automated testing such that I can have my client tests encompass the calls to the service?有没有办法在自动化测试期间部署 ws,以便我可以让我的客户端测试包含对服务的调用?

Obviously this would require deployment to an application server... or is this kind of testing ill-advised?显然这需要部署到应用程序服务器......或者这种测试不明智?

I found a suitable solution to this problem.我找到了解决这个问题的合适方法。 Using embedded Tomcat (7.0.11), I was able to successfully deploy my web-service programmatically during the running of the unit test.使用嵌入式 Tomcat (7.0.11),我能够在单元测试运行期间以编程方式成功部署我的 Web 服务。

tomcat = new Tomcat();
tomcat.setBaseDir(".");
tomcat.setPort(8084);
tomcat.addWebapp("/", System.getProperty("user.dir") + "/build/web");
tomcat.setHostname("localhost");
tomcat.enableNaming();
tomcat.start();

This approach suited me well as I wanted to automate the testing of a web-service client.这种方法很适合我,因为我想自动化 Web 服务客户端的测试。 As Piyush pointed out, soapUI is a perfect way to perform integration testing.正如Piyush所指出的,soapUI 是执行集成测试的完美方式。

Edit编辑

If you are using context.xml you will need to define it like so:如果你使用context.xml你需要像这样定义它:

Context ctx = tomcat.addWebapp("/", System.getProperty("user.dir") + "/build/web");
File contextFile = new File("META-INF/context.xml");
ctx.setConfigFile(contextFile);
  • This is a good article on unit testing RESTful service.这是一篇关于单元测试 RESTful 服务的好文章 You can also use REST-assured to test REST services and validating the response using Junit in Java.您还可以使用REST-assured测试 REST 服务并使用 Java 中的 Junit 验证响应。
  • For functional testing, you can use SOAPUI .对于功能测试,您可以使用SOAPUI
  • For JAX-WS services, check this article .对于 JAX-WS 服务,请查看这篇 文章 You can also check on the Spring way of testing JAX-WS here .您还可以在此处查看 Spring 测试 JAX-WS 的方式。 You can as well do unit testing from Netbeans by following documentation mentioned here您也可以按照此处提到的文档从 Netbeans 进行单元测试

How are you generating your service?你是如何产生你的服务的? What kind of Web Services are they?它们是什么类型的 Web 服务? Isn't your generated service a regular POJO?您生成的服务不是常规 POJO 吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM