作者:蒲哟独CrRz | 来源:互联网 | 2024-11-21 16:03
Eclipse下使用JUnit3来进行测试的框架为:
- 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;
- publicclass DBTest extends TestCase {
- protectedvoid setUp()throws Exception {
- super.setUp();
- }
- protected voidtearDown() throws Exception {
- super.tearDown();
- }
- publicvoid testDB()throws ClassNotFoundException, SQLException {
- COnnectionconn= DB.getConn();
- Statementstmt = conn.createStatement();
- ResultSet rs= stmt.executeQuery("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----------");
- }
- }
所有类似的测试方法都可以写在此类中,使用断言对测试结果进行分析。多个测试例子需要使用JUnit的Suite来进行封装。其结构为:
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;
}
}
上述程序可以使用Eclipse的自动生成功能来进行,Junit选项位于Java下。以上测试结果为:
#