Java Unit Test (Junit)

Source: Internet
Author: User

In some cases, we need to unit test our own code (the benefit is to reduce the effort and expense of late maintenance), which is some of the most basic module tests. Of course, in the case of unit testing, it is necessary to understand the internal logic implementation of the code we test, so that we can clearly compare the results we expect from the code logic implementation to the actual results of the test when testing.

Nonsense less say, on the code:

First create a Java project and create a student data class in the project that is being tested by the unit, as follows

 Packagecom.phicomme.hu; 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; }    }

Under Eclipse, unit Tests 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----> 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:

 Packagecom.phicomme.test;Importcom.phicomme.hu.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 ("DJM", "Boy", 178, 24, "East China Law and politics"); 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 (24, Teststudent.getage ()); System.out.println ("Testgetage ()"); }}

Test Class 2:

 Packagecom.phicomme.test;Importjunit.framework.TestCase;Importcom.phicomme.hu.Student; Public classStudenttestextendstestcase{PrivateStudent teststudent; @Overrideprotected voidSetUp ()throwsException {//TODO auto-generated Method Stub        Super. SetUp (); Teststudent=NewStudent ("Steven_hu", "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 ("Hdy", Teststudent.getname ()); }}

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.

The code is as follows:

 Packagecom.phicomme.test;Importcom.phicomme.hu.StudentTest02;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 (studenttest.class); Suite.addtestsuite (StudentTest01.class); returnSuite; }}

Finally, test the above three classes separately (select the class you want to test----> right-click---->run as---->junit test):

Test result diagram for Studenttest class:

Test result diagram for STUDENTTEST01 class:

Test result diagram for AllTest class:

The Java test is going to be here, and hopefully it will be helpful to have time to talk about the Android unit test, and to write a UI interface on the phone to replace Eclipse's test interface;

This article transferred from: http://blog.csdn.net/stevenhu_223/article/details/8269157

Java Unit Test (Junit)

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.