How to quickly write multi-threaded Junit unit test cases

Source: Internet
Author: User

This article from One Coder blog, reprint please note the Source: http://www.coderli.com/archives/multi-thread-junit-grobountils/

Those who have written Junit unit tests should feel that Junit itself does not support general multi-threaded tests, because the underlying implementation of Junit is to exit the use case execution with System. exit. The JVM is terminated, and other threads started in the test thread cannot be executed. The JunitCore code is as follows:

 
 
  1. /** 
  2.      * Run the tests contained in the classes named in the <code>args</code>. 
  3.      * If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1. 
  4.      * Write feedback while tests are running and write 
  5.      * stack traces for all failed tests after the tests all complete. 
  6.      * @param args names of classes in which to find tests to run 
  7.      */ 
  8.     public static void main(String... args) { 
  9.         runMainAndExit(new RealSystem(), args); 
  10.     } 
  11.  
  12.     /** 
  13.      * Do not use. Testing purposes only. 
  14.      * @param system  
  15.      */ 
  16.     public static void runMainAndExit(JUnitSystem system, String... args) { 
  17.         Result result= new JUnitCore().runMain(system, args); 
  18.         system.exit(result.wasSuccessful() ? 0 : 1); 
  19.     } 

RealSystem. java:

 
 
  1. public void exit(int code) { 
  2.  
  3.         System.exit(code); 
  4.  
  5.     } 

Therefore, to write a multi-threaded Junit test case, the main thread must wait for all sub-threads to complete the execution before exiting. The method is the join method in the Thread. Then again, is there no third-party package support for such a simple and typical requirement? Through google, I quickly found GroboUtils, an open-source third-party toolkit for Junit multithreading testing. GroboUtils official website is as follows: http://groboutils.sourceforge.net/download page: http://groboutils.sourceforge.net/downloads.html Maven dependency mode:

 
 
  1. <dependency> 
  2.       <groupId>net.sourceforge.groboutils</groupId> 
  3.       <artifactId>groboutils-core</artifactId> 
  4.       <version>5</version> 
  5.     </dependency> 

Note: third-party database support is required:

Repository Opensymphony Releases
Repository url Https://oss.sonatype.org/content/repositories/opensymphony-releases
You can compile a multi-threaded test case after relying on the Jar package. Easy to use:
 
 
  1. /**
  2. * Multithreading Test Cases
  3. *
  4. * @ Author lihzh (One Coder)
  5. * @ Date 9:18:11 pm
  6. * @ Blog http://www.coderli.com
  7. */
  8. @ Test
  9. Public void MultiRequestsTest (){
  10. // Construct a Runner
  11. TestRunnable runner = new TestRunnable (){
  12. @ Override
  13. Public void runTest () throws Throwable {
  14. // Test content
  15. }
  16. };
  17. Integer runnerCount = 100;
  18. // Rnner array, the number of concurrent tasks.
  19. TestRunnable [] trs = new TestRunnable [runnerCount];
  20. For (int I = 0; I <runnerCount; I ++ ){
  21. Trs [I] = runner;
  22. }
  23. // Runner used to execute multi-threaded test cases. Input an array consisting of a single Runner defined earlier
  24. MultiThreadedTestRunner mttr = new MultiThreadedTestRunner (trs );
  25. Try {
  26. // Content defined in the concurrent execution of development
  27. Mttr. runTestRunnables ();
  28. } Catch (Throwable e ){
  29. E. printStackTrace ();
  30. }
  31. }

Run the command to check the effect. You can also execute multi-threaded test cases on Junit :).

This article from One Coder blog, reprint please note the Source: http://www.coderli.com/archives/multi-thread-junit-grobountils/

 

This article is from the "coder" blog. For more information, contact the author!

Related Article

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.