http://blog.csdn.net/stevenhu_223/article/details/8269157
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
http://download.csdn.net/detail/stevenhu_223/4884357
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:
[Java]View PlainCopy
- Package com.phicomme.hu;
- Public class Student
- {
- private String name;
- private String sex;
- private int high;
- private int age;
- Private String School;
- Public Student (string name, String sex,int. int, int age, string school)
- {
- this.name = name;
- this.sex = sex;
- This.high = high;
- this.age = age;
- This.school = School;
- }
- Public String getName ()
- {
- return name;
- }
- public void SetName (String name)
- {
- this.name = name;
- }
- Public String getsex ()
- {
- return sex;
- }
- public void Setsex (String sex)
- {
- this.sex = sex;
- }
- public int Gethigh ()
- {
- return high;
- }
- public void Sethigh (int high)
- {
- This.high = high;
- }
- public int getage ()
- {
- return age;
- }
- Public Boolean setage (int.)
- {
- if (age >)
- {
- return false;
- }
- Else
- {
- this.age = age;
- return true;
- }
- }
- Public String Getschool ()
- {
- return School;
- }
- public void Setschool (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:
[Java]View PlainCopy
- Package com.phicomme.test;
- Import com.phicomme.hu.Student;
- Import Junit.framework.TestCase;
- Public class StudentTest01 extends TestCase
- {
- Student teststudent;
- //This method is called before each test method (test case) is executed
- @Override
- protected void SetUp () throws Exception
- {
- //TODO auto-generated method stub
- Super.setup ();
- Teststudent = New Student ("DJM", "Boy", 178, " East China Law and Politics");
- System.out.println ("setUp ()");
- }
- //This method is called after each test method is executed
- @Override
- protected void TearDown () throws Exception
- {
- //TODO auto-generated method stub
- Super.teardown ();
- System.out.println ("TearDown ()");
- }
- //test case, testing the Getsex () method of the Person object
- public void Testgetsex ()
- {
- Assertequals ("Boy", Teststudent.getsex ());
- System.out.println ("Testgetsex ()");
- }
- //Test the Getage () method of the Person object
- public void Testgetage ()
- {
- Assertequals (teststudent.getage ());
- System.out.println ("Testgetage ()");
- }
- }
Test Class 2:
[Java]View PlainCopy
- Package com.phicomme.test;
- Import Junit.framework.TestCase;
- Import com.phicomme.hu.Student;
- Public class Studenttest extends TestCase
- {
- private Student teststudent;
- @Override
- protected void SetUp () throws Exception
- {
- //TODO auto-generated method stub
- Super.setup ();
- Teststudent = New Student ("Steven_hu", "Boy", "he", " Shanghai Polytechnic");
- }
- @Override
- protected void TearDown () throws Exception
- {
- //TODO auto-generated method stub
- Super.teardown ();
- }
- public void Testsetage ()
- {
- Asserttrue (Teststudent.setage (21));
- }
- public void Testgetschool ()
- {
- //Expected value is not the same as the actual value, failed during test (Failure)
- Assertequals ("Nanchang University", Teststudent.getschool ());
- }
- public void Testgetname ()
- {
- 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:
[Java]View PlainCopy
- Package com.phicomme.test;
- Import com.phicomme.hu.StudentTest02;
- Import Junit.framework.Test;
- Import Junit.framework.TestSuite;
- Public class AllTest
- {
- //static persontest p = new Persontest ();
- //static persontest p1 = new Persontest ();
- public Static Test Suite ()
- {
- TestSuite Suite = new TestSuite ("Test for Com.phicomme.test");
- //suite.addtest (P);
- //suite.addtest (p1);
- Suite.addtestsuite (studenttest. Class);
- Suite.addtestsuite (StudentTest01. Class);
- return suite;
- }
- }
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;
Java Unit Test (Junit)