类 SpringJUnit4ClassRunner
- java.lang.Object
- org.junit.runner.Runner
- org.junit.runners.ParentRunner<FrameworkMethod>
- org.junit.runners.BlockJUnit4ClassRunner
- org.springframework.test.context.junit4.SpringJUnit4ClassRunner
- 所有已实现的接口:
Describable,Filterable,Sortable
- 直接已知子类:
SpringRunner
public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner
SpringJUnit4ClassRunneris a custom extension of JUnit'sBlockJUnit4ClassRunnerwhich provides functionality of the Spring TestContext Framework to standard JUnit tests by means of theTestContextManagerand associated support classes and annotations.To use this class, simply annotate a JUnit 4 based test class with
@RunWith(SpringJUnit4ClassRunner.class)or@RunWith(SpringRunner.class).The following list constitutes all annotations currently supported directly or indirectly by
SpringJUnit4ClassRunner. (Note that additional annotations may be supported by variousTestExecutionListenerorTestContextBootstrapperimplementations.)@Test(expected=...)@Test(timeout=...)@Timed@Repeat@Ignore@ProfileValueSourceConfiguration@IfProfileValue
If you would like to use the Spring TestContext Framework with a runner other than this one, use
SpringClassRuleandSpringMethodRule.NOTE: As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
- 从以下版本开始:
- 2.5
- 作者:
- Sam Brannen, Juergen Hoeller
- 另请参阅:
SpringRunner,TestContextManager,AbstractJUnit4SpringContextTests,AbstractTransactionalJUnit4SpringContextTests,SpringClassRule,SpringMethodRule
构造器概要
构造器 构造器 说明 SpringJUnit4ClassRunner(Class<?> clazz)Construct a newSpringJUnit4ClassRunnerand initialize aTestContextManagerto provide Spring testing functionality to standard JUnit tests.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected ObjectcreateTest()Delegate to the parent implementation for creating the test instance and then allow theTestContextManagerto prepare the test instance before returning it.protected TestContextManagercreateTestContextManager(Class<?> clazz)Create a newTestContextManagerfor the supplied test class.DescriptiongetDescription()Return a description suitable for an ignored test class if the test is disabled via@IfProfileValueat the class-level, and otherwise delegate to the parent implementation.protected Class<? extends Throwable>getExpectedException(FrameworkMethod frameworkMethod)Get theexceptionthat the supplied test method is expected to throw.protected longgetJUnitTimeout(FrameworkMethod frameworkMethod)protected longgetSpringTimeout(FrameworkMethod frameworkMethod)Retrieve the configured Spring-specifictimeoutfrom the@Timedannotation on the supplied test method.protected TestContextManagergetTestContextManager()Get theTestContextManagerassociated with this runner.protected booleanisTestMethodIgnored(FrameworkMethod frameworkMethod)Returntrueif@Ignoreis present for the supplied test method or if the test method is disabled via@IfProfileValue.protected StatementmethodBlock(FrameworkMethod frameworkMethod)Augment the default JUnit behavior with potential repeats of the entire execution chain.protected StatementpossiblyExpectingExceptions(FrameworkMethod frameworkMethod, Object testInstance, Statement next)Perform the same logic asBlockJUnit4ClassRunner.possiblyExpectingExceptions(FrameworkMethod, Object, Statement)except that the expected exception is retrieved usinggetExpectedException(FrameworkMethod).voidrun(RunNotifier notifier)Check whether the test is enabled in the current execution environment.protected voidrunChild(FrameworkMethod frameworkMethod, RunNotifier notifier)Perform the same logic asBlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier), except that tests are determined to be ignored byisTestMethodIgnored(FrameworkMethod).protected StatementwithAfterClasses(Statement statement)Wrap theStatementreturned by the parent implementation with aRunAfterTestClassCallbacksstatement, thus preserving the default JUnit functionality while adding support for the Spring TestContext Framework.protected StatementwithAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement)Wrap theStatementreturned by the parent implementation with aRunAfterTestMethodCallbacksstatement, thus preserving the default functionality while adding support for the Spring TestContext Framework.protected StatementwithBeforeClasses(Statement statement)Wrap theStatementreturned by the parent implementation with aRunBeforeTestClassCallbacksstatement, thus preserving the default JUnit functionality while adding support for the Spring TestContext Framework.protected StatementwithBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement)Wrap theStatementreturned by the parent implementation with aRunBeforeTestMethodCallbacksstatement, thus preserving the default functionality while adding support for the Spring TestContext Framework.protected StatementwithPotentialRepeat(FrameworkMethod frameworkMethod, Object testInstance, Statement next)Wrap the suppliedStatementwith aSpringRepeatstatement.protected StatementwithPotentialTimeout(FrameworkMethod frameworkMethod, Object testInstance, Statement next)Perform the same logic asBlockJUnit4ClassRunner.withPotentialTimeout(FrameworkMethod, Object, Statement)but with additional support for Spring's@Timedannotation.从类继承的方法 org.junit.runners.BlockJUnit4ClassRunner
collectInitializationErrors, computeTestMethods, describeChild, getChildren, getTestRules, isIgnored, methodInvoker, rules, testName, validateConstructor, validateFields, validateInstanceMethods, validateNoNonStaticInnerClass, validateOnlyOneConstructor, validateTestMethods, validateZeroArgConstructor
从类继承的方法 org.junit.runners.ParentRunner
childrenInvoker, classBlock, classRules, createTestClass, filter, getName, getRunnerAnnotations, getTestClass, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods
构造器详细资料
SpringJUnit4ClassRunner
public SpringJUnit4ClassRunner(Class<?> clazz) throws InitializationError
Construct a newSpringJUnit4ClassRunnerand initialize aTestContextManagerto provide Spring testing functionality to standard JUnit tests.- 参数:
clazz- the test class to be run- 抛出:
InitializationError- 另请参阅:
createTestContextManager(Class)
方法详细资料
createTestContextManager
protected TestContextManager createTestContextManager(Class<?> clazz)
Create a newTestContextManagerfor the supplied test class.Can be overridden by subclasses.
- 参数:
clazz- the test class to be managed
getTestContextManager
protected final TestContextManager getTestContextManager()
Get theTestContextManagerassociated with this runner.
getDescription
public Description getDescription()
Return a description suitable for an ignored test class if the test is disabled via@IfProfileValueat the class-level, and otherwise delegate to the parent implementation.
run
public void run(RunNotifier notifier)
Check whether the test is enabled in the current execution environment.This prevents classes with a non-matching
@IfProfileValueannotation from running altogether, even skipping the execution ofprepareTestInstance()methods inTestExecutionListeners.
withBeforeClasses
protected Statement withBeforeClasses(Statement statement)
Wrap theStatementreturned by the parent implementation with aRunBeforeTestClassCallbacksstatement, thus preserving the default JUnit functionality while adding support for the Spring TestContext Framework.
withAfterClasses
protected Statement withAfterClasses(Statement statement)
Wrap theStatementreturned by the parent implementation with aRunAfterTestClassCallbacksstatement, thus preserving the default JUnit functionality while adding support for the Spring TestContext Framework.
createTest
protected Object createTest() throws Exception
Delegate to the parent implementation for creating the test instance and then allow theTestContextManagerto prepare the test instance before returning it.
runChild
protected void runChild(FrameworkMethod frameworkMethod, RunNotifier notifier)
Perform the same logic asBlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier), except that tests are determined to be ignored byisTestMethodIgnored(FrameworkMethod).- 覆盖:
runChild在类中BlockJUnit4ClassRunner
methodBlock
protected Statement methodBlock(FrameworkMethod frameworkMethod)
Augment the default JUnit behavior with potential repeats of the entire execution chain.Furthermore, support for timeouts has been moved down the execution chain in order to include execution of
@Beforeand@Aftermethods within the timed execution. Note that this differs from the default JUnit behavior of executing@Beforeand@Aftermethods in the main thread while executing the actual test method in a separate thread. Thus, the net effect is that@Beforeand@Aftermethods will be executed in the same thread as the test method. As a consequence, JUnit-specified timeouts will work fine in combination with Spring transactions. However, JUnit-specific timeouts still differ from Spring-specific timeouts in that the former execute in a separate thread while the latter simply execute in the main thread (like regular tests).- 覆盖:
methodBlock在类中BlockJUnit4ClassRunner- 另请参阅:
possiblyExpectingExceptions(FrameworkMethod, Object, Statement),withBefores(FrameworkMethod, Object, Statement),withAfters(FrameworkMethod, Object, Statement),withRulesReflectively(FrameworkMethod, Object, Statement),withPotentialRepeat(FrameworkMethod, Object, Statement),withPotentialTimeout(FrameworkMethod, Object, Statement)
isTestMethodIgnored
protected boolean isTestMethodIgnored(FrameworkMethod frameworkMethod)
Returntrueif@Ignoreis present for the supplied test method or if the test method is disabled via@IfProfileValue.
possiblyExpectingExceptions
protected Statement possiblyExpectingExceptions(FrameworkMethod frameworkMethod, Object testInstance, Statement next)
Perform the same logic asBlockJUnit4ClassRunner.possiblyExpectingExceptions(FrameworkMethod, Object, Statement)except that the expected exception is retrieved usinggetExpectedException(FrameworkMethod).
getExpectedException
protected Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod)
Get theexceptionthat the supplied test method is expected to throw.Supports JUnit's
@Test(expected=...)annotation.Can be overridden by subclasses.
- 返回:
- the expected exception, or
nullif none was specified
withPotentialTimeout
protected Statement withPotentialTimeout(FrameworkMethod frameworkMethod, Object testInstance, Statement next)
Perform the same logic asBlockJUnit4ClassRunner.withPotentialTimeout(FrameworkMethod, Object, Statement)but with additional support for Spring's@Timedannotation.Supports both Spring's
@Timedand JUnit's@Test(timeout=...)annotations, but not both simultaneously.- 覆盖:
withPotentialTimeout在类中BlockJUnit4ClassRunner- 返回:
- either a
SpringFailOnTimeout, aFailOnTimeout, or the suppliedStatementas appropriate - 另请参阅:
getSpringTimeout(FrameworkMethod),getJUnitTimeout(FrameworkMethod)
getJUnitTimeout
protected long getJUnitTimeout(FrameworkMethod frameworkMethod)
- 返回:
- the timeout, or
0if none was specified
getSpringTimeout
protected long getSpringTimeout(FrameworkMethod frameworkMethod)
Retrieve the configured Spring-specifictimeoutfrom the@Timedannotation on the supplied test method.- 返回:
- the timeout, or
0if none was specified - 另请参阅:
TestAnnotationUtils.getTimeout(Method)
withBefores
protected Statement withBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement)
Wrap theStatementreturned by the parent implementation with aRunBeforeTestMethodCallbacksstatement, thus preserving the default functionality while adding support for the Spring TestContext Framework.
withAfters
protected Statement withAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement)
Wrap theStatementreturned by the parent implementation with aRunAfterTestMethodCallbacksstatement, thus preserving the default functionality while adding support for the Spring TestContext Framework.
withPotentialRepeat
protected Statement withPotentialRepeat(FrameworkMethod frameworkMethod, Object testInstance, Statement next)