Test classification: white box test, black box test, unit test, integrated test, function test ... White box test refers to the tests made under the conditions that the software is "how to complete the function and" What), which is generally completed by the developer, and the unit test is a white box test because Developers understand the software you have written. JUnit is a regression test framework written by Erich Gamma and Kent Beck. The regression test is that you constantly test the code written (such as unit test): Writing some, test some, debug some, then loop this process, you Will constantly repeat the previous test, even if you are writing other classes.
Step 1: Go to the JUnit Home (http://www.junit.org) Download the latest version 3.8.1 package junit-3.8.1.zip. Unstage the compression package to C: / junit (can be customizable).
Step 2: If the directory is C: / Junit, add: "C: / junit /; c: /junit/junit.jar;" defined class path. Run under the command prompt: Java junit.swingui.teestrunner, if everything is correct, the application will open. Looking for an example of the program comes with the program in the drop-down menu, such as junit.samples.alltests, click on "Run" observation.
Step 3: Implement your own Test plan, there is currently a database operation class called MyBean, as follows: package junit.samples;
Import java.sql. *; import java.io. *;
Public class mybean {statement stmt = null; resultset = null; connection conn = null; string result = null; public string con () {// initialization database Try {class.forname ("Org.gjt.mm.mysql.driver ") .newinstance (); string url =" jdbc: mysql: //192.168.0.88/weBoA? user = root & password = "; conn = drivermanager.getConnection (URL); Return" Connection Success! ";} catch (Exception E ) {System.out.println (e); return "connection error!";}} Public string gogo (String LMDM) {// query database try {stmt = conn.createstatement (); string sql = "SELECT * FROM TB_LM WHERE N_LMDM = '" LMDM "' "; rs = stmt.executeQuery (SQL); // Execute the query while (rs.next ()) {result = rs.getstring (" n_sjid ");}} catch (Exception E ) {Result = e.tostring ();} finally { // Turn off the JDBC resource if (RS! = Null) try {r r (sqlexception ex) {EX.PRINTSTACKTRACE ();} if (conn! = Null) Try {conn.close (); Catch (SQLEXCEPTION EX) {EX.PrintStackTrace ();}} Return Result;} then creates a test class: TestmyBean, as follows: package junit.samples; import junit.samples.mybean; import junit.framework. *;
Public Class TestmyBean Extends Testcase {// Testcase Sub-class Private MyBean Aname; // Constructs Object PUBLIC TESTMYBEAN (String Name) {Super (Name);} Protected Void Setup () {// Perform initialization task Aname = new mybean ();} public static test suite () {// Test Return New Testsuite (TestmyBean.class);}
Public void testcon () {// compares assert.asserttrue (! aname.equals (null)); // assertion assert.assertequals ("Connection Success!", Aname.con ());} Public void testgogo () {// Compare aname.con () on expected values and GOGO methods; assert.asserttrue (! Aname.Equals (null)); // Assert.assertequals ("0", Aname.gogo ("0", Aname.gogo "1"));}} Explan as follows: First, you want to introduce the class import junit.samples.mybean to be tested; then introduce the JUnit Framework Import Junit.framework. *; Similar to a servlet, you need to inherit the parent class TestCase; instantiate a MyBean in the setup () method for use; Suite () is a very special static method, which will use the reflex dynamic creation of all The test suite of the TestXxxx method determines how many tests can be executed; the testCon () method tests the MYBean's CON method, and asserts the result "Connection Success!" And verified in the assert.assertequals () method. The testgogo () method is similar to the testCon () method.
Step 4: Compile TestMyBean, MyBean classes into * .class files, select the TestmyBean class just defined on the JUnit console, and run. If everything is correct, a green strip is displayed and the test is correct. If red is displayed, there will be corresponding display in Results, and check the error in the MyBean class according to the prompt. In general, as long as it asserts that the Mybean class is compliant, the TestmyBean class is almost impossible.