com.google.testing.threadtester
Class AnnotatedTestRunner

java.lang.Object
  extended by com.google.testing.threadtester.BaseThreadedTestRunner
      extended by com.google.testing.threadtester.AnnotatedTestRunner

public class AnnotatedTestRunner
extends BaseThreadedTestRunner

Runs a set of multithreaded tests defined by a series of annotations. To create a set of tests, define a class with a public no-arg constructor, and a set of publically annotated methods. A typical test setup would be:

 public class MyClassTest extends TestCase {
   public MyClassTest() {}

   private MyClass myClass;

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

   @ThreadedBefore
   public void before() {
     myClass = new MyClass();
   }

   @ThreadedAfter
   public void after() {
     assertEquals("Hello World", myClass.getMessage());
   }

   @ThreadedMain(name="TestOne")
   public void testPrint() {
     myClass.printMessage("Hello");
   }

   @ThreadedSecondary(name="TestOne")
   public void testPrint2() {
     myClass.printMessage("World");
   }
 }
 
The runner will run every test case defined in the test class. A test case consists of an initialisation method with the @ThreadedBefore annotation, and a pair of execution methods, one with the ThreadedMain annotation, and one with the ThreadedSecondary. The execution annotations should have the same name. The test runner will run each pair of annotated methods in two separate threads, interleaving them to verify that the invoked calls are thread safe.

Before invoking each method pair, the runner will invoke the method with the ThreadedBefore annotation. This method must create a new instance of the object being tested. Additional setup code common the all of the tests can also be added here. Note that only a single instance of the object under test can be created.

After invoking each method pair, the ThreadedAfter method will be invoked. This can be used to verify results, and to free resources.

A test case may also contain an optional third method tagged with the ThreadedVerification attribute. This will be invoked after the named test case has been run, and can be used to verify that the particular case has run successfully.

The sequence for a test case named "test1" is thus:

The interleaving of the main and secondary methods is handled automatically by the test framework. It will analyse the @ThreadedMain method to find the first call to a method in the class-under-test. For each executable line in the target method it will then:

Finally, the entire test class may define one static method with the ThreadedBeforeAll annotation, and one static method with the ThreadedAfterAll. These methods are invoked before and after all of the test cases have been run.

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

Constructor Summary
AnnotatedTestRunner()
           
 
Method Summary
protected  String getWrapperName()
          Gets the name of the wrapper class that runs the test.
 
Methods inherited from class com.google.testing.threadtester.BaseThreadedTestRunner
inThreadedTest, runTests, runTests, setDebug, setTimeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AnnotatedTestRunner

public AnnotatedTestRunner()
Method Detail

getWrapperName

protected String getWrapperName()
Description copied from class: BaseThreadedTestRunner
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 BaseThreadedTestRunner.runTests(java.lang.Class, java.lang.Class...) is invoked, and the BaseTestWrapper.runTests(java.lang.Class, java.util.List) method will be invoked.

Specified by:
getWrapperName in class BaseThreadedTestRunner


Copyright © 2013. All Rights Reserved.