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:
- /**
- * 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 are running and write
- * stack traces for all failed tests after the tests all complete.
- * @param args names of classes in which to find tests to run
- */
- public static void main(String... args) {
- runMainAndExit(new RealSystem(), args);
- }
-
- /**
- * Do not use. Testing purposes only.
- * @param system
- */
- public static void runMainAndExit(JUnitSystem system, String... args) {
- Result result= new JUnitCore().runMain(system, args);
- system.exit(result.wasSuccessful() ? 0 : 1);
- }
RealSystem. java:
- public void exit(int code) {
-
- System.exit(code);
-
- }
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:
- <dependency>
- <groupId>net.sourceforge.groboutils</groupId>
- <artifactId>groboutils-core</artifactId>
- <version>5</version>
- </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:
- /**
- * Multithreading Test Cases
- *
- * @ Author lihzh (One Coder)
- * @ Date 9:18:11 pm
- * @ Blog http://www.coderli.com
- */
- @ Test
- Public void MultiRequestsTest (){
- // Construct a Runner
- TestRunnable runner = new TestRunnable (){
- @ Override
- Public void runTest () throws Throwable {
- // Test content
- }
- };
- Integer runnerCount = 100;
- // Rnner array, the number of concurrent tasks.
- TestRunnable [] trs = new TestRunnable [runnerCount];
- For (int I = 0; I <runnerCount; I ++ ){
- Trs [I] = runner;
- }
- // Runner used to execute multi-threaded test cases. Input an array consisting of a single Runner defined earlier
- MultiThreadedTestRunner mttr = new MultiThreadedTestRunner (trs );
- Try {
- // Content defined in the concurrent execution of development
- Mttr. runTestRunnables ();
- } Catch (Throwable e ){
- E. printStackTrace ();
- }
- }
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!