File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on addTestSuite & addTest in JUnit

 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic