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 .. 36 37 38 39 40 41 < 42 > 43 44 45 46 47 48 .. 166 >> Next

01: import junit.framework.TestCase;
02:
03: public class employeeFormBeanTestCase extends TestCase { 04: public employeeFormBeanTestCase(String name) {
Listing 12.3 employeeFormBeanTestCase.java
JUnit: Unit Testing Made Simple 103
05 super(name);
06 }
07 public void noTestCase() {
08 }
09 public void testCase1() {
10 employeeFormBean eForm = new employeeFormBean();
11 assertTrue(eForm != null);
12 }
13 public void testCase2() {
14 employeeFormBean eForm = new employeeFormBean();
15 assertTrue(eForm != null);
16
17 eForm.setFirstName("Steven")
18 assertTrue("Steven" == eForm .getFirstName());
19 }
20 public void testCase3() {
21 employeeFormBean eForm = new employeeFormBean();
22 assertTrue(eForm != null);
23
24 eForm.setLastName("Fitzgerald");
25 assertTrue("Fitzgerald" == eForm.getLastName());
26 }
27 public void testCase4() {
28 employeeFormBean eForm = new employeeFormBean();
29 assertTrue(eForm != null);
30
31 eForm.setFirstName("John");
32 eForm.setLastName("Walsh");
33 assertTrue(eForm.validate())
34 }
35 public void testCase5() {
36 employeeFormBean eForm = new employeeFormBean();
37 assertTrue(eForm != null);
38
39 String s = eForm.getErrorMsg("firstName");
40 assertTrue(!s.equals("Please enter your first name"));
41 }
42 public void testCase6() {
43 employeeFormBean eForm = new employeeFormBean();
44 assertTrue(eForm != null);
45
46 String s = eForm.getErrorMsg("lastName");
47 assertTrue(!s.equals("Please enter your last name"));
48 }
49 public void testCase(int arg) {
50 }
51 }
Listing 12.3 (continued)
104 Item 12
Additionally, unit tests can be created to ensure that data in a database remains consistent and has not been corrupted during testing and integration operations. This is accomplished by creating unit tests that validate JDBC connections and user queries on database data.
01
02 import java.sql.*;
03 import java.util.*;
04 import junit.framework.*;
05 import junit.runner.BaseTestRunner;
06
07 public class dbQueryBean {
08
09 private static final String DRIVER_NAME="org.gjt.mm.mysql.Driver";
10 private static final String DB_URL="jdbc:mysql://localhost/States";
11 private static final String USERNAME="";
12 private static final String PASSWORD="";
13 private static final String QUERY="Select * from Topics";
14
15 Connection conn = null;
16 Statement stmt = null;
17 ResultSet rslt = null;
18
19 public dbQueryBean() {
20
21 try {
22 // get driver
23 Class.forName(DRIVER_NAME);
24 // connect to the MySQL db
25 Connection conn = 2
DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
26 Statement stmt = conn.createStatement();
27 ResultSet rslt = stmt.executeQuery(QUERY);
28 }
29 catch(Exception e) {
30 }
31 }
32
33 public void closeDb()
34 {
35 try {
36 // get driver
37 this.conn.close();
38 }
39 catch(Exception e) {
40 }
41 }
42
Listing 12.4 dbQueryBean.java
JUnit: Unit Testing Made Simple 105
43 public ResultSet getResultSet() {
44 return this.rslt;
45 }
46
47 public Connection getConnection() {
48 return this.conn;
49 }
50
51 public Statement getStatement() {
52 return this.stmt;
53 }
54
55 /* main */
56 public static void main(String[] args) {
57 junit.textui.TestRunner.run(suite());
58 }
59
60 / * *
61 * TestSuite that runs all the sample tests
62 *
63 */
64 public static Test suite() {
65 TestSuite suite= new TestSuite("DB Query Unit Tests");
66 suite.addTestSuite(dbQueryBeanTestCase.class);
67 return suite;
68 }
70 }
Listing 12.4 (continued)
The dbQueryBeanTestCase in Listing 12.5 demonstrates how to assess database connections and result sets that are returned from database queries. In most cases, a simple instantiation of your bean followed by an assert after the invocation of your bean's methods is the way to unit test your code. In this example, static database information is tested; on many enterprise systems the data is dynamic and tests like this would not be proper.
01 import java.sql.*;
02 import junit.framework.TestCase;
03
04 public class dbQueryBeanTestCase extends TestCase {
05 public dbQueryBeanTestCase(String name) {
06 super(name);
07 }
Listing 12.5 dbQueryBeanTestCase.java (continued)
106 Item 12
08 public void noTestCase() {
09 }
10 public void testCase1() {
11 employeeFormBean eForm = new employeeFormBean();
12 assertTrue(eForm != null);
13 }
14 public void testCase2() {
15
16 try {
17
18 dbQueryBean db = new dbQueryBean();
19 assertTrue(db != null);
20
21 // Get the resultset meta-data
22 ResultSet rslt = db.getResultSet();
23 ResultSetMetaData rmeta = rslt.getMetaData();
24
25 // Use meta-data to determine column #'s in each row
26 int numColumns = rmeta.getColumnCount();
27 String[] s = new String[numColumns];
28
29 for (int i=1; i < numColumns; i++) {
Previous << 1 .. 36 37 38 39 40 41 < 42 > 43 44 45 46 47 48 .. 166 >> Next