This article describes the unit test of Java programming. Share it for your reference, as follows:
Click here to download the complete example code code.
At some point, we need to unit test the code we write ourselves (the benefit is to reduce the effort and expense of post-maintenance), which are some of the most basic module testing. Of course, while conducting unit tests, you must also be clear about the internal logical implementation of the code we tested, so that we can clearly verify and compare the results we hope to achieve the code logic implementation with the actual results obtained by the test during testing.
Less nonsense, please add the code:
First, create a java project, and create a unit-tested Student data class in the project, as follows:
package com.phicomme.hu; public class Student { private String name; private String sex; private int high; private int age; private String school; pu blic Student(String name, String sex ,int high, int age, String school) { this.name = name; this.sex = sex; this.high = high; this.age = age; this.school = school; } public String getName() { return name; } p ublic 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 age) { if (age >25) { return false; } else { this.age = age; re turn true; } } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } }
Unit test this class under eclipse:
First import the Junit package: select the java project, right-click --->Select properties ---->Select Java Build Path in the window---->Click Add Library on the right--->In the pop-up window list Select Junit----->Next----->Junit 4 (I use Junit 4)---->finish
In this way, the Junit 4 package is exported, and the next step is to create a test class:
Put the test class and the tested class in different packages (can also be placed in the same package, just for the difference here), the code is as follows:
Test Class 1:
package com.phicomme.test; import com.phicomme.hu.Student; import junit.framework.TestCase; public class StudentTest01 extends TestCase { Student test Student; //This method calls @ before executing each test method (test case) Override protected void setUp() throws Exception { // TODO Auto-generated method stub super.setUp(); testStudent = new Student("djm", "boy", 178, 24, " East China Politics and Law"); System.out. println("setUp()"); } //This method calls @Override protected void tearDown() throws Exception { // TODO Auto-generated method stub super.tearDown() ; System.out. println("tearDown()"); } //Test case, test the getSex() method of 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(24, testStudent.getAge()); System.out.println("testGetAge()"); } }
Test Class 2:
package com.phicomme.test; import junit.framework.TestCase; import com.phicomme.hu.Student; public class StudentTest extends TestCase { private Student t testStudent; @Override protected void setUp() throws Exception { // TODO Auto-generated method stub super.setUp(); testStudent = new Student("steven_hu", "boy", 170 , 23, "Shanghai Science and Technology"); } @Override protected void tearDown() throws Exception { // TOD O Auto-generated method stub super .tearDown(); } public void testSetage() { assertTrue(testStudent.setAge(21)); } public void testGetSchool() { //The expected value is different from the actual value, and there is a failure during the test (Failure) as sertEquals("Nanchang University", testStudent.getSchool()); } public void testGetName() { assertEquals("hdy", testStudent.getName()); } }
Of course, if you need to test the above two test classes together, you can implement it through the TestSuite class, which is equivalent to a suite that can add all test classes to run tests together;
The code is as follows:
package com.phicomme.test; import com.phicomme.hu.StudentTest02; import junit.framework.Test; import junit.framework.TestSuite; public class AllT est { //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 (select the class that needs to be tested ----->Run As---->Junit Test):
Test result diagram of StudentTest class:
Test result diagram of StudentTest01 class:
Test result diagram of AllTest class:
That’s all about Java testing. I hope it will be helpful to everyone. If you have time, I will continue to talk about unit testing on Android and implement the writing of a UI interface on your mobile phone instead of the test interface in the above picture;
I hope this article will be helpful to everyone's Java programming.