public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner
SpringJUnit4ClassRunner
は JUnit の BlockJUnit4ClassRunner
のカスタム拡張であり、TestContextManager
および関連するサポートクラスとアノテーションによって、Spring TestContext フレームワークの機能を標準の JUnit テストに提供します。 このクラスを使用するには、@RunWith(SpringJUnit4ClassRunner.class)
または @RunWith(SpringRunner.class)
で JUnit 4 ベースのテストクラスにアノテーションを付けます。
以下のリストは、現在 SpringJUnit4ClassRunner
によって直接または間接的にサポートされているすべてのアノテーションを構成しています。(追加のアノテーションは、さまざまな TestExecutionListener
または TestContextBootstrapper
実装によってサポートされる場合があることに注意してください)
@Test(expected=...)
@Test(timeout=...)
@Timed
@Repeat
@Ignore
@ProfileValueSourceConfiguration
@IfProfileValue
Spring TestContext フレームワークをこのランナー以外のランナーで使用する場合は、SpringClassRule
および SpringMethodRule
を使用します。
注意 : Spring Framework 4.3 以降、このクラスには JUnit 4.12 以上が必要です。
SpringRunner
, TestContextManager
, AbstractJUnit4SpringContextTests
, AbstractTransactionalJUnit4SpringContextTests
, SpringClassRule
, SpringMethodRule
コンストラクターと説明 |
---|
SpringJUnit4ClassRunner(ClassSE<?> clazz) |
修飾子と型 | メソッドと説明 |
---|---|
protected ObjectSE | createTest() テストインスタンスを作成するために親実装に委譲し、 TestContextManager がテストインスタンスを準備してから返すようにします。 |
protected TestContextManager | createTestContextManager(ClassSE<?> clazz) 提供されたテストクラスの新しい TestContextManager を作成します。 |
Description | getDescription() テストがクラスレベルで @IfProfileValue を介して無効になっている場合、無視されたテストクラスに適した説明を返します。それ以外の場合は、親実装に委譲します。 |
protected ClassSE<? extends ThrowableSE> | getExpectedException(FrameworkMethod frameworkMethod) 提供されたテストメソッドがスローすると予想される exception を取得します。 |
protected long | getJUnitTimeout(FrameworkMethod frameworkMethod) |
protected long | getSpringTimeout(FrameworkMethod frameworkMethod) |
protected TestContextManager | getTestContextManager() このランナーに関連付けられている TestContextManager を取得します。 |
protected boolean | isTestMethodIgnored(FrameworkMethod frameworkMethod) |
protected Statement | methodBlock(FrameworkMethod frameworkMethod) 実行チェーン全体の潜在的な繰り返しで、デフォルトの JUnit 動作を強化します。 |
protected Statement | possiblyExpectingExceptions(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement next) getExpectedException(FrameworkMethod) を使用して予期される例外が取得されることを除いて、BlockJUnit4ClassRunner.possiblyExpectingExceptions(FrameworkMethod, Object, Statement) と同じロジックを実行します。 |
void | run(RunNotifier notifier) 現在の実行環境でテストが有効になっているかどうかを確認します。 |
protected void | runChild(FrameworkMethod frameworkMethod, RunNotifier notifier) テストが isTestMethodIgnored(FrameworkMethod) によって無視されると判断されることを除いて、BlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier) と同じロジックを実行します。 |
protected Statement | withAfterClasses(Statement statement) 親実装によって返された Statement を RunAfterTestClassCallbacks ステートメントでラップし、デフォルトの JUnit 機能を保持しながら、Spring TestContext フレームワークのサポートを追加します。 |
protected Statement | withAfters(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement statement) 親実装によって返された Statement を RunAfterTestMethodCallbacks ステートメントでラップします。これにより、Spring TestContext フレームワークのサポートを追加しながら、デフォルトの機能を保持します。 |
protected Statement | withAfterTestExecutionCallbacks(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement statement) 提供された Statement を RunAfterTestExecutionCallbacks ステートメントでラップし、Spring TestContext フレームワークのサポートを追加しながら、デフォルトの機能を保持します。 |
protected Statement | withBeforeClasses(Statement statement) 親実装によって返された Statement を RunBeforeTestClassCallbacks ステートメントでラップし、デフォルトの JUnit 機能を保持しながら、Spring TestContext フレームワークのサポートを追加します。 |
protected Statement | withBefores(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement statement) 親実装によって返された Statement を RunBeforeTestMethodCallbacks ステートメントでラップします。これにより、Spring TestContext フレームワークのサポートを追加しながら、デフォルトの機能を保持します。 |
protected Statement | withBeforeTestExecutionCallbacks(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement statement) 提供された Statement を RunBeforeTestExecutionCallbacks ステートメントでラップし、Spring TestContext フレームワークのサポートを追加しながら、デフォルトの機能を保持します。 |
protected Statement | withPotentialRepeat(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement next) 提供された Statement を SpringRepeat ステートメントでラップします。 |
protected Statement | withPotentialTimeout(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement next) BlockJUnit4ClassRunner.withPotentialTimeout(FrameworkMethod, Object, Statement) と同じロジックを実行しますが、Spring の @Timed アノテーションを追加サポートします。 |
collectInitializationErrors, computeTestMethods, createTest, describeChild, getChildren, getTestRules, isIgnored, methodInvoker, rules, testName, validateConstructor, validateFields, validateInstanceMethods, validateNoNonStaticInnerClass, validateOnlyOneConstructor, validateTestMethods, validateZeroArgConstructor
childrenInvoker, classBlock, classRules, createTestClass, filter, getName, getRunnerAnnotations, getTestClass, order, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods, withInterruptIsolation
cloneSE, equalsSE, finalizeSE, getClassSE, hashCodeSE, notifySE, notifyAllSE, toStringSE, waitSE, waitSE, waitSE
public SpringJUnit4ClassRunner(ClassSE<?> clazz) throws InitializationError
SpringJUnit4ClassRunner
を作成し、TestContextManager
を初期化して、Spring Test 機能を標準の JUnit テストに提供します。clazz
- 実行するテストクラス InitializationError
createTestContextManager(Class)
protected TestContextManager createTestContextManager(ClassSE<?> clazz)
TestContextManager
を作成します。サブクラスでオーバーライドできます。
clazz
- 管理するテストクラス protected final TestContextManager getTestContextManager()
TestContextManager
を取得します。public Description getDescription()
@IfProfileValue
を介して無効になっている場合、無視されたテストクラスに適した説明を返します。それ以外の場合は、親実装に委譲します。Describable
の getDescription
ParentRunner<FrameworkMethod>
の getDescription
ProfileValueUtils.isTestEnabledInThisEnvironment(Class)
public void run(RunNotifier notifier)
これにより、一致しない @IfProfileValue
アノテーションを持つクラスが完全に実行されなくなり、TestExecutionListeners
の prepareTestInstance()
メソッドの実行がスキップされます。
protected Statement withBeforeClasses(Statement statement)
Statement
を RunBeforeTestClassCallbacks
ステートメントでラップし、デフォルトの JUnit 機能を保持しながら、Spring TestContext フレームワークのサポートを追加します。ParentRunner<FrameworkMethod>
の withBeforeClasses
RunBeforeTestClassCallbacks
protected Statement withAfterClasses(Statement statement)
Statement
を RunAfterTestClassCallbacks
ステートメントでラップし、デフォルトの JUnit 機能を保持しながら、Spring TestContext フレームワークのサポートを追加します。ParentRunner<FrameworkMethod>
の withAfterClasses
RunAfterTestClassCallbacks
protected ObjectSE createTest() throws ExceptionSE
TestContextManager
がテストインスタンスを準備してから返すようにします。BlockJUnit4ClassRunner
の createTest
ExceptionSE
TestContextManager.prepareTestInstance(java.lang.Object)
protected void runChild(FrameworkMethod frameworkMethod, RunNotifier notifier)
isTestMethodIgnored(FrameworkMethod)
によって無視されると判断されることを除いて、BlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier)
と同じロジックを実行します。BlockJUnit4ClassRunner
の runChild
protected Statement methodBlock(FrameworkMethod frameworkMethod)
さらに、タイムアウトのサポートは、@Before
および @After
メソッドの実行を時限実行に含めるために、実行チェーンに移動しました。これは、実際のテストメソッドを別のスレッドで実行しながらメインスレッドで @Before
および @After
メソッドを実行するデフォルトの JUnit 動作とは異なることに注意してください。正味の効果は、@Before
および @After
メソッドがテストメソッドと同じスレッドで実行されることです。結果として、JUnit で指定されたタイムアウトは、Spring トランザクションと組み合わせて正常に機能します。ただし、JUnit 固有のタイムアウトは、Spring 固有のタイムアウトとは異なり、前者は別のスレッドで実行されますが、後者は(通常のテストのように)メインスレッドで実行されるだけです。
BlockJUnit4ClassRunner
の methodBlock
BlockJUnit4ClassRunner.methodInvoker(FrameworkMethod, Object)
, withBeforeTestExecutionCallbacks(FrameworkMethod, Object, Statement)
, withAfterTestExecutionCallbacks(FrameworkMethod, Object, Statement)
, possiblyExpectingExceptions(FrameworkMethod, Object, Statement)
, withBefores(FrameworkMethod, Object, Statement)
, withAfters(FrameworkMethod, Object, Statement)
, withRulesReflectively(FrameworkMethod, Object, Statement)
, withPotentialRepeat(FrameworkMethod, Object, Statement)
, withPotentialTimeout(FrameworkMethod, Object, Statement)
protected boolean isTestMethodIgnored(FrameworkMethod frameworkMethod)
protected Statement possiblyExpectingExceptions(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement next)
getExpectedException(FrameworkMethod)
を使用して予期される例外が取得されることを除いて、BlockJUnit4ClassRunner.possiblyExpectingExceptions(FrameworkMethod, Object, Statement)
と同じロジックを実行します。BlockJUnit4ClassRunner
の possiblyExpectingExceptions
@Nullable protected ClassSE<? extends ThrowableSE> getExpectedException(FrameworkMethod frameworkMethod)
exception
を取得します。JUnit の @Test(expected=...)
アノテーションをサポートします。
サブクラスでオーバーライドできます。
null
protected Statement withPotentialTimeout(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement next)
BlockJUnit4ClassRunner.withPotentialTimeout(FrameworkMethod, Object, Statement)
と同じロジックを実行しますが、Spring の @Timed
アノテーションを追加サポートします。Spring の @Timed
アノテーションと JUnit の @Test(timeout=...)
アノテーションの両方をサポートしますが、両方を同時にサポートすることはできません。
BlockJUnit4ClassRunner
の withPotentialTimeout
SpringFailOnTimeout
、FailOnTimeout
、提供された Statement
のいずれかを必要に応じて getSpringTimeout(FrameworkMethod)
, getJUnitTimeout(FrameworkMethod)
protected long getJUnitTimeout(FrameworkMethod frameworkMethod)
0
protected long getSpringTimeout(FrameworkMethod frameworkMethod)
0
TestAnnotationUtils.getTimeout(Method)
protected Statement withBeforeTestExecutionCallbacks(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement statement)
Statement
を RunBeforeTestExecutionCallbacks
ステートメントでラップし、Spring TestContext フレームワークのサポートを追加しながら、デフォルトの機能を保持します。protected Statement withAfterTestExecutionCallbacks(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement statement)
Statement
を RunAfterTestExecutionCallbacks
ステートメントでラップし、Spring TestContext フレームワークのサポートを追加しながら、デフォルトの機能を保持します。protected Statement withBefores(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement statement)
Statement
を RunBeforeTestMethodCallbacks
ステートメントでラップします。これにより、Spring TestContext フレームワークのサポートを追加しながら、デフォルトの機能を保持します。BlockJUnit4ClassRunner
の withBefores
RunBeforeTestMethodCallbacks
protected Statement withAfters(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement statement)
Statement
を RunAfterTestMethodCallbacks
ステートメントでラップします。これにより、Spring TestContext フレームワークのサポートを追加しながら、デフォルトの機能を保持します。BlockJUnit4ClassRunner
の withAfters
RunAfterTestMethodCallbacks
protected Statement withPotentialRepeat(FrameworkMethod frameworkMethod, ObjectSE testInstance, Statement next)