First of all ,suite , Chinese is A set of , A group of It means .

that ,TestSuite, seeing the name of a thing one thinks of its function , It's used to run a set of tests .

How to use it ?

Suppose there is a test class like this  StudentDAOTest , The code is as follows :

 package org.young.junit.testsuite;
import junit.framework.TestCase;
import org.young.junit.testsuite.dao.StudentDAO;
import org.young.junit.testsuite.dao.StudentDAOImpl;
import org.young.junit.testsuite.entity.Student; public class StudentDAOTest extends TestCase { private StudentDAO dao; /**
* establish DAO example
*/
public void setUp() {
dao = new StudentDAOImpl();
} public void testAdd() {
Student stu = new Student(); dao.add(stu);
} public void testDelete() { dao.delete("id");
} public void testUpdate() {
Student stu = new Student(); dao.update(stu);
} public void testLoadWithId() { Student stu = dao.load("xyz"); assertNotNull(stu);
} public void testLoadWithNullOrEmptyStr() { Student stu = dao.load("");
assertNull(stu); stu = dao.load(null);
assertNull(stu);
} }

If you want to execute several methods at a time , Instead of changing all the methods ?

TestSuite It's time to play. .

For comparison , Another test class  CourseDAOTest , The code is as follows :

 package org.young.junit.testsuite;
import org.junit.Before;
import org.junit.Test;
import org.young.junit.testsuite.dao.CourseDAO;
import org.young.junit.testsuite.dao.CourseDAOImpl;
import org.young.junit.testsuite.entity.Course; import junit.framework.TestCase;
import junit.framework.TestSuite; /**
* Suite The first way to achieve this is
*
* public static Test suite(){} The way
*
* @author by Young.ZHU
* on 2013-9-30
*
* Package&FileName: org.young.junit.testsuite.CourseDAOTest
*/
public class CourseDAOTest extends TestCase { private CourseDAO dao; public CourseDAOTest() {
super();
} public CourseDAOTest(String name) {
super(name);
} @Override
protected void setUp() throws Exception {
super.setUp(); dao = new CourseDAOImpl();
} /**
* Be careful : Inherit TestCase after ,JUnit 4 Inside @Before 、@Test It's no use waiting for annotations
*
* @Before The function of can be determined by the method setUp() Realization
*/
@Before
public void init() {
System.out.println("fsdfsdf");
dao = new CourseDAOImpl();
} /**
* Execute some methods of this test class
*
* The method head must be like this public static junit.framework.Test suite()
* namely , static state (static) Of
*
* @return
*/
public static junit.framework.Test suite() {
TestSuite suite = new TestSuite(); /*
* The string parameter is the method of the test class that you want to execute
*/
suite.addTest(new CourseDAOTest("testLoad"));
suite.addTest(new CourseDAOTest("testAdd")); return suite;
} @Test
public void testAdd() {
Course course = new Course(); dao.add(course);
} @Test
public void testDelete() {
fail("Not yet implemented");
} @Test
public void testUpdate() {
fail("Not yet implemented");
} @Test
public void testLoad() {
Course course = dao.load("course_id"); assertNotNull(course);
} }

Run it first , Look at the effect :

Although this test class has been added (add)、 Delete (delete)、 Change (update)、 check (load), But there are only two methods that actually work —— testLoad and testAdd .

The secret is code number 60 Yes  suite() Method , This method determines which methods the test class executes .

There are two points to explain :

1、 About the way  suite() It's the first way

As the note says , The method head of this method is fixed

 public static junit.framework.Test suite() {
// your code ...
}

2、 Test class construction method

Test class  CourseDAOTest   pass the civil examinations 30 Constructor with arguments on line , stay 66 Row sum 67 Yes, it does .

The parameter of the constructor is the name of the test method to be executed .

Last , Put the two together , Test class  AllTest , The code is as follows :

 package org.young.junit.testsuite;
import junit.framework.TestSuite;
public class AllTest {
public static junit.framework.Test suite() {
TestSuite suite = new TestSuite("All Test"); /*
* StudentDAOTest All test methods for class
*/
suite.addTest(new TestSuite(StudentDAOTest.class));
/*
* CourseDAOTest Partial methods of the class
*/
suite.addTest(CourseDAOTest.suite()); return suite;
} }

After operation , The effect is as follows :

Detailed code can be referred to :

https://github.com/YoungZHU/CollectionCode4Java/tree/master/test/org/young/junit/testsuite

