584 WebSphere Studio Application Developer Version 5 Programming Guide JUnit supplied methods The assertEquals, assertTrue, and fail methods are provided by the JUnit framework. JUnit provides a number of methods that can be used to assert conditions and fail a test if the condition is not met. These methods are inherited from the class junit.framework.Assert (see Table 17-1). Table 17-1 JUnit assert methods All of these methods include an optional String parameter that allows the writer of a test to provide a brief explanation of why the test failed—this message is reported along with the failure when the test is executed, for example: assertEquals(String message, object expected, object actual);). Creating a TestSuite A TestSuite is used to run one or more test cases at once. Application Developer contains a simple wizard to create a test suite. Select the itso.junit package and New -> Other -> JUnit -> TestSuite . Alternatively select the project folder and package after you start the wizard. The test classes window shows all test cases in the specified package, selected by default. In our case, BankingTestTest is shown and selected. The first page of the wizard is shown in Figure 17-7. Method name Description assertEquals Assert that two objects or primitives are equal. Compares objects using equals, and compares primitives using ==. assertNotNull Assert that an object is not null assertNull Assert that an object is null assertSame Assert that two objects refer to the same object. Compares using ==. assertTrue Assert that a boolean condition is true fail Fails the test Chapter 17. JUnit and component testing 585 Figure 17-7 Create JUnit TestSuite By default, the test suite is called AllTests. If you had multiple test classes, you could include them in one suite. In our case, we have only one test class. The check boxes to create a method stub for the main method does just that, and we select that here as well as the check box to add a TestRunner statement. The generated AllTests Java source opens, and requires a small modification. You must change the TestRunner statement to the following: public static void main(String[] args) { junit.textui.TestRunner.run( suite() ); } This code uses the text-based test runner tool in the JUnit framework, which runs the tests and reports the results. In our case, using a TestSuite is not required. However, as you add more and more test cases, a TestSuite is more practical. 586 WebSphere Studio Application Developer Version 5 Programming Guide Running the test case There are a couple of ways to run our new test case. Select the BankingTestTest class and Run -> Run As -> JUnit Test from the menu bar. Application Developer opens a JUnit View with the results of the run (Figure 17-8). Figure 17-8 JUnit view A test is considered to be successful if the test method returns normally. A test fails if one of the methods from the Assert class signals a failure. An error indicates that an unexpected exception was raised by the test method, or the setUp or tearDown method was invoked before or after it. The JUnit view is more interesting when an error or failure occurs. Update BankingTestTest so that the testDepositInvalidAccount method causes an error by changing the account ID to a valid one: banking.deposit("104-4001", new BigDecimal(1)); Figure 17-9 shows the JUnit view when the test case is run as a JUnit test again. This time, an error occurs and its details are output in the Failures list. Tip: To run just one test case, select the test case class and Run -> Run As -> JUnit Test . To run all the test cases, run the test suite class. Chapter 17. JUnit and component testing 587 Figure 17-9 Junit view with failure Selecting the testDepositInvalidAccount method in the Failures list updates the Failure Trace window to show the stack trace of the failure. This makes it easy for you to track where the failure occurred. Double-clicking the entry in the Failure Trace list takes you to the specified line in the specified Java source file. Alternatively, the BankingTestTest class can be run as a Java application by selecting Run -> Run As -> Java Application , which executes the main method and uses the TestRunner from the JUnit framework to run and output the test results. Figure 17-10 shows the output from the Banking test case, containing the same update noted above, run as a Java application. We can see that there was one success and one failure. The failure occurred when running testDepositInvalidAccount. Figure 17-10 Output from running BankingTest as Java Application, with failure 588 WebSphere Studio Application Developer Version 5 Programming Guide Each dot (.) in the first line of the output represents the start of a test. We have two tests in our test case, so there are two dots. An “F” indicates a failure, so one test failed. Once all the tests have completed, the test runner shows how long they took to run and a summary of the results. Once we have corrected the error, the output in Figure 17-11 is shown. Figure 17-11 Output from running the test case as Java Application, no failures Testing the Web applications You can also create test cases that run against one of the Web projects, ItsoProGuideBasicWeb or ItsoProGuideStrutsWeb. However, you cannot easily run the test cases when the Web application uses the EJB back-end. Test cases are run as Java applications, and you cannot access EJBs from a Java project. Possible alternatives: You can test the Banking class in the Web projects if you change the flag to use the in-memory objects: boolean ejb = true; You create a J2EE client project as part of the enterprise application for the test cases. J2EE clients can access EJBs when run in a client container. You create test case classes as servlets in the Web project. Component testing The Component Test perspective provides a framework for defining and executing test cases. The basic framework supports three sorts of test case, based on their different scheduler: manual, Java, and HTTP. You can also create report generators to work with the data returned by an executed test case. In order to use component tests, you must have a host with the Agent Controller installed and running. The Agent Controller is not installed by default when Chapter 17. JUnit and component testing 589 installing Application Developer. For more information on the Agent Controller, see “Agent Controller” on page 657. Here we will define Java test cases, which implement the JUnit framework described earlier in the chapter. We will use the project ItsoProGuideJUnit explained in “Preparing for JUnit” on page 578. Change to the Component Test perspective by clicking Window -> Open Perspective -> Other , then selecting Component Test , and click OK . Creating a Java test case In the definition view, right-click Testcases and select New -> Testcase . Select the ItsoProGuideJUnit project, then enter BankingTestCase in the New Testcase wizard, as shown in Figure 17-12. Figure 17-12 New Test case wizard 590 WebSphere Studio Application Developer Version 5 Programming Guide In the next page of the wizard, select Java , then click Finish to complete the wizard. This is illustrated in Figure 17-13. Figure 17-13 Specify attributes of a test case You have now created the BankingTestCase test case. You now see BankingTestCase listed in the Testcases folder of the Definition view. Next, we create a task within the test case that translates into a method in a JUnit test class. In the Outline view, select Main Block . Select New -> Task -> Java from the context menu. In the editor view for BankingTestCase, change the name of the task from T1 to testGetAccount (Figure 17-14). You can also add a description for this task in the Description field, if desired. Chapter 17. JUnit and component testing 591 Figure 17-14 BankingTestCase in the editor view Save the changes. We have now outlined the tasks to be performed in this Java test case. Now we prepare the test case, thus generating a JUnit test class. Preparing a Java test case Before running a Java test case, we must prepare it. This generates all JUnit code. We can then add code to the previously declared tasks, which map to methods in a JUnit test case. In the Definition view, select the BankingTestCase and Prepare (context). The prepare test case wizard opens (Figure 17-15). 592 WebSphere Studio Application Developer Version 5 Programming Guide Figure 17-15 Prepare test case Click Next , then click New Host and the new host dialog opens (Figure 17-16). Figure 17-16 New host dialog Chapter 17. JUnit and component testing 593 Select ItsoProGuideJUnit, enter the host name localhost, and click Finish . Click Finish again to complete the Prepare wizard. Java source files have been generated for this test case. Next we add code to the generated test method. Updating code of Java test case Open the Java perspective, then expand ItsoProGuideJUnit -> comptest.java.bankingtestcase1. Double-click MainBlock.java to open the test case, then add the following code to testGetAccount: try { BankingTest banking = new BankingTest(); Account account = banking.getAccount("104-4001"); } catch (Exception ex) { fail(ex.getMessage()); } This code gets details about an account with account ID 104-4001. If an exception occurs, then the test fails. Otherwise, it passes. You have to add import statements for Banking and Account in your test class. This can be done by right-clicking in the text editor and selecting Source -> Organize Imports . Save the file. Finally, return to the Component Test perspective, where we will run the component test. Note: The ItsoProGuideJUnit project has been modified extensively with new library files. However, the reference to the ItsoProGuideJava project was removed. Open the project properties and select the ItsoProGuideJava project in the Java Build Path Projects page. Note: If you have the automatic build preference turned off, you must build your project before executing it. To build the project, press CTRL-B. Note: For testing of any applications that use database access, make sure that a user name and password to connect to the database is specified within the application’s code. If not, during component testing, the user SYSTEM is used by default. If this user name is not defined or does not have the proper security properties, your tests may fail inexplicably. . (context). The prepare test case wizard opens (Figure 17- 15) . 59 2 WebSphere Studio Application Developer Version 5 Programming Guide Figure 17- 15 Prepare test case Click Next , then click New Host . you add more and more test cases, a TestSuite is more practical. 58 6 WebSphere Studio Application Developer Version 5 Programming Guide Running the test case There are a couple of ways to run our. Output from running BankingTest as Java Application, with failure 58 8 WebSphere Studio Application Developer Version 5 Programming Guide Each dot (.) in the first line of the output represents the