The most basic module testing
1: First create a Java project and create a student data class in the project that is being unit tested, as follows:
PackageUnitTest; Public classStudent {PrivateString name; PrivateString sex; Private intHigh ; Private intAge ; PrivateString School; PublicStudent (string name, String sex,intHighintAge , String School) { This. Name =name; This. Sex =sex; This. High =High ; This. Age =Age ; This. School =School; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString Getsex () {returnsex; } Public voidsetsex (String sex) { This. Sex =sex; } Public intGethigh () {returnHigh ; } Public voidSethigh (intHigh ) { This. High =High ; } Public intGetage () {returnAge ; } Public BooleanSetage (intAge ) { if(Age > 25) { return false; } Else { This. Age =Age ; return true; } } PublicString Getschool () {returnSchool; } Public voidSetschool (String school) { This. School =School; }}
Student
2: under Eclipse Unit test this class:
First import the JUnit package: Select Java Project , right-click---> select Properties----> Choose Java Build Path----in the window > click Add Library on the right side---- > in the pop-up window list, select JUnit----> Next----->junit 4 (I'm using junit 4)---->finish
So the JUnit 4 package is done, and the next step is to create the test class:
Put the test class and the tested class in a different package (or in the same package, here just to make a difference), the code is as follows:
Test Class 1:
Packageunittestexample;Importunittest.student;Importjunit.framework.TestCase; Public classStudentTest01extendsTestCase {Student teststudent; //This method is called before each test method (test case) is executed .@Overrideprotected voidSetUp ()throwsException {//TODO auto-generated Method Stub Super. SetUp (); Teststudent=NewStudent ("Li Peng", "Boy", 172, 21, "East China Jiaotong University"); System.out.println ("SetUp ()"); } //This method calls after each test method is executed@Overrideprotected voidTearDown ()throwsException {//TODO auto-generated Method Stub Super. TearDown (); System.out.println ("TearDown ()"); } //test cases, testing the Getsex () method of the Person object Public voidTestgetsex () {assertequals ("Boy.", Teststudent.getsex ()); System.out.println ("Testgetsex ()"); } //test the Getage () method of the Person object Public voidTestgetage () {assertequals (21st, Teststudent.getage ()); System.out.println ("Testgetage ()"); }}
StudentTest01
Test Class 2:
Packageunittestexample;Importunittest.student;Importjunit.framework.TestCase; Public classStudentTest02extendsTestCase {PrivateStudent teststudent; @Overrideprotected voidSetUp ()throwsException {//TODO auto-generated Method Stub Super. SetUp (); Teststudent=NewStudent ("Xiao Ming", "Boy", 170, 23, "Shanghai Polytechnic"); } @Overrideprotected voidTearDown ()throwsException {//TODO auto-generated Method Stub Super. TearDown (); } Public voidTestsetage () {asserttrue (Teststudent.setage (21st)); } Public voidTestgetschool () {//The expected value is not the same as the actual value, and the test fails (Failure)Assertequals ("Nanchang University", Teststudent.getschool ()); } Public voidTestgetname () {assertequals ("Little Flower", Teststudent.getname ()); }}
StudentTest02
Of course, if you need to test both of these two test classes together, it can be implemented by the Testsuite class, which is equivalent to a suite that can add all the test classes and run tests together.
Comprehensive test class:
Packageunittestexample;Importunittest.student;Importjunit.framework.Test;ImportJunit.framework.TestSuite; Public classAllTest {//static Persontest p = new Persontest (); //static Persontest p1 = new Persontest (); Public StaticTest Suite () {TestSuite Suite=NewTestSuite ("Test for Com.phicomme.test"); //suite.addtest (P); //suite.addtest (p1);Suite.addtestsuite (StudentTest02.class); Suite.addtestsuite (StudentTest01.class); returnSuite; }}
alltest
Finally, test the above three classes separately (select the class you want to test----> right-click---->run as---->junit test):
STUDENTTEST01 class Test results:
StudentTest02 class Test results:
AllTest class Test results:
Java Unit Test (JUNIT)