JUnit —— TestSuite More articles about the use of

  1. Java unit testing (Junit+Mock+ Code coverage )

    WeChat official account [ Programmer's world ] Author Huang Xiaoxie , Slash the youth , some 985 master , Ali Java R & D Engineer , On 2018 In autumn, I got BAT headlines . NetEase . Didi et al 8 A big factory offer, At present, we are committed to sharing the learning experience of these years . ...

  2. Explore in depth JUnit 4

    Before the start About this tutorial introduce Java 5 The note is JUnit Has brought about a significant change , It has changed from a testing framework that is well known and loved by developers to a simpler but less well-known framework . In this tutorial , I'm going to explore JUnit ...

  3. Java unit testing (Junit+Mock+ Code coverage )--------- turn

    Java unit testing (Junit+Mock+ Code coverage ) See here for the original Unit testing is writing test code , Used to detect specific . Definite . The function of fine particles . Unit testing does not necessarily guarantee that the program functions correctly , There is no guarantee that the whole business is prepared . Monadic test ...

  4. 【java test 】Junit、Mock+ Code coverage

    See here for the original Unit testing is writing test code , Used to detect specific . Definite . The function of fine particles . Unit testing does not necessarily guarantee that the program functions correctly , There is no guarantee that the whole business is prepared . Unit testing is not only used to ensure the correctness of the current code , What's more, it's used to ensure that ...

  5. How to use Java test IBM Systems Director Of REST API

    from : http://www.ibm.com/developerworks/cn/aix/library/au-aix-systemsdirector/section2.html How to use Java ...

  6. Java Basic learning summary (24)——Java Unit tests JUnit4 Detailed explanation

    Java Unit tests JUnit4 Detailed explanation And JUnit3 Different ,JUnit4 Identify test methods by annotation . The main annotations currently supported are : @BeforeClass  The overall situation will only be executed once , And it's the first run @Before  ...

  7. junit4 Further discussion of

    Last time, I just recorded junit4 The usage of several common tags . In this article , Let's further analyze junit4 Usage of . 1. Assertion junit4 A very common use in is assertion . When it comes to assertions , You can't be more familiar with it . But maybe some friends ...

  8. JUnit And TestCase and TestSuite Detailed explanation

    First of all TestCase as well as TestSuite.    TestCase: Literally , The test case . Provide a test method for one or more methods . It's usually a test    TestSuite: Test set , It's a set of tests . One t ...

  9. JUnit 3.8 Let all test programs Realization Composite testing (TestSuite)

    Before it was a single program test , This method is feasible when there are few test classes , But there are more test classes , This single test method is not recommended , That would have to be used Composite test One TestSuite It's a composite test . It runs the test suite .   This ...

Random recommendation

  1. 【USACO 2.4】Overfencing(bfs shortest path )

    H That's ok W The labyrinth of columns , use 2*H+1 The string representation of the line , Each line has at most 2*W+1 Characters , Omit the space after each line . There are only two exits on the boundary of the maze , Find the shortest path from each point to the exit . +-+-+-+-+-+ | | +-+ +-+ ...

  2. acm mathematics ( turn )

    Let's put this thing here first . I'll use it later # It's the number 1.burnside Theorem ,polya Counting method     You can see brudildi Of < Combinatorial mathematics >, This chapter of that book is very detailed and easy to understand . Better be able to ...

  3. Spring Declarative transaction management is based on @Transactional annotation

    summary : We already know Spring There are two common ways of declarative transaction management , One is based on tx/aop Namespace xml The configuration file , The other is based on @Transactional annotation .          The first way I have mentioned above is big ...

  4. iOS Common design patterns and mechanisms KVO

    KVO http://blog.kyleduo.com/2014/10/20/ios_learning-kvo/ Definition : KVO:Key-Value Observing, yes Foundation Framework provide ...

  5. Write high quality code to improve C# programmatic 157 A suggestion [4-9]

    Preface First of all, this article is synchronized to http://www.cnblogs.com/aehyok/p/3624579.html. This article is mainly to study and record the content : Suggest 4.TryParse Than Parse good Suggest 5. Use int ...

  6. grep Easy to use

     grep " keyword " file file name  | tail -100|grep " keyword "  --col       grep The function of grep Look up... From one or more text files ...

  7. SQL GROUP BY sentence

    Aggregate function ( such as SUM) Often need to add GROUP BY sentence . GROUP BY sentence GROUP BY Statement is used in conjunction with the aggregate function , Group result sets according to one or more columns . SQL GROUP BY grammar ...

  8. JS Event dispatcher EventDispatcher

    stay Java and AS It is often used in EventDispatcher, Wrote a JS Version of . addListener : Add event listener removeListener: Remove event listener dispatchEvent: distributed ...

  9. [BZOJ2296] [POJ Challenge] Random seeds

    Description 1tthinking Besides random algorithms , Nothing else . But he can still ac A lot of questions , What did he use ? He would choose a good random seed , Then output the answer . Often a good seed he chooses can have 99% Probability ...

  10. css Code sorting

    width:( Width ) height:( Height ) border:1px solid red:( Frame : Border thickness Show Color ) border-radius:10deg:( Rounded border ) box-shadow: ...