com.google.testing.threadtester
Class BaseThreadedTestRunner

java.lang.Object
  extended by com.google.testing.threadtester.BaseThreadedTestRunner
Direct Known Subclasses:
AnnotatedTestRunner, ThreadedTestRunner

public abstract class BaseThreadedTestRunner
extends Object

Runs a set of multithreaded tests. This is an abstract base class that defines the basic framework for running tests. Subclasses will add specific mechanisms for defining tests.

To create a multithreaded test, define a class with a public no-arg constructor, and a set of public instance methods that run the actual test cases. These methods typically use custom annotations to identify them. To run the tests, use the runTests(Class, Class...) method in one of the regular unit test cases. For example, using JUnit 4 syntax:

 public class MyClassTest {

   public MyClassTest() {}

   // This method is invoked as part of the regular unit test
   @Test
   public void runThreadedTests {
     new BaseThreadedTestRunner().runTests(MyClassTest.class, MyClass.class);
   }

   // This method is invoked by the BaseThreadedTestRunner. It should not be
   // invoked as part of the regular unit test.
   @SomeTestAnnotation
   public void testThreading {
     ...
   }

 
Note that when a test is run via the BaseThreadedTestRunner, the test targets will have been instrumented, allowing Breakpoints to be created. For this reason, the threaded tests cannot be run directly from the test harness, as the test targets will not have been correctly instrumented. Internally, the test framework reloads the clases using an instrumenting classloader.

Author:
alasdair.mackintosh@gmail.com (Alasdair Mackintosh)
See Also:
Instrumentation

Constructor Summary
BaseThreadedTestRunner()
           
 
Method Summary
protected abstract  String getWrapperName()
          Gets the name of the wrapper class that runs the test.
 boolean inThreadedTest()
          Returns true when called within a multithreaded test that has been executed via a subclass of this class.
 void runTests(Class<?> test, Class<?>... targets)
          Run the multithreaded tests defined in a given class.
 void runTests(Class<?> tester, List<Class<?>> targets)
          Run the multithreaded tests defined in a given class.
 void setDebug(boolean newDebug)
          Sets the debug mode.
 void setTimeout(long newTimeout)
          Sets the timeout value (in milliseconds) used for all internal waits and joins.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BaseThreadedTestRunner

public BaseThreadedTestRunner()
Method Detail

setDebug

public void setDebug(boolean newDebug)
Sets the debug mode. (The default is false.) If true, then additional debugging information is printed to stdout. Useful for debugging tests that fail.


setTimeout

public void setTimeout(long newTimeout)
Sets the timeout value (in milliseconds) used for all internal waits and joins. Any thread that takes longer than this time to terminate, or to reach a break point will trigger an exception (and hence a test failure.)


inThreadedTest

public boolean inThreadedTest()
Returns true when called within a multithreaded test that has been executed via a subclass of this class. Returns false otherwise.


runTests

public void runTests(Class<?> test,
                     Class<?>... targets)
Run the multithreaded tests defined in a given class. This method creates a new instance of the given class, and invokes the relevant test methods.

A multithreaded test will normally require at least one class to be instrumented. To specify a single such class, use this method. To specify multiple classes, use runTests(Class, List).

Parameters:
test - the class containing a set of multithreaded test methods.
targets - the classes which should be instrumented.
Throws:
IllegalArgumentException - if the test class cannot be loaded, or its test methods cannot be invoked. Will also throw any RuntimeExceptions thrown by the invoked test methods.
See Also:
Instrumentation, ClassInstrumentation

runTests

public final void runTests(Class<?> tester,
                           List<Class<?>> targets)
Run the multithreaded tests defined in a given class. This method creates a new instance of the given class, and invokes the relevant test methods.

Parameters:
tester - the class containing a set of multithreaded test methods.
targets - the classes which should be instrumented.
Throws:
IllegalArgumentException - if the test class cannot be loaded, or its test methods cannot be invoked. Will also throw any RuntimeExceptions thrown by the invoked test methods.
See Also:
Instrumentation, ClassInstrumentation

getWrapperName

protected abstract String getWrapperName()
Gets the name of the wrapper class that runs the test. The named class should be a subclass of BaseTestWrapper.

This method is overridden by concrete implementations. An instance of the wrapper class will be created when runTests(java.lang.Class, java.lang.Class...) is invoked, and the BaseTestWrapper.runTests(java.lang.Class, java.util.List) method will be invoked.



Copyright © 2013. All Rights Reserved.