本教程涵盖了通过HelloWorld示例对Java代码,Web服务,Servlet,SCA和EJB bean进行单元和组件测试(开发HelloWorld程序被认为是学习新概念的最简单方法)。 在这里,您可以使用JUnit和Jakarta Cactus测试框架执行单元和组件测试。
完成本教程后,您应该了解如何执行单元和组件测试,以及如何使用IBM Rational Application Developer,IBM Rational Software Developer或IBM WebSphere Integration Developer使用Apache Ant自动化这些测试。
要继续学习本教程,您应该具有要测试的技术的基本知识,无论它是SCA,Servlet,EJB Bean,Web服务还是Java代码。 您还应该对系统要求部分中列出的产品具有基本的技术经验。
在本教程中,您可以使用以下任何工具来测试Java代码,Web服务,Servlet和EJB Bean:
Rational Software Architect V6.0 Rational Application Developer V6.0要执行SCA测试以及Java,Web服务,servlet和EJB测试,您需要WebSphere Integration Developer V6.0.1或6.0.2。
运行组件测试需要IBM Rational Agent Controller,它是Rational Software Architect捆绑包中的一个附加组件。
在任何软件开发项目中,测试都是至关重要的。 它模拟了几种执行路径,以交付无缺陷且经过良好测试的产品,从而确保其健壮性和高质量给客户。 忽略软件测试(由于交货日期紧迫,计划依赖发布修复程序或其他原因),增加了修复以后出现的错误的成本。 这是因为随着软件开发周期的继续,错误修复成本急剧增加。
在软件开发周期内执行四种主要的测试类型或阶段,如图1所示:
单元测试是白盒测试,用于测试基本代码单元,包括Java类,Web服务,EJB Bean和Servlet。 组件测试会测试基本代码单元的集成,以在将某些基本功能集成到生产代码库中之前执行某些功能。 系统测试将测试整个系统,并确保将一组组件集成在一起。 验收测试测试软件产品是否满足客户的功能和非功能要求。注意金字塔状的结构,它强调了测试顺序的重要性。 例如,在对每个代码单元执行单元测试之后,您将执行组件测试以确保这些代码单元的集成的有效性。
JUnit是由Erich Gamma和Kent Beck编写的Java回归测试框架。 如Apache网站上所述, Cactus是一个简单的测试框架,用于对服务器端Java代码(例如servlet,EJB Bean,标记库和过滤器)进行单元测试。 扩展JUnit的Cactus旨在降低编写服务器端代码的测试成本,并且实现了容器内策略,这意味着测试在容器服务器内部执行(请参阅参考资料 ,以获取更多信息的链接)关于仙人掌)。 在本教程中,您将使用JUnit和Cactus测试框架来演示单元和组件测试。 本教程末尾的“ 下载”部分提供了本教程中说明的所有测试。
本节介绍了本教程中所需的环境准备工作。
如果您使用的是Rational Software Architect,Rational Application Developer或WebSphere Integration Developer,则需要启用Web服务开发和测试功能:
选择窗口>首选项>工作台>功能 。 选中右窗格中的Web Service Developer ,如图2所示。使用JUnit框架进行测试的项目必须在其类路径中包含junit.jar,该路径位于<IDE_HOME> \ eclipse \ plugins \ org.junit_3.8.1 \ junit.jar中。 <IDE_HOME>是Rational Application Developer,Rational Software Developer或WebSphere Integration Developer的安装目录。 例如,如果您使用的是WebSphere Integration Developer,并且其安装目录位于C:\ Program Files \ IBM \ WebSphere \ 601,则junit.jar应该位于C:\ Program Files \ IBM \ WebSphere \ 601 \ eclipse \ plugins \ org.junit_3.8.1。 要将junit.jar添加到项目类路径:
右键单击项目,然后选择属性 。 从左窗格中选择“ Java构建路径 ”。 转到“ 库”选项卡,然后单击“ 添加外部JAR” 。 浏览至junit.jar ,如图3所示。要将Cactus框架用于动态Web项目中的服务器端单元测试,必须遵循以下步骤:
将以下.jar文件添加到WebContent \ WEB-INF \ lib: Aspectjrt-1.2.1.jar 仙人掌1.7.2.jar commons-httpclient-2.0.2.jar commons-logging-1.0.4.jar junit.jar注意: Cactus扩展了JUnit,因此必须包含junit.jar。 (可从“ 下载”部分或从apache.org获得Cactus .jar文件。)
现在,您需要添加对于在服务器端调用测试方法至关重要的Cactus servlet(通过ServletTestRedirector servlet),并添加运行测试所必需的servlet(通过ServletTestRunner servlet)。 要添加servlet:
打开动态Web项目的web.xml文件。 单击Source选项卡,并添加以下两个servlet,如清单1所示: Servlet名称 : ServletRedirector , Servlet类 : org.apache.cactus.server.ServletTestRedirector 。 Servlet名称 : ServletTestRunner , Servlet类 : org.apache.cactus.server.runner.ServletTestRunner 。本节涵盖以下类型的单元测试:
使用JUnit框架进行Java和Web服务单元测试 使用Cactus框架的Servlet和EJB单元测试以下步骤显示了要测试的Java类:
导入HelloWorldJava.zip (可在“ 下载”部分中获得)作为项目交换,或创建一个名为HelloWorldJava的Java项目。 在com.ibm.tdc包下创建一个名为HelloWorld的类。 将清单2中的代码粘贴到HelloWorld类中。测试用例是针对特定目标开发的一组测试输入,执行条件和预期结果。 要为某个Java类创建JUnit测试用例,请遵循以下步骤:
切换到Java透视图。 右键单击HelloWorld.java ,然后选择新建> JUnit测试用例 。 出现一个消息框,用于将junit.jar添加到构建路径,如图4所示。在清单3中,请注意以下几点:
public static void main(String[] args)是可选的,但用于从命令行运行JUnit测试用例。 TestRunner用于执行测试用例。 textui用于在文本模式下运行测试用例,因此测试结果显示在控制台中。 setUp()是TestCase类的方法之一。 它在每个测试方法之前执行,而tearDown()在每个测试方法之后执行。 setUp()和tearDown()都是可选方法。 在清单3中, setUp()用于创建被测类的新实例。 testGetGreeting()测试getGreeting()方法。 JUnit命名约定规定测试方法必须具有前缀test且不带参数,也就是说,它应遵循此签名public void testXXX() [throws ...]因为JUnit TestRunner使用反射来自动运行测试方法。 一个测试用例中必须至少有一种测试方法,否则运行JUnit测试用例会导致失败。 assertEquals()用于比较测试结果和期望值。 如果预期结果与实际结果相等,则断言成功,否则,则断言失败。 JUnit提供了不同版本的断言以支持各种输出检查。 (请参阅相关信息的链接,有关断言类的更多信息。) 图6的序列图中说明了JUnit测试用例的执行。您可以使用以下两种方法之一运行JUnit测试用例:
右键单击HelloWorldTest ,然后选择运行> JUnit测试 。 出现绿色条,如图7所示,指示测试用例成功。 (红色条表示测试用例失败或错误。)一个测试套件用于在一个套件中收集多个测试用例。 因此,只需单击一下即可运行许多测试用例。
右键单击测试用例的软件包com.ibm.tdc ,然后选择New> Other> Java> JUnit> JUnit Test Suite 。 单击下一步 。 选择要包括在测试套件中的测试用例,如图9所示,然后单击Finish 。以下代码是自动生成的:
在这种情况下, addTestSuite()用于添加测试用例类。
运行测试套件与运行JUnit测试用例完全相同。 请注意, TestRunner寻找public static Test suite()来运行测试套件。
Web服务代理是从Web服务描述语言(WSDL)文件生成的一组类,这些类通过在简单Java代码中隐藏查找和调用详细信息来简化客户端开发。
您可以通过从Web服务生成代理来使用JUnit测试Web服务。 针对代理运行JUnit测试用例,该代理在后台调用Web服务。 成功测试代理意味着成功测试Web服务。
要创建受测试的Web服务,请将HelloWebService.zip (在“ 下载”部分中提供)导入为项目交换。 该Web服务只有一个操作getGreeting() ,它用作先前测试过的Java项目中的HelloWorld类。 清单5显示了Web服务实现。
以下步骤将引导您创建一个包含Web服务客户端类和Web服务单元测试类的Java项目:
创建一个名为HelloWebServiceTest的Java项目,以表示Web服务测试项目。 要生成Web服务客户端(即代理类),请在HelloWebServiceTest Java项目下创建一个名为wsdl的文件夹。 将HelloService.wsdl文件(从HelloWebService项目检索)粘贴到wsdl文件夹中。 右键单击HelloService.wsdl ,然后选择Web Services> Generate Client ,如图10所示。注意: testGetGreeting()引发RemoteException因为getGreeting()引发此异常。
执行以下步骤之一来运行Web服务测试以及不同的失败和错误情形:
请参阅“ 运行JUnit测试用例”部分以运行测试用例。 根据清单6中的testGetGreeting()代码,如果HelloWebService关闭(例如,未部署在服务器上),则JUnit框架将捕获远程异常,并生成带有该异常的完整堆栈跟踪的错误-这对于调试非常有用—如图12所示。请注意,在这种情况下,该错误强调了一个无法预料的问题,该问题表明发生了未捕获的异常(远程异常)。
另一种方法是捕获RemoteException ,如清单7所示。在清单7中,可以预见到RemoteException调用,并且使用fail()不会生成完整的堆栈跟踪,包括引发异常的方法。 但它确实会生成随附的消息。
如果HelloWebService关闭,则执行fail()并发生故障,如图13所示。
请注意,失败强调发生了预期的问题。
本节探讨HelloWorld servlet单元测试。 您使用Cactus测试框架,该框架扩展了JUnit测试框架以执行容器内测试。
以下步骤将引导您创建一个简单的servlet并进行测试。
导入HelloWorld.zip (在“ 下载”部分中可用)作为项目交换。 或创建一个名为HelloWorld的动态Web项目,并创建一个名为HelloWorldServlet的servlet。 将清单8中的代码粘贴到HelloWorldServlet中,该代码如下: 从请求参数获取名称。 设置会话问候。 将问候语写到响应中。如果您通过编写http://localhost:9080/HelloWorld/HelloWorldServlet?name=rosa在浏览器上测试了该servlet,则结果应如图14所示。
请按照以下步骤创建HelloWorldServletTest类,该类扩展了Cactus ServletTestCase类,并允许您测试HelloWorldServlet 。
执行仙人掌测试准备部分中提到的步骤。 切换到Java透视图。 右键单击HelloWorldServlet.java ,然后选择新建>其他> Java> JUnit> JUnit测试用例 。 在“ JUnit测试用例”对话框中,选择org.apache.cactus.ServletTestCase作为超类,然后单击“ 下一步” 。 选择getGreeting方法,然后单击Finish ,如图15所示。总体代码应类似于清单11所示。
以下步骤显示了如何运行HelloWorldServletTest类。
右键单击HelloWorldServletTest测试类,然后选择Run> Run 。 在“配置”列表中选择“ JUnit ”,然后单击“ 新建” 。 单击右窗格中的“ 参数”选项卡。 在VM arguments字段中将Cactus上下文URL设置为-Dcactus.contextURL=http://localhost:9080/HelloWorld ,如图16所示。请注意,HelloWorld是项目名称。 cactus.contextURL表示测试应用程序在其下运行的应用程序上下文。本节说明如何创建HelloWorld无状态会话EJB Bean,以及如何使用仙人掌测试框架执行单元测试。
请按照以下步骤创建一个HelloWorld无状态会话EJB bean:
要创建要测试的无状态会话EJB Bean, 请将HelloEJB.zip (在“ 下载”部分中提供)导入为项目交换。 或选择“ 文件”>“新建”>“项目”>“ EJB”>“ EJB项目” ,输入HelloEJB作为项目名称,然后单击“ 完成” 。 右键单击HelloEJB ,选择New> Other> EJB> Enterprise Bean ,然后单击Next 。 输入图17中所示的值,在其中选择Session bean作为EJB类型并定义bean的基本属性。 然后单击“ 下一步” 。HelloEJB的测试在您先前在Servlet单元测试部分中创建的HelloWorld动态Web项目中进行 。
打开HelloWorldEAR的部署描述符。 转到Module选项卡,并将HelloEJBClient.jar添加到Project Utility Jars列表中,如图20所示。请参阅“ 运行servlet测试”部分以运行HelloEJBTest 。
组件测试在将基本代码单元集成到生产代码库中之前测试它们的集成,以执行某些功能。 本节将引导您使用WebSphere Integration Developer,Rational Software Architect和Rational Application Developer中的组件测试功能来对Java代码和Web服务执行自动化的组件测试。 自动化的组件测试功能扩展了JUnit框架。
有一些要求:运行组件测试之前,需要安装Rational Agent Controller。 为了测试Java代码和Web服务组件,您必须首先创建一个组件测试项目(在WebSphere Integration Developer,Rational Software Architect或Rational Application Developer中),该项目代表了测试组件的容器(参见图22)。
执行以下步骤来创建组件测试项目,在其中将创建组件测试时生成的测试工件分组:
导入CompTest.zip (可在“ 下载”部分中获得)作为项目交换。 或通过选择File> New> Project> Component Test> Component Test Project创建一个组件测试项目 ,然后单击Next ,如图23所示。组件测试是在将代码的基本单元集成到生产代码库中之前对其进行测试以执行特定功能的基本代码单元的集成的能力。 假设您在Java单元测试部分中创建的HelloWorldJava项目具有另一个名为GoodMorning类。 此类具有一个方法sayGoodMorning() ,该方法采用Hello! name格式的字符串Hello! name Hello! name ,对其进行解析以检索该名称,然后返回Good Morning name 。 该类已通过单元测试,成为HelloWorld类。 现在,您想将这两个类集成在一起,以形成一个使用name并返回Good Morning name的特定场景。 这是如何做:
选择文件>新建>其他>组件测试> Java> Java组件测试 ,然后单击下一步 。 选择将与新Java组件测试相关联的CompTest组件测试项目。 当出现“选择要测试的组件”对话框时,选择HelloWorld和GoodMorning类(要测试的类)。 输入HelloFlow作为测试名称,如图25所示,然后单击Next 。本节通过测试HelloWebService讨论Web服务组件测试。 要在先前创建的CompTest中创建Web服务组件测试(在“ 创建组件测试项目”部分中),请执行以下操作:
选择“ 文件”>“新建”>“其他”>“组件测试”>“ Web服务”>“ Web Service组件测试” ,然后单击“ 下一步” 。 选择将与新的Web服务组件测试相关联的CompTest组件测试项目,然后单击Next 。 在“选择要测试的Web服务”对话框中,浏览到HelloService.wsdl ,然后选择要测试的端口类型,如图31所示。此对话框负责根据所选的WSDL文件生成代理类。<soap:address>标记内容是Web服务端点,它表示在其上部署和运行Web服务的网络物理地址。 (请参阅相关主题有关肥皂的更多信息:地址)
观察“测试数据比较器”视图中的“实际值与预期值”列。 图36显示测试成功通过,并且实际结果和预期结果匹配。 (这由“实际”列中结果的绿色表示;如果实际结果和预期结果之间不匹配,则结果在“实际”列中显示为红色)。SCA是一种新的编程模型,专门用于在面向服务的体系结构(SOA)中构建和组装业务解决方案,以及用于集成和组合服务。 (有关更多信息,请参阅“ 使用服务组件体系结构构建SOA解决方案 ” [developerWorks,2005年10月)。)
您可以使用ServletTestCase执行SCA组件测试。 此测试用例使用Cactus框架通过独立引用从外部调用SCA组件。 在本节中,您将创建一个具有SCA组件的HelloWorldModule模块,这些SCA组件已连接到独立引用。 然后,创建一个名为HelloWorldModuleTest的动态Web项目,以测试SCA组件。
执行以下步骤来创建和测试SCA模块:
导入HelloWorldModule.zip (在“ 下载”部分中提供)作为项目交换。 或通过单击文件>新建>项目>模块来创建模块 。 然后单击“ 下一步” 。 输入HelloWorldModule作为项目名称。 在“ 业务集成”透视图中,导航到并右键单击HelloWorldModule>接口 。 选择“ 新建”>“接口” ,并将接口命名为HelloInterface 。 接口编辑器打开后,添加getGreeting操作,该操作将名称作为输入,并将greeting作为输出,如图37所示。如您在图38中看到的, Invoke用于调用HelloJava ,而Snippet用于更改问候语的值。
拖放独立引用 ,并将独立引用连接到HelloJava和HelloJavaProcess ,如图39所示。以下步骤引导您完成创建一个动态Web项目HelloWorldModuleTest,该项目将测试HelloWorldModule:
切换到J2EE透视图,然后在Project Explorer视图中展开Enterprise Applications 。 右键单击HelloWorldModuleApp ,然后选择新建>动态Web项目 。 在“新建动态Web项目”对话框中,输入名称,例如HelloWorldModuleTest ,这是用于测试HelloWorldModuleApp的项目。 点击完成 。 对HelloWorldModuleTest项目执行仙人掌测试准备部分中介绍的步骤。 必须将HelloWorldModuleTest(依赖于HelloWorldModule)添加到HelloWorldModule中。 为此,请切换到“ 业务集成”透视图,选择“ HelloWorldModule” ,然后单击鼠标右键以打开“依赖关系编辑器”。 展开J2EE ,然后单击Add将HelloWorldModuleTest动态Web项目添加到SCA模块,如图40所示。The previous code tests both the HelloJava and HelloProcess SCA components using the testHelloJava() and testHelloProcess() methods, respectively. Each method uses the service manager to access the SCA component through the partner reference name, invokes the getGreeting() operation, then validates the result through assertion.
Refer to the Run the servlet test section to run HelloWorldTest.
Ant is a Java-based tool used for build automation; in this case Ant is used to automate JUnit and Cactus test cases, and generate the test results in XML files (see Related topics for a link to more information about Ant).
The following steps show how to use the JUnit Ant task to run the HelloWorldTest (created earlier). This lets you perform the unit test for the HelloWorld Java class, as explained in the Java unit testing section.
Create an XML file named build.xml , and place it in the Java project. Paste the code from Listing 16 in the build.xml file.In Listing 16, note that:
<pathelement location="bin"/> refers to the place of the .class files (the classes under test and the test cases). <junit printsummary="yes" haltonfailure="no"> runs the JUnit test cases and prints a summary. <formatter type="xml"/> sets the test results format. <batchtest todir="C:/temp/"> specifies the location of the generated test results reports. <fileset dir="C:/Workspace/HelloWoldJava"> refers to the location of the Java project. <include name="**/*Test*.java"> refers to test case with the word Test in it under any package. Note that **/*Test*.java can be replaced by com/ibm/tdc/HelloWorldTest.java . To run Ant, drag and drop the build.xml file in the Ant view. Right-click junitTest , and select Run Ant , as shown in Figure 41.The test results appear in the Console, and the test reports are generated at the location you specified earlier.
Follow these steps to use the Cactus Ant task to run HelloWorldServletTest (created earlier) to perform the unit test for the HelloWorldServlet servlet, as covered in the Servlet unit testing section.
Add cactus-ant-1.7.2.jar and cargo-0.5.jar under WebContent\WEB-INF\lib to run the Cactus Ant task. (You can download these .jar files from the Download section of this article or from apache.org.) Right-click HelloWorldEAR , select Export > EAR file , and choose C:/temp/test/HelloWorldEAR.ear as the destination. Create an XML file named build.xml , and place it in the HelloWorld dynamic Web project. Paste the code from Listing 17 in the build.xml file.In Listing 17, note that:
<property name="server.lib" value="..."/> refers to the place of the server library. <path id="test.classpath"> refers to the j2ee.jar file. <path id="cactus.classpath"> refers to the Cactus .jar files. <taskdef resource="cactus.tasks" classpathref="cactus.classpath"/> defines the Cactus task and refers to the Cactus .jar files. <cactus ... earfile="C:/temp/test/HelloWorldEAR.ear"> refers to the dynamic Web project EAR file that was exported above in step 2. Drag and drop build.xml in the Ant view, then right-click cactusTest > Run Ant . Select the JRE tab, and choose Separate JRE . Select the server on which your dynamic Web project is running. For example, if you're using WebSphere Process Server, choose WPS Server v6.0 JRE . (Note that the WebSphere Process Server test environment is available with WebSphere Integration Developer.) Set the VM arguments to -Dcactus.contextURL=http://localhost:9080/HelloWorld , as shown in Figure 43.In this tutorial, you learned how to perform unit and component testing using the JUnit and Cactus frameworks. You also walked through the automation of tests with Ant scripts using IBM Rational tools.
The author would like to thank Ahmed Abbas, Dr. Alaa Youssef, and Mahmoud Ouda for reviewing the tutorial and for their valuable suggestions.
翻译自: https://www.ibm.com/developerworks/webservices/tutorials/ws-testing/ws-testing.html