public class LoadTimeWeavableTestRunner extends org.junit.runner.Runner implements org.junit.runner.manipulation.Filterable, org.junit.runner.manipulation.Sortable
Runner
which uses a custom classloader with a copy of the classpath and allows
for transformers to be added to the ClassLoader for load-time weaving.
Useful when writing tests that use JPA with EclipseLink since it depends upon load-time weaving.
In order to use this class, you must have a BootstrapTest
annotation available somewhere in the hierarchy
of your test class (usually on the same class where the RunWith
annotation is specified which references this
runner class). This informs the runner about a test that it can run to execute any one-time initialization for
the test suite. Ideally, this bootstrap test will execute code which loads JPA persistence units and any associated
ClassFileTransformers for load-time weaving. This is necessary because it is common for an integration test to have
references in the test class itself to JPA entities which need to be weaved. When this occurs, if the persistence
units and ClassFileTransformers are not properly loaded before the entity classes are loaded by the classloader, then
instrumentation will (silently!) fail to occur.
Much of the code in this class was copied from the JUnit ParentRunner, BlockJUnit4ClassRunner, and TomcatInstrumentableClassLoader.
Constructor and Description |
---|
LoadTimeWeavableTestRunner(Class<?> testClass)
Constructs a new
ParentRunner that will run @TestClass |
Modifier and Type | Method and Description |
---|---|
protected org.junit.runners.model.Statement |
childrenInvoker(org.junit.runner.notification.RunNotifier notifier)
Returns a
Statement : Call runChild(org.junit.runners.model.FrameworkMethod, org.junit.runner.notification.RunNotifier)
on each object returned by getChildren() (subject to any imposed
filter and sort) |
protected org.junit.runners.model.Statement |
classBlock(org.junit.runner.notification.RunNotifier notifier)
Constructs a
Statement to run all of the tests in the test class. |
protected List<org.junit.rules.TestRule> |
classRules() |
protected void |
collectInitializationErrors(List<Throwable> errors)
Adds to
errors a throwable for each problem noted with the test class (available from getTestClass() ). |
protected List<org.junit.runners.model.FrameworkMethod> |
computeOriginalTestMethods() |
protected List<org.junit.runners.model.FrameworkMethod> |
computeTestMethods()
Returns the methods that run tests.
|
protected Object |
createTest()
Returns a new fixture for running a test.
|
protected org.junit.runner.Description |
describeChild(org.junit.runners.model.FrameworkMethod method)
Returns a
Description for child , which can be assumed to
be an element of the list returned by getChildren() |
protected org.junit.runner.Description |
describeOriginalChild(org.junit.runners.model.FrameworkMethod method) |
void |
filter(org.junit.runner.manipulation.Filter filter) |
protected List<org.junit.runners.model.FrameworkMethod> |
getChildren()
Returns a list of objects that define the children of this Runner.
|
protected ClassLoader |
getCustomClassLoader() |
org.junit.runner.Description |
getDescription() |
protected String |
getName()
Returns a name used to describe this Runner
|
protected List<org.junit.runners.model.FrameworkMethod> |
getOriginalChildren() |
org.junit.runners.model.TestClass |
getOriginalTestClass()
Returns the original test class that was passed to this test runner.
|
protected Annotation[] |
getRunnerAnnotations() |
org.junit.runners.model.TestClass |
getTestClass()
Returns a
TestClass object wrapping the class to be executed. |
protected List<org.junit.rules.TestRule> |
getTestRules(Object target) |
protected org.junit.runners.model.Statement |
methodBlock(org.junit.runners.model.FrameworkMethod method)
Returns a Statement that, when executed, either returns normally if
method passes, or throws an exception if method fails. |
protected org.junit.runners.model.Statement |
methodInvoker(org.junit.runners.model.FrameworkMethod method,
Object test)
Returns a
Statement that invokes method on test |
protected org.junit.runners.model.Statement |
possiblyExpectingExceptions(org.junit.runners.model.FrameworkMethod method,
Object test,
org.junit.runners.model.Statement next)
Deprecated.
Will be private soon: use Rules instead
|
protected List<org.junit.rules.MethodRule> |
rules(Object target) |
void |
run(org.junit.runner.notification.RunNotifier notifier) |
protected boolean |
runBootstrapTest(org.junit.runner.notification.RunNotifier notifier,
org.junit.runners.model.TestClass testClass) |
protected void |
runChild(org.junit.runners.model.FrameworkMethod method,
org.junit.runner.notification.RunNotifier notifier)
Runs the test corresponding to
child , which can be assumed to be
an element of the list returned by getChildren() . |
protected void |
runLeaf(org.junit.runners.model.Statement statement,
org.junit.runner.Description description,
org.junit.runner.notification.RunNotifier notifier)
Runs a
Statement that represents a leaf (aka atomic) test. |
protected void |
setTestMethod(Object test,
Method method)
Sets the
Method on the test case if it is MethodAware |
protected void |
setTestName(Object test,
Method testMethod) |
void |
sort(org.junit.runner.manipulation.Sorter sorter) |
protected String |
testName(org.junit.runners.model.FrameworkMethod method)
Returns the name that describes
method for Description s. |
protected void |
validateConstructor(List<Throwable> errors)
Adds to
errors if the test class has more than one constructor,
or if the constructor takes parameters. |
protected void |
validateFields(List<Throwable> errors) |
protected void |
validateInstanceMethods(List<Throwable> errors)
Deprecated.
unused API, will go away in future version
|
protected void |
validateNoNonStaticInnerClass(List<Throwable> errors) |
protected void |
validateOnlyOneConstructor(List<Throwable> errors)
Adds to
errors if the test class has more than one constructor
(do not override) |
protected void |
validatePublicVoidNoArgMethods(Class<? extends Annotation> annotation,
boolean isStatic,
List<Throwable> errors)
Adds to
errors if any method in this class is annotated with
annotation , but:
is not public, or
takes parameters, or
returns something other than void, or
is static (given isStatic is false ), or
is not static (given isStatic is true ). |
protected void |
validateTestMethods(List<Throwable> errors)
Adds to
errors for each method annotated with @Test that
is not a public, void instance method with no arguments. |
protected void |
validateZeroArgConstructor(List<Throwable> errors)
Adds to
errors if the test class's single constructor takes
parameters (do not override) |
protected org.junit.runners.model.Statement |
withAfterClasses(org.junit.runners.model.Statement statement)
Returns a
Statement : run all non-overridden @AfterClass methods on this class
and superclasses before executing statement ; all AfterClass methods are
always executed: exceptions thrown by previous steps are combined, if
necessary, with exceptions from AfterClass methods into a
MultipleFailureException . |
protected org.junit.runners.model.Statement |
withAfters(org.junit.runners.model.FrameworkMethod method,
Object target,
org.junit.runners.model.Statement statement)
Deprecated.
Will be private soon: use Rules instead
|
protected org.junit.runners.model.Statement |
withBeforeClasses(org.junit.runners.model.Statement statement)
Returns a
Statement : run all non-overridden @BeforeClass methods on this class
and superclasses before executing statement ; if any throws an
Exception, stop execution and pass the exception on. |
protected org.junit.runners.model.Statement |
withBefores(org.junit.runners.model.FrameworkMethod method,
Object target,
org.junit.runners.model.Statement statement)
Deprecated.
Will be private soon: use Rules instead
|
protected org.junit.runners.model.Statement |
withPotentialTimeout(org.junit.runners.model.FrameworkMethod method,
Object test,
org.junit.runners.model.Statement next)
Deprecated.
Will be private soon: use Rules instead
|
public LoadTimeWeavableTestRunner(Class<?> testClass) throws org.junit.runners.model.InitializationError
ParentRunner
that will run @TestClass
org.junit.runners.model.InitializationError
protected ClassLoader getCustomClassLoader()
protected void validatePublicVoidNoArgMethods(Class<? extends Annotation> annotation, boolean isStatic, List<Throwable> errors)
errors
if any method in this class is annotated with
annotation
, but:
isStatic is false
), or
isStatic is true
).protected org.junit.runners.model.Statement classBlock(org.junit.runner.notification.RunNotifier notifier)
Statement
to run all of the tests in the test class. Override to add pre-/post-processing.
Here is an outline of the implementation:
runChild(org.junit.runners.model.FrameworkMethod, org.junit.runner.notification.RunNotifier)
on each object returned by getChildren()
(subject to any imposed filter and sort).@BeforeClass
methods on this class
and superclasses before the previous step; if any throws an
Exception, stop execution and pass the exception on.
@AfterClass
methods on this class
and superclasses before any of the previous steps; all AfterClass methods are
always executed: exceptions thrown by previous steps are combined, if
necessary, with exceptions from AfterClass methods into a
MultipleFailureException
.
Statement
protected org.junit.runners.model.Statement withBeforeClasses(org.junit.runners.model.Statement statement)
Statement
: run all non-overridden @BeforeClass
methods on this class
and superclasses before executing statement
; if any throws an
Exception, stop execution and pass the exception on.protected org.junit.runners.model.Statement withAfterClasses(org.junit.runners.model.Statement statement)
Statement
: run all non-overridden @AfterClass
methods on this class
and superclasses before executing statement
; all AfterClass methods are
always executed: exceptions thrown by previous steps are combined, if
necessary, with exceptions from AfterClass methods into a
MultipleFailureException
.protected List<org.junit.rules.TestRule> classRules()
ClassRule
s that can transform the block that runs
each method in the tested class.protected org.junit.runners.model.Statement childrenInvoker(org.junit.runner.notification.RunNotifier notifier)
Statement
: Call runChild(org.junit.runners.model.FrameworkMethod, org.junit.runner.notification.RunNotifier)
on each object returned by getChildren()
(subject to any imposed
filter and sort)public final org.junit.runners.model.TestClass getTestClass()
TestClass
object wrapping the class to be executed.public final org.junit.runners.model.TestClass getOriginalTestClass()
protected final void runLeaf(org.junit.runners.model.Statement statement, org.junit.runner.Description description, org.junit.runner.notification.RunNotifier notifier)
Statement
that represents a leaf (aka atomic) test.protected Annotation[] getRunnerAnnotations()
public org.junit.runner.Description getDescription()
getDescription
in interface org.junit.runner.Describable
getDescription
in class org.junit.runner.Runner
public void run(org.junit.runner.notification.RunNotifier notifier)
run
in class org.junit.runner.Runner
protected boolean runBootstrapTest(org.junit.runner.notification.RunNotifier notifier, org.junit.runners.model.TestClass testClass)
public void filter(org.junit.runner.manipulation.Filter filter) throws org.junit.runner.manipulation.NoTestsRemainException
filter
in interface org.junit.runner.manipulation.Filterable
org.junit.runner.manipulation.NoTestsRemainException
public void sort(org.junit.runner.manipulation.Sorter sorter)
sort
in interface org.junit.runner.manipulation.Sortable
protected void runChild(org.junit.runners.model.FrameworkMethod method, org.junit.runner.notification.RunNotifier notifier)
child
, which can be assumed to be
an element of the list returned by getChildren()
.
Subclasses are responsible for making sure that relevant test events are
reported through notifier
protected org.junit.runner.Description describeChild(org.junit.runners.model.FrameworkMethod method)
Description
for child
, which can be assumed to
be an element of the list returned by getChildren()
protected org.junit.runner.Description describeOriginalChild(org.junit.runners.model.FrameworkMethod method)
protected List<org.junit.runners.model.FrameworkMethod> getChildren()
protected List<org.junit.runners.model.FrameworkMethod> getOriginalChildren()
protected List<org.junit.runners.model.FrameworkMethod> computeTestMethods()
@Test
on this class and superclasses that
are not overridden.protected List<org.junit.runners.model.FrameworkMethod> computeOriginalTestMethods()
protected void collectInitializationErrors(List<Throwable> errors)
errors
a throwable for each problem noted with the test class (available from getTestClass()
).
Default implementation adds an error for each method annotated with
@BeforeClass
or @AfterClass
that is not
public static void
with no arguments.protected void validateNoNonStaticInnerClass(List<Throwable> errors)
protected void validateConstructor(List<Throwable> errors)
errors
if the test class has more than one constructor,
or if the constructor takes parameters. Override if a subclass requires
different validation rules.protected void validateOnlyOneConstructor(List<Throwable> errors)
errors
if the test class has more than one constructor
(do not override)protected void validateZeroArgConstructor(List<Throwable> errors)
errors
if the test class's single constructor takes
parameters (do not override)@Deprecated protected void validateInstanceMethods(List<Throwable> errors)
errors
for each method annotated with @Test
,
@Before
, or @After
that is not a public, void instance
method with no arguments.protected void validateFields(List<Throwable> errors)
protected void validateTestMethods(List<Throwable> errors)
errors
for each method annotated with @Test
that
is not a public, void instance method with no arguments.protected Object createTest() throws Exception
Exception
protected void setTestMethod(Object test, Method method) throws Exception
Method
on the test case if it is MethodAware
method
- the current method to be runtest
- the test instanceException
protected void setTestName(Object test, Method testMethod) throws Exception
Exception
protected String testName(org.junit.runners.model.FrameworkMethod method)
method
for Description
s.
Default implementation is the method's nameprotected org.junit.runners.model.Statement methodBlock(org.junit.runners.model.FrameworkMethod method)
method
passes, or throws an exception if method
fails.
Here is an outline of the default implementation:
method
on the result of createTest()
, and
throw any exceptions thrown by either operation.
method
's @Test
annotation has the expecting
attribute, return normally only if the previous step threw an
exception of the correct type, and throw an exception otherwise.
method
's @Test
annotation has the timeout
attribute, throw an exception if the previous step takes more
than the specified number of milliseconds.
@Before
methods on this class
and superclasses before any of the previous steps; if any throws an
Exception, stop execution and pass the exception on.
@After
methods on this class
and superclasses after any of the previous steps; all After methods are
always executed: exceptions thrown by previous steps are combined, if
necessary, with exceptions from After methods into a
MultipleFailureException
.
@Rule
fields to modify the execution of the
above steps. A Rule
may prevent all execution of the above steps,
or add additional behavior before and after, or modify thrown exceptions.
For more information, see TestRule
protected org.junit.runners.model.Statement methodInvoker(org.junit.runners.model.FrameworkMethod method, Object test)
Statement
that invokes method
on test
@Deprecated protected org.junit.runners.model.Statement possiblyExpectingExceptions(org.junit.runners.model.FrameworkMethod method, Object test, org.junit.runners.model.Statement next)
Statement
: if method
's @Test
annotation
has the expecting
attribute, return normally only if next
throws an exception of the correct type, and throw an exception
otherwise.@Deprecated protected org.junit.runners.model.Statement withPotentialTimeout(org.junit.runners.model.FrameworkMethod method, Object test, org.junit.runners.model.Statement next)
Statement
: if method
's @Test
annotation
has the timeout
attribute, throw an exception if next
takes more than the specified number of milliseconds.@Deprecated protected org.junit.runners.model.Statement withBefores(org.junit.runners.model.FrameworkMethod method, Object target, org.junit.runners.model.Statement statement)
Statement
: run all non-overridden @Before
methods on this class and superclasses before running next
; if
any throws an Exception, stop execution and pass the exception on.@Deprecated protected org.junit.runners.model.Statement withAfters(org.junit.runners.model.FrameworkMethod method, Object target, org.junit.runners.model.Statement statement)
Statement
: run all non-overridden @After
methods on this class and superclasses before running next
; all
After methods are always executed: exceptions thrown by previous steps
are combined, if necessary, with exceptions from After methods into a
MultipleFailureException
.protected List<org.junit.rules.MethodRule> rules(Object target)
target
- the test case instanceprotected List<org.junit.rules.TestRule> getTestRules(Object target)
target
- the test case instanceCopyright © 2005–2016 The Kuali Foundation. All rights reserved.