JUnit uses groboutils for multithreaded testing

Source: Internet
Author: User
Tags throwable

students who have written JUnit unit tests should have feelings, JUnit itself is not supported for normal multithreaded testing , this is 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 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)  {      &nBsp;   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); }

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:

package com.junittest.threadtest;    import java.util.arraylist;   import java.util.hashset;  import java.util.hashtable;  import  java.util.list;  import java.util.map;  import java.util.set;     import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;  import  net.sourceforge.groboutils.junit.v1.testrunnable;      import  org.junit.test;    public class mutithreadtest {         static String[] path = new String[] {  ""  };       static map<string, string> countmap = new  Hashtable<String, String> ();       static map<string,  string> countmap2 = new hashtaBle<string, string> ();      static set<string>  Countset = new hashset<string> ();      static list< String> list = new arraylist<string> ();          @Test       public void testthreadjunit ()  throws  Throwable {           //runner array, how many you want to be in concurrency.          TestRunnable[] trs = new  Testrunnable [10];          for (int i=0;i<10;i+ +) {              trs[i]=new threada ();          }           //  runner for executing multithreaded test cases,An array of the individual runner defined earlier is passed into the          multithreadedtestrunner mttr  = new multithreadedtestrunner (TRS);                   //  develop concurrent execution of content defined in an array           mttr.runtestrunnables ();                       }         private class ThreadA extends TestRunnable {            @Override            Public void runtest ()  throws Throwable {               //  Test Content           &NBSP;&NBSP;&NBSP;MYCOMMMETHOD2 ();           }      }   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;VOID&NBSP;MYCOMMMETHOD2 ()  throws exception {           system.out.println ("= = ="  +  Thread.CurrentThread (). GetId ()  +  "Begin to execute mycommmethod2");           for  (int i = 0; i <10; i++)  {               int a   = 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.