Java Unit Test (JUNIT)

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/stevenhu_223/article/details/826915 Related code download link: 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
  1. Package com.phicomme.hu;
  2. Public class Student
  3. {
  4. private String name;
  5. private String sex;
  6. private int high;
  7. private int age;
  8. Private String School;
  9. Public Student (string name, String sex,int. int, int age, string school)
  10. {
  11. this.name = name;
  12. this.sex = sex;
  13. This.high = high;
  14. this.age = age;
  15. This.school = School;
  16. }
  17. Public String getName ()
  18. {
  19. return name;
  20. }
  21. public void SetName (String name)
  22. {
  23. this.name = name;
  24. }
  25. Public String getsex ()
  26. {
  27. return sex;
  28. }
  29. public void Setsex (String sex)
  30. {
  31. this.sex = sex;
  32. }
  33. public int Gethigh ()
  34. {
  35. return high;
  36. }
  37. public void Sethigh (int high)
  38. {
  39. This.high = high;
  40. }
  41. public int getage ()
  42. {
  43. return age;
  44. }
  45. Public Boolean setage (int.)
  46. {
  47. if (age >)
  48. {
  49. return false;
  50. }
  51. Else
  52. {
  53. this.age = age;
  54. return true;
  55. }
  56. }
  57. Public String Getschool ()
  58. {
  59. return School;
  60. }
  61. public void Setschool (String school)
  62. {
  63. This.school = School;
  64. }
  65. }

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
  1. Package com.phicomme.test;
  2. Import com.phicomme.hu.Student;
  3. Import Junit.framework.TestCase;
  4. Public class StudentTest01 extends TestCase
  5. {
  6. Student teststudent;
  7. //This method is called before each test method (test case) is executed
  8. @Override
  9. protected void SetUp () throws Exception
  10. {
  11. //TODO auto-generated method stub
  12. Super.setup ();
  13. Teststudent = New Student ("DJM", "Boy", 178, " East China Law and Politics");
  14. System.out.println ("setUp ()");
  15. }
  16. //This method is called after each test method is executed
  17. @Override
  18. protected void TearDown () throws Exception
  19. {
  20. //TODO auto-generated method stub
  21. Super.teardown ();
  22. System.out.println ("TearDown ()");
  23. }
  24. //test case, testing the Getsex () method of the Person object
  25. public void Testgetsex ()
  26. {
  27. Assertequals ("Boy", Teststudent.getsex ());
  28. System.out.println ("Testgetsex ()");
  29. }
  30. //Test the Getage () method of the Person object
  31. public void Testgetage ()
  32. {
  33. Assertequals (teststudent.getage ());
  34. System.out.println ("Testgetage ()");
  35. }
  36. }


Test Class 2:

[Java]View Plaincopy
  1. Package com.phicomme.test;
  2. Import Junit.framework.TestCase;
  3. Import com.phicomme.hu.Student;
  4. Public class Studenttest extends TestCase
  5. {
  6. private Student teststudent;
  7. @Override
  8. protected void SetUp () throws Exception
  9. {
  10. //TODO auto-generated method stub
  11. Super.setup ();
  12. Teststudent = New Student ("Steven_hu", "Boy", "he", " Shanghai Polytechnic");
  13. }
  14. @Override
  15. protected void TearDown () throws Exception
  16. {
  17. //TODO auto-generated method stub
  18. Super.teardown ();
  19. }
  20. public void Testsetage ()
  21. {
  22. Asserttrue (Teststudent.setage (21));
  23. }
  24. public void Testgetschool ()
  25. {
  26. //Expected value is not the same as the actual value, failed during test (Failure)
  27. Assertequals ("Nanchang University", Teststudent.getschool ());
  28. }
  29. public void Testgetname ()
  30. {
  31. Assertequals ("Hdy", Teststudent.getname ());
  32. }
  33. }


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
  1. Package com.phicomme.test;
  2. Import com.phicomme.hu.StudentTest02;
  3. Import Junit.framework.Test;
  4. Import Junit.framework.TestSuite;
  5. Public class AllTest
  6. {
  7. //static persontest p = new Persontest ();
  8. //static persontest p1 = new Persontest ();
  9. public Static Test Suite ()
  10. {
  11. TestSuite Suite = new TestSuite ("Test for Com.phicomme.test");
  12. //suite.addtest (P);
  13. //suite.addtest (p1);
  14. Suite.addtestsuite (studenttest.   Class);
  15. Suite.addtestsuite (StudentTest01.   Class);
  16. return suite;
  17. }
  18. }


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)

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.