JUnit uses groboutils for multithreaded testing

Source: Internet
Author: User

Those of you who have written JUnit unit tests should have a feeling thatJUnit itself does not support normal multithreaded testing , because the underlying implementation of JUnit is executed with the System.exit exit use case. The JVM is terminated, and other threads that are started by the test thread will not be able to execute naturally. The Junitcore code is as follows:

/*** Run The tests contained in the classes named in the <code>args</code>. * If All tests run successfully, exit with a status of 0.      Otherwise exit with a status of 1.      * Write feedback while tests is running and Write * Stack traces for all failed tests after the tests all complete. * @paramargs names of classes in which to find tests to run*/      Public Static voidMain (String ... args) {Runmainandexit (NewRealsystem (), args); }      /*** Do not use .      testing purposes only. * @paramsystem*/      Public Static voidrunmainandexit (Junitsystem system, String ... args) {result result=NewJunitcore (). Runmain (System, args); System.exit (result.wassuccessful ()? 0:1); } 

Realsystem.java:

 Public void exit (int  code) {         System.exit (code);     

So to write multithreaded JUnit test cases, you must let the main thread wait for all child threads to complete before exiting. The way to think of it is naturally the join method in thread. Then again, such a simple and typical demand, there is no third-party package support it? With Google, we quickly found the open source third-party toolkit for Groboutils, the JUnit multithreaded test.

Groboutils official website is as follows: http://groboutils.sourceforge.net/

Download page: http://groboutils.sourceforge.net/downloads.html, use groboutils-5\lib\core\ after decompression Grobotestingjunit-1.2.1-core.jar this can be

Groboutils is a collection of tools that contains a variety of test tools, using the JUnit extensions in the toolset.

You can write multithreaded test cases after you rely on a good jar package. Getting Started is simple:

 Packagecom.junittest.threadtest; Importjava.util.ArrayList; ImportJava.util.HashSet; Importjava.util.Hashtable; Importjava.util.List; ImportJava.util.Map; ImportJava.util.Set; ImportNet.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner; Importnet.sourceforge.groboutils.junit.v1.TestRunnable; Importorg.junit.Test;  Public classMutithreadtest {Staticstring[] Path =NewString[] {"" }; Staticmap<string, string> countmap =NewHashtable<string, string>(); Staticmap<string, string> countMap2 =NewHashtable<string, string>(); StaticSet<string> Countset =NewHashset<string>(); Staticlist<string> list =NewArraylist<string>(); @Test Public voidTestthreadjunit ()throwsThrowable {//Runner Array, how many you want to be in concurrency. Testrunnable[] TRS =Newtestrunnable [10];  for(inti=0;i<10;i++) {Trs[i]=NewThreada (); }          //The runner used to perform multithreaded test cases, passing an array of the previously defined single runner intoMultithreadedtestrunner mttr =NewMultithreadedtestrunner (TRS); //develop concurrent execution of content defined in an arrayMttr.runtestrunnables (); }        Private classThreadaextendstestrunnable {@Override Public voidRuntest ()throwsThrowable {//Test ContentmyCommMethod2 (); }      }         Public voidMYCOMMMETHOD2 ()throwsException {System.out.println ("= = =" + Thread.CurrentThread (). GetId () + "begin to execute MYCOMMMETHOD2");  for(inti = 0; I <10; i++) {               intA = I*5;          System.out.println (a); } System.out.println ("= = =" + Thread.CurrentThread (). GetId () + "end to execute MYCOMMMETHOD2"); }  }  

The runtime relies on the log4j jar file, the Groboutils jar package.

Main focus 3 Categories: Testrunnable,testmonitorrunnable,multithreadedtestrunner, all from the package: Net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner.

(1) Testrunnable abstract class, which represents a test thread, the instance needs to implement the class's Runtest () method, and write its own test code in the method.

This class inherits the Junit.framework.Assert class from JUnit, so you can use the various assert methods in Testrunnable

(Unfortunately, this class is deprecated in the new version of JUnit because the version of JUnit used by Groboutils is older and has not been updated for a long time.)

The class implements the runnable and calls the abstract method Runtest () in the Run method.

(2) Multithreadedtestrunner

This class is quite similar to a executeservice, which can be used to execute testrunnable, and the constructor needs to pass in the testrunnable array.

Represents the thread that needs to be tested.

Call the Multithreadedtestrunner.runtestrunnables () method to start the test thread and start the test execution.

This method, by default, lets the test thread testrunnable the Run method for up to 1 days, or it can call

Multithreadedtestrunner.runtestrunnables (Long MaxTime) This method, and then the test thread testrunnable

Executes up to maxtime milliseconds. If the testrunnable has not completed after MaxTime milliseconds, the testrunnable

will be interrupted, and Multithreadedtestrunner will throw an exception,

Causes a test failure fail ("Threads did not within" + MaxTime + "milliseconds.").

Each testrunnable in runtest need to be able to control themselves at what time to end themselves, precisely control the test time, do not use

Above the MaxTime.

(3) Testmonitorrunnable

Represents a monitoring thread that allows each testrunnable to correspond to a testmonitorrunnable and monitor in testmonitorrunnable

Testrunnable.testmonitorrunnable is a subclass of Testrunnable and provides some handy ways to use it.

JUnit uses groboutils for multithreaded testing

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.