Lab 7b) test a web application

1,282 views
1,204 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,282
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lab 7b) test a web application

  1. 1. V4.1 Instructor Exercises Guide EXempty Exercise 6. Test a Web Application What this exercise is about In this lab, you will create and execute component tests on a Web application. The first test case focuses on the Java classes within the application. The second test case treats the entire Web application as one component. You will record and play back HTTP messages to test the Web application’s behavior. What you should be able to do At the end of the lab, you should be able to: • Create a test project and test suite to hold test artifacts • Create a JUnitTest • Capture HTTP messages using HTTP Recording • Configure a URL Test Suite to test Web application components • Execute a URL Test Suite against a Web application Introduction The automated component test features available in the IBM Rational Developer (V7 and later) products help you find defects easily, improving the overall quality of Java and J2EE applications. The component test capabilities provide an end-to-end test solution by allowing you to create, edit, run, and view results for tests. Tests also facilitate the reuse of test suites. This feature allows tests to be used throughout the development process. This exercise highlights how to use the automated component testing features available with Rational Application Developer. Following the library case study, you will create two types of tests: a Java application test and a URL application test. In the Java test case, you will test the model classes in the LibraryUtility Java application. Examine the behavior of the item and patron objects as you perform routine operations. The second test examines the LibraryWeb Web application as a whole. Record and playback HTTP messages to determine whether the application is functioning properly. © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-1 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  2. 2. Instructor Exercises Guide Requirements Before starting this lab, you must complete the library database set up from Exercise 1. Develop a Java Application. You must also set up the LibraryWeb Web project according to the instructions in Exercise 3. This exercise requires several code fragments and files. Make sure that these files appear in the <LAB_FILES>Test directory before starting the lab. Warning This lab is very sensitive to firewall, proxy, and intrusion detection software. Instructor exercise overview This lab focuses on two types of test: Java and URL test. Developers should use Java component tests to exercise Java applications and Java classes within a project. As a Java application itself, Java tests do not need to run in an application server. As such, the library application itself must switch from using the JDBC data source to direct JDBC calls in the first test suite. Students modify the properties file to alter the behavior of the DriverManager class. The library application was designed to support a range of data access methods. The second type of test treats the entire Web application as one component. URL test cases replay recorded HTTP messages. Its support will appear more rudimentary than the other types of component tests. Students need to work through a series of steps to configure a URL application test. Unlike the Java application test, there is no view to determine whether a test case passed or failed, except for checking the HTTP status code. Make sure that students stop the integrated test environment before running the Java component test. The library database does not allow concurrent access; this is the behavior of the Cloudscape database as configured through the steps in the Lab Set Up guide. The test case will not be able to access the library database and it will fail. If derby.jar is not added to LibraryTest project’s build path, the students will get a NullPointer exception when they try to access the database - as a connection cannot be created. 6-2 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  3. 3. V4.1 Instructor Exercises Guide EXempty Exercise instructions Preface The lab exercises in this course are designed to run in sequence. If you have skipped one of the previous labs, you must follow the steps in Appendix A before starting this lab. The following references in the exercise instructions represent directory locations in your workstation: • <LAB_FILES>: C:LabFiles70 • <SDP_DIR>: c:IBMSDP70 © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-3 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  4. 4. Instructor Exercises Guide 6.1. Create a Test Project Before you can create a component test case, you need a Java project to hold the test cases and test run results. Enable the component test capability to reveal the Test wizard and perspective. Run the component test project wizard to create a new test project. __ 1. Switch to the Test perspective. __ a. Select Window Open Perspective Other Test from the main menu. (Note that you may have to select the Show all check box in the Open Perspective dialog.) __ b. Click OK to Confirm Enablement. __ 2. Create a Java project named LibraryTest to hold test cases and test run results. __ a. From the main menu, select File New Project. __ b. In the New Project wizard, select Java Project. Click Next. __ c. Type LibraryTest for the Project name. __ d. Click Finish. If prompted, do not switch to the Java perspective. __ 3. Create tests and assets folders in LibraryTest to organize the artifacts you will create in this exercise. __ a. Right-click LibraryTest and select New Folder. Type tests as the Folder name. __ b. Click Finish. __ c. Right-click LibraryTest and select New Folder. Type assets as the Folder name. __ d. Click Finish. __ 4. Add LibraryUtility to the Build Path of LibraryTest. This will allow the tests to access the classes in that project. __ a. Right-click LibraryTest and select Properties Java Build Path. __ b. In the Properties for LibraryTest dialog, select the Projects tab and click Add. __ c. In the Required Project Selection dialog, select LibraryUtility and click OK. 6-4 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  5. 5. V4.1 Instructor Exercises Guide EXempty __ d. Click OK. © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-5 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  6. 6. Instructor Exercises Guide 6.2. Create a JUnit Test Create a JUnit test to exercise the Patron Java class. JUnit tests treat one or more Java classes as a unit of test. __ 5. Create a TPTP JUnit Test in the Test perspective. __ a. Switch to the Test perspective. You can either click Test in the perspective toolbar or select Window Open Perspective Other from the main menu and select Test. __ b. In the Test Navigator, right-click LibraryTest and select New Test Element TPTP JUnit Test. __ c. Click Next. Click Yes if you are prompted to add libraries to the build path. __ d. In the New JUnit Test Definition dialog, enter the following: - Package: com.ibm.library.testing - Name: PatronTest __ e. Click Next. __ f. On the next page, select LibraryTest/tests as the location of the new test. __ g. Click Finish. 6-6 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  7. 7. V4.1 Instructor Exercises Guide EXempty __ 6. Add test methods findByEmail and getAllPatrons to the PatronTest. __ a. In the Test Editor, click the Test Methods tab. __ b. Click Add and type findByEmail for the Name. __ c. Click Add again and type getAllPatrons for the Name. Note With JUnit, the test methods name would usually have the prefix test. However you do not need to prefix the test method names in this tool. __ 7. Add behavior to the Patron Test, to invoke findByEmail and getAllPatrons. __ a. In the Test Editor, click the Behavior tab. __ b. Click Add Loop. Keep the iterations at 1. __ c. Click Add Invocation and double-click findByEmail. __ d. Select Loop1, as the following invocation will be a child of it. __ e. Click Add Invocation and double-click getAllPatrons. © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-7 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  8. 8. Instructor Exercises Guide __ f. Save all your changes. 6-8 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  9. 9. V4.1 Instructor Exercises Guide EXempty 6.3. Edit the PatronTest Test Case The Test wizard created the PatronTest test case. You added two methods to this test case in the test editor: findByEmail and getAllPatrons. You will now implement the methods. __ 8. Open the behavior class(PatronTest) and add a patron field __ a. Press CTRL+SHIFT+R to open the Open Resource dialog. __ b. Type patron in the search field, and double-click PatronTest.java. __ c. Add the following field declaration on the line after the class declaration public class PatronTest extends HyadesTestCase { private Patron patron; __ d. Use Source Organize Imports to import the Patron class. __ 9. Replace the FindByEmail and getAllPatrons methods. You can cut and paste from <LABROOT>/Test/snippets/snippet-1.txt public void findByEmail() throws Exception { patron = Patron.findByEmail("jb@bogus.ibm.com"); assertEquals("Jim", p.getFirstName()); } public void getAllPatrons() throws Exception { Collection<Patron> patrons = null; © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-9 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  10. 10. Instructor Exercises Guide patrons = Patron.getAllPatrons(); assertTrue(patrons.size() == 99); } __ 10. Use Source Organize Imports to import java.util.Collection. Save your changes. 6-10 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  11. 11. V4.1 Instructor Exercises Guide EXempty 6.4. Use a JDBC Connection Instead of a Data Source The classes in LibraryUtility use the DriverManager class to establish connections to a database. However, data sources are not normally available to Java applications. In order to test the database functionalities in a plain Java™ Virtual Machine (JVM), the connection method must be temporarily changed to a JDBC connection. The Library application supports a variety of data access methods. Change the databaseInformation.properties file to switch the access method from a data source to direct JDBC calls. __ 11. Stop the server before continuing, in order to free the lock on the databaseinformation.properties file. __ a. In the Servers view, right-click WebSphere Application Server v6.1 and select Stop. __ 12. Make a backup copy of the existing properties file. __ a. Switch to the J2EE perspective. __ b. In the Project Explorer view, right-click LibraryUtility databaseInformation.properties file and select Refactor Rename. __ c. Change the name to databaseInformationBACKUP.properties. __ d. Click OK. __ 13. Copy the JDBC Connection properties to databaseInformation.properties. __ a. In the Project Explorer view, select databaseInformationJDBC.properties. __ b. Press CTRL+C and then press CTRL+V. __ c. In the Name Conflict dialog, change the name to databaseInformation.properties. __ d. Click OK. __ 14. Add the Derby database JAR, derby.jar, to the project build path of LibraryTest. __ a. Right-click the LibraryTest Java project in the Project Explorer view, and select Properties in the pop-up menu. __ b. In the Properties dialog, select Java Build Path in the left pane, and select the Libraries tab on the right pane. © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-11 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  12. 12. Instructor Exercises Guide __ c. Click Add External JARs, and browse to <SDP_DIR>runtimesbase_v61derbylibderby.jar. __ d. Click OK. 6-12 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  13. 13. V4.1 Instructor Exercises Guide EXempty 6.5. Run the Component Tests At this point, you have a Java test case. The JavaUtility classes no longer depend on the JDBC data source provided by the server to connect to the library database. Run the test cases against the library application and examine the results from the test. Warning Firewalls, proxies, and intrusion detection software can interfere with the communication between Rational Application Developer and IBM® Rational® Agent Controller. Here are some possible issues: • Your computer configuration uses a proxy in your Web browser. Rational Application Developer may indicate compatibility issues. You may need to disable your proxy during this lab. • Your computer configuration uses intrusion detection software. The software may block Rational Application Developer from writing results to the local file system. You may see the following error when you run tests: No location specified. • You create a new IBM® WebSphere® Application Server profile using the Typical profile creation setting in the Profile Management Tool. The server is added as a Windows service. Rational Application Developer will try to start Agent Controller as a service as well. This will fail if you did not, as a separate task, install Agent Controller as a Windows service. __ 15. The LibraryUtility Java project needs direct access to the library database. If you have not already done so, stop the server in order to close the JDBC connection to the database. __ a. From the Servers view, select WebSphere Application Server v6.1. __ b. If the server is running, click the Stop the server button from the view toolbar. __ c. Wait until the server stops and verify that the server Status is Stopped. __ 16. Run the PatronTest. __ a. Switch to the Test perspective. You can either click Test in the perspective toolbar or select Window Open Perspective Other from the main menu and select Test. © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-13 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  14. 14. Instructor Exercises Guide __ b. In the Test Navigator, right-click LibraryTest tests PatronTest and select Run As Test from the main menu. Note You can also select Run As Run and configure some options for your launch configuration. __ 17. Examine the results of the test case execution. __ a. Double-click the LibraryTest PatronTest[Timestamp] test result. __ b. In the Test Log, look at the Verdict field. __ c. Click the Events tab and expand the events. 6-14 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  15. 15. V4.1 Instructor Exercises Guide EXempty __ d. Click on the fail node for getAllPatrons, and notice that the assertion failed because of the wrong number of patrons. Note If both test executions fail, there may be a problem connecting to the database. Usually the error will be a NullPointerException. This is because the persistence layer is unable to create a connection to the database and returns null instead. Several problems could have occurred: • The LibraryUtility databaseInformation.properties file has invalid settings. • The file derby.jar was not added to the LibraryTest Build Path and a ClassNotFoundException was thrown that resulted in a null connection. • The server, the Data perspective, or some other process is connected to the database. If the preceding hints do not resolve the problem, add method breakpoints at the beginning of the findByEmail and getAllPatrons methods. In the Test Navigator, right-click LibraryTest tests PatronTest and select Debug As Test from the main menu. © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-15 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  16. 16. Instructor Exercises Guide 6.6. Restore the Data source Connection When you are done with the component test, you need to restore the connection for DriverManager back to using the data source. __ 18. Delete the existing database properties file. __ a. In the Project Explorer view, right-click the LibraryUtility databaseInformation.properties file and select Delete. __ b. Click Yes to the confirm delete dialog. __ 19. Rename the original database properties file back to its original file name. __ a. Right-click on the databaseInformationBACKUP.properties file and select Refactor Rename. __ b. In the Refactor Rename dialog, enter databaseInformation.properties as the new file name. __ c. Click OK to accept the new name. 6-16 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  17. 17. V4.1 Instructor Exercises Guide EXempty 6.7. Record an URL Test Case The component testing framework in Rational Application Developer also supports Web application testing. In order to test a Web application, you must capture a HTTP request message and play it back under test conditions. In this section, record your interaction with the patron item search servlet through the search.jsp page. Warning If your computer configuration uses a proxy in your Web browser, Rational Application Developer may indicate compatibility issues. You may need to disable your proxy during this lab. However you may then need to switch to communicating to the server via SOAP. To do so, in the Servers view, double-click the test server, and click SOAP in the Server connection type and admin port area. If you computer configuration uses intrusion detection software, it may block Rational Application Developer from writing results to the local file system. You may see the following error when you run tests: No location specified. __ 20. Switch to the Test perspective. __ 21. Create a Recorder Artifact. __ a. In the Test Navigator view, right-click LibraryTest, and select New Test Element Recording HTTP Recording. __ b. In the HTTP Recording dialog, select the LibraryTest tests folder. © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-17 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  18. 18. Instructor Exercises Guide __ c. Change the Recording file name to LibraryHTTPRecording.rec. __ d. Click Finish. This will open an external Web browser. __ 22. Start the server. __ a. Open the Servers view by selecting Window Show View Other from the main menu, and then select Server Servers from the Show View dialog. __ b. In the Servers view, right-click WebSphere Application Server V6.1 and select Start. __ c. Wait until the server status is Started. __ 23. Browse the library project from the external Web browser that opened automatically when you created the recording. Your actions will be recorded by the tool. __ a. In the browser address bar, enter http://localhost:9080/Library/search.jsp. Click Enter. 6-18 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  19. 19. V4.1 Instructor Exercises Guide EXempty __ b. Enter abc@ibm.com in the Search Phrase text box and select the Email radio button. Click Search. __ c. The no result page should display. Click Go Back to the Search Page. __ d. Back on the search page, enter 4 in the Search Phrase and select Patron ID as the Criteria. Click Search. __ e. The list items page should now show up indicating a successful search. __ f. Close the browser. © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-19 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  20. 20. Instructor Exercises Guide __ 24. Customize the HTTP Recording. __ a. LibraryHTTPRecording should automatically be loaded into the editor once the external browser closes. Switch to the Behavior tab of the editor. __ b. Select Loop 1 in the Behavior column. __ c. Enter 10 in the Number of Iterations field. __ d. Save the changes. 6-20 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  21. 21. V4.1 Instructor Exercises Guide EXempty 6.8. Generate Executable URL Test The LibraryHTTPRecording Test Suite stores the HTTP messages captured by the recorder. To playback the captured HTTP messages, generate executable URL tests from the URL test suite. __ 25. Generate a Java class that executes the URL test. __ a. In the Test Navigator view, expand LibraryTest tests. __ b. Right-click LibraryHTTPRecording and select Generate. __ c. In the TPTP URL (JUnit) Test Definition Code Generation dialog, set /LibraryTest as the Source Folder. Click Finish. © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-21 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  22. 22. Instructor Exercises Guide 6.9. Create a Deployment File for the Test Suite The deployment file test artifact specifies which test suite and test cases belong to the HTTP test. Use the test artifact wizard to create a deployment file and associate the HTTP test suite to the file. __ 26. Create a Deployment file for LibraryHTTPRecording. __ a. In the Test Navigator view, right-click LibraryTest New Test Element. Select Test Assets Deployment. Click Next. __ b. In the New Deployment dialog, select LibraryTest Test Suite assets as the parent folder, and enter LibraryHTTPDeploy as the Name. __ c. Click Next. __ d. On the description page, click Next. __ e. In the define artifacts page, click Add. 6-22 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  23. 23. V4.1 Instructor Exercises Guide EXempty __ f. In the Add Artifact Association dialog, select Create a new resource, click Next. © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-23 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  24. 24. Instructor Exercises Guide __ g. Select LibraryTest assets as the parent folder and enter LibraryHTTPArtifact as the Name. Click Next. __ h. In the description page, click Next. 6-24 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  25. 25. V4.1 Instructor Exercises Guide EXempty __ i. In the define test assets page, select LibraryHTTPRecording.testsuite. Click Finish. __ j. Back to the New Deployment dialog, LibraryHTTPArtifact should now be shown in the list. __ k. Click Next. __ 27. Create a new location test artifact to hold test case data associated with the HTTP test suite. __ a. In the define locations page, click Add to bring up the Add Location Association dialog. __ b. In the Add Location Association dialog, select Create new resource, then click Next. © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-25 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  26. 26. Instructor Exercises Guide __ c. In the define file resource page, select LibraryTest assets as the parent folder and enter LibraryHTTPLocation as the Name. __ d. Click Finish. 6-26 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  27. 27. V4.1 Instructor Exercises Guide EXempty __ e. Back in the location page of the New Deployment dialog, LibraryHTTPLocation should now be shown in the list. __ f. Click Next. __ g. In the define mapping page, select LibraryHTTPArtifact on the left and LibraryHTTPLocation on the right, then click the down arrow to add the mapping to the list below. © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-27 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  28. 28. Instructor Exercises Guide __ h. Click Finish. 6-28 Developing Web Applications © Copyright IBM Corp. 2004, 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
  29. 29. V4.1 Instructor Exercises Guide EXempty 6.10. Run the URL Test With all of the HTTP test suite artifacts created, you can now run the URL test suite. Create a launch configuration to specify which URL test case to run. Execute the URL test and examine the results, written to in the LibraryTest project. __ 28. Run the URL test. __ a. Right-click LibraryTest tests LibraryHTTPRecording and select Run As Test. Note You can also select Run As Run and configure some options for your launch configuration. __ 29. Once the URL test finishes, examine the test results. __ a. Double-click LibraryTest LibraryHTTPRecording. __ b. Expand Loop, Page, and invocation to examine individual results. End of exercise © Copyright IBM Corp. 2004, 2007 Exercise 6. Test a Web Application 6-29 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

×