JUnit test and suite

Source: Internet
Author: User

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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.