Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 35 36 37 38 39 40 < 41 > 42 43 44 45 46 47 .. 166 >> Next

The lifecycle build script shown above highlights Ant's ability to ease the construction and deployment of Java projects by automating source code packaging, integration, script execution, and production system deployment. Hopefully, users can use these Ant build techniques to make their lifecycle activities more efficient.
NOTE
100 Item 12
Item 12: JUnit: Unit Testing Made Simple
My wife is a good cook, a really good cook, but she drives me crazy sometimes with experiments on existing recipes that I like and expect to taste a certain way. Sometimes she does this because she's run out of some ingredient or she just feels like trying something new. Me, I don't like to try new things and prefer consistency in what I eat. My wife has made several suggestions on what I should do about this problem, the cleanest version being that I cook for myself. Rather than taking a chance that I might actually learn how to cook, I've graciously learned to accept her unpredictable ways.
Software testing exhibits these same qualities and inconsistencies. Many times developers don't write proper tests because they feel that they don't have the time to do so, and when they do, they often introduce their own programming biases in their manual tests, making them irrelevant. Software testers often don't understand what they are testing because developers don't communicate very well what exactly needs to be tested. Growing code repositories and deadline pressures complicate this matter, as well as the attrition of developers on many projects.
To address this problem, development teams need to enforce some order into their systems to prevent the chaos that habitually occurs, and that can be accomplished by implementing the JUnit framework to test logic boundaries and to ensure that the overall logic of your software components is correct.
JUnit is an open-source unit-testing framework written in Java that allows users to create individual test cases that are aggregated in test suites for deployment. Lines 16 and 17 illustrate this. The test case for my unit test is called OneTestCase.class, and my test suite is "test". If a test class does not define a suite method, then the TestRunner application will extract a suite and fill it with all methods that start with "test" using the Java reflection mechanism.
01
02 import junit.framework.*;
03 import junit.runner.BaseTestRunner;
04
05 /**
06 * TestSuite that runs all the sample tests
07 *
08 */
09 public class AllTests {
10
11 public static void main(String[] args) {
12 junit.textui.TestRunner.run(suite());
13 }
14
15 public static Test suite() {
16 testsuite suite» new testsuiteCFramework Tests");
17 suite.addTestSuite(OneTestCase.class);
18 return suite;
19 }
20 }
Listing 12.1 AllTests.java
JUnit: Unit Testing Made Simple 101
Since we develop Web applications on many of our projects, our unit tests focus primarily on JavaBean components to ensure that our program logic is sound and consistent. In the code below, an employee form validates user inputs from a Web page. Lines 59 to 71 demonstrate how to add JUnit test code to our JavaBean applications for unit testing.
01
02 import java.util.*;
03 import junit.framework.*;
04 import junit.runner.BaseTestRunner;
05
06 public class employeeFormBean {
07 private String firstName;
08 private String lastName;
09 private String phone;
10 private Hashtable errors;
11
12 public boolean validate() {
13 boolean allOk=true;
14
15 if (firstName.equals("")) {
16 errors.put("firstName" ,"Please enter your first name");
17 firstName="";
18 allOk=false;
19 }
20 if (lastName.equals("")) {
21 errors.put("lastName", "Please enter your last name");
22 lastName="";
23 allOk=false;
24 }
25 return allOk;
26 }
27
28 public String getErrorMsg(String s) {
29 String errorMsg =(String)errors.get(s.trim());
30 return (errorMsg == null) ? ":errorMsg;
31 }
32
33 public employeeFormBean() {
34 firstName="";
35 lastName="";
36 // errors
37 errors = new Hashtable();
38 }
39
40 // GET methods -----------------
41 public String getFirstName() {
42 return firstName;
43 }
Listing 12.2 employeeFormBean.java (continued)
102 Item 12
44
45 public String getLastName() {
46 return lastName;
47 }
48
49 // SET methods ---------------------------------------------------
50 public void setFirstName(String fname) {
51 firstName =fname;
52 }
53
54 public void setLastName(String lname) {
55 lastName =lname;
56 }
57
58 /* main */
59 public static void main(String[] args) {
60 junit.textui.TestRunner.run(suite());
61 }
62
63 /**
64 * TestSuite that runs all the sample tests
65 *
66 */
67 public static Test suite() {
68 TestSuite suite= new TestSuite("Employee Form Unit Tests");
69 suite.addTestSuite(employeeFormBeanTestCase.class);
70 return suite;
71 }
73 }
Listing 12.2 (continued)
Listing 12.3 illustrates how a test case should be written to test the JavaBean that was created. Notice the assert statements that check the getter/setter methods in the employ-eeFormBean component. These asserts ensure that expected behavior is maintained in our code and that bugs that might be introduced to this application are captured and resolved easily. One thing to be aware of when you do create unit tests with JUnit is that if you insert System.out.println() statements in your test cases, it does not print the text output to the console when you run your test from the command line.
Previous << 1 .. 35 36 37 38 39 40 < 41 > 42 43 44 45 46 47 .. 166 >> Next