aspose file tools*
The moose likes Testing and the fly likes Question on addTestSuite & addTest in JUnit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Question on addTestSuite & addTest in JUnit " Watch "Question on addTestSuite & addTest in JUnit " New topic
Author

Question on addTestSuite & addTest in JUnit

Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

I am new to JUnit and I have just completed a series of tests in this one junit test file called SQLServerInitTest:

public class SQLServerInitTest extends TestCase {

static String HOST_NAME = "localhost";
static String DB_NAME = "ESDev";
static String USER_NAME = "test";
static String PASSWORD = "test";
static String[] DB_TABLE = {"POI"};
static String[] FIELD_NAMES = {"ASSETNUMBER, CHARGETO, HOSPCODE, ITEMNUMBER, LINEITEMPRICE, NEC, NEXTACCT, PURCHASEUM, QUANTITYORDERED, RECSTATUS, REQUISITIONNUMBER, SPCDESCRIPTION, TYPESWITCH"};

public static void main(String[] args) {
junit.swingui.TestRunner.run(SQLServerInitTest.class);
}

/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}

/*
* @see TestCase#tearDown() //last to run in the suite method.
*/
protected void tearDown() throws Exception {
super.tearDown();
Connection con = getConnection();
if(con != null){
try {
con.close();
}
catch (Exception e) {
System.out.println("Error closing connection: " + e.getMessage());
}
}
}

/*
* @testConnection
*/
public void testConnection(){
Connection con = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://"+ HOST_NAME +":1433;DatabaseName="+ DB_NAME +";User="+ USER_NAME +";Password="+ PASSWORD +"");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

/*
* @getConnect
*/
public Connection getConnection(){
// Make connection to a SQL Server 2000 database.
Connection con = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://"+ HOST_NAME +":1433;DatabaseName="+ DB_NAME +";User="+ USER_NAME +";Password="+ PASSWORD +"");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
return con;
}

/*
* @testDropTable
*/
public void testDropTable () throws SQLException{
try {
Connection con = getConnection();
Statement stmt = con.createStatement();
String dropTable = "DROP TABLE";
for (int i = 0; i < DB_TABLE.length; i++){
String stringCode = new String();
stringCode = stringCode + DB_TABLE[i];
}

// Drop POI table.
int temp = stmt.executeUpdate(dropTable);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

/*
* @testCreateTable
*/
public void testCreateTable() throws SQLException{
try {
Connection con = getConnection();
Statement stmt = con.createStatement();

// Create table with columns in database
stmt.executeUpdate("CREATE TABLE poi (AssetNumber VARCHAR(100), ChargeTo VARCHAR(100), " +
"HospCode VARCHAR(100), ItemNumber VARCHAR(100), LineItemPrice DECIMAL, " +
"NEC VARCHAR(100), NextAcct VARCHAR(100), PurchaseUM VARCHAR(100), " +
"QuantityOrdered VARCHAR(100), RecStatus VARCHAR(100), RequisitionNumber VARCHAR(100), " +
"SPCDescription VARCHAR(100), TypeSwitch VARCHAR(100)");
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}

/*
* @testInsertRecords
*/
public void testInsertRecords () throws SQLException{
try {
Connection con = getConnection();
Statement stmt = con.createStatement();

// Insert records into the POI table.
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('68468', '8681', 'HAR', NULL, 25.00, '4050', 'savoym', 'EA00001', '1', 'A', '10007523', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('1561', '6841', 'PHD', NULL, 125.00, '3000', 'savoym', 'EA00046', '1', 'A', '10007521', 'ANOTHER TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('46846', '4841', 'HSW', NULL, 1225.00, '8481', 'savoym', 'EA01841', '1', 'A', '10007534', 'POI WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('86411', '7121', 'PHK', NULL, 725.00, '4680', 'savoym', 'EA008646', '1', 'A', '10007610', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('86411', '7121', 'PHK', NULL, 725.00, '4680', 'savoym', 'EA008646', '1', 'A', '10007610', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('86411', '7121', 'PHK', NULL, 725.00, '4680', 'savoym', 'EA008646', '1', 'A', '10007610', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('86411', '7121', 'PHK', NULL, 725.00, '4680', 'savoym', 'EA008646', '1', 'A', '10007610', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('86411', '7121', 'PHK', NULL, 725.00, '4680', 'savoym', 'EA008646', '1', 'A', '10007610', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('86411', '7121', 'PHK', NULL, 725.00, '4680', 'savoym', 'EA008646', '1', 'A', '10007610', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('86411', '7121', 'PHK', NULL, 725.00, '4680', 'savoym', 'EA008646', '1', 'A', '10007610', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('86411', '7121', 'PHK', NULL, 725.00, '4680', 'savoym', 'EA008646', '1', 'A', '10007610', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('86411', '7121', 'PHK', NULL, 725.00, '4680', 'savoym', 'EA008646', '1', 'A', '10007610', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('86411', '7121', 'PHK', NULL, 725.00, '4680', 'savoym', 'EA008646', '1', 'A', '10007610', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('86411', '7121', 'PHK', NULL, 725.00, '4680', 'savoym', 'EA008646', '1', 'A', '10007610', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
stmt.executeUpdate("INSERT INTO poi "+ FIELD_NAMES +" "+
"VALUES ('86411', '7121', 'PHK', NULL, 725.00, '4680', 'savoym', 'EA008646', '1', 'A', '10007610', 'TESTING WEB APPLICATION FOR APPROVING RECORDS(1)', 'N')");
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}

public static Test suite() {
TestSuite suite = new TestSuite(SQLServerInitTest.class);

//$JUnit-BEGIN$
suite.addTest(new SQLServerInitTest("testConnection"));
suite.addTest(new SQLServerInitTest("getConnection"));
suite.addTest(new SQLServerInitTest("testDropTable"));
suite.addTest(new SQLServerInitTest("testCreateTable"));
suite.addTest(new SQLServerInitTest("testInsertRecords"));
//$JUnit-END$
return suite;
}
}


Down as the end of this code are bolded lines of code that give me an error as such:

The constructor SQLServerInitTest(String) is undefined

Any suggestions or direction on how to clear up my error would be appreciated.

Thank you.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Well, as the error message says, you didn't provide the constructor getting a String argument. You will have to write one. It will probably just call the super constructor, passing on the String argument.

As an aside, it looks like you are manually building the test suite because the tests need to be executed in a certain order? Are you aware of the fact that this is a "test smell"?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

Ilja,

Thanks, I did just that by creating a constructor and that resolved my issue.

Also, I did NOT know that was a test smell. We have a contractor working with us and that is how he told us to build this test and the test suite respectively.

Are there down sides to a "TEST SMELL"?

Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

A "smell" is always bad. "Test smell" is a backformation from Kent Beck's "code smell". A "smell" is an indication that something's wrong, and you should check into it.

In this case, the problem is that your tests aren't independent. If one fails, they're all going to fail, which reduces their diagnostic value and also makes it hard to add, remove, or refactor tests.

I realize that you've done it this way to reduce the number of connections made to your database -- but that's actually the problem right there: unit tests shouldn't be hitting a real database unless absolutely necessary. Introduce a persistence layer, and test against DAOs loaded from a file. Or if you are specifically testing database code, try to use something lighter-weight like HSQLDB so that you can afford to reconnect on each test.


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
What Ernest said. For more info on code smells, see http://c2.com/cgi/wiki?CodeSmell
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on addTestSuite & addTest in JUnit
 
Similar Threads
Null pointer exception in con.createStatement();
inserting null values in the backend using enterprise beans
JDBC Oracle connection...not working
how to run code in netbeans
data mismatch error