The framework for testing with JUnit3 in Eclipse is:
[Java]
Package test;
Import java. SQL. Connection;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;
Import server. db. DB;
Import junit. framework. TestCase;
Public class DBTest extends TestCase {
Protected void setUp () throws Exception {
Super. setUp ();
}
Protected voidtearDown () throws Exception {
Super. tearDown ();
}
Public void testDB () throws ClassNotFoundException, SQLException {
// Read the database
Connectionconn = DB. getConn ();
Statementstmt = conn. createStatement ();
ResultSet rs = stmt.exe cuteQuery ("select * from users ;");
System. out. println ("---------- \ nDB: Teststart ");
While (rs. next ()){
System. out
. Println (rs. getInt (1) + rs. getString (2) + rs. getString (3 ));
}
AssertEquals (1, 1 );
System. out. println ("DB: Statusok \ n ----------");
}
}
All similar test methods can be written in this class, and the test results are analyzed using assertions. The JUnit Suite is used for encapsulation for multiple test examples. Its structure is as follows:
[Java]
Package test;
Import junit. framework. Test;
Import junit. framework. TestSuite;
Public class AllTests {
Public static Testsuite (){
TestSuitesuite = new TestSuite ("Test for test ");
// $ JUnit-BEGIN $
Suite. addTestSuite (ProducerToolTest. class );
Suite. addTestSuite (DBTest. class );
// $ JUnit-END $
Returnsuite;
}
} Www.2cto.com
The above program can be implemented using the automatic generation function of Eclipse, and the Junit option is located under Java. The above test results are: