高レベル、私は非常に簡単な JUnit テスト クラスを持っています。いくつかの @Tests と、いくつかのセットアップを行う単一の @Before があります。1 つのテスト ケースでは、セットアップが異なります (実行したくありません)。
いくつかの検索から、https://stackoverflow.com/a/13017452/413254を見つけました。これは、特定のアノテーションをチェックして @Before ステートメントを実行する @Rule を作成することを提案します。
私の混乱は、ルールで @Before メソッドを実行する方法に関するものです。それを行う方法はありますか?または、テスト クラス自体を渡して @before メソッドを実行する必要がありsetup()
ますか (以下の例)。
public class NoBeforeRule implements TestRule {
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (description.getAnnotation(NoBefore.class) == null) {
// Is there something like `base.executeAllTheBefores()'
}
base.evaluate();
}
};
}
}
関連するテスト コード:
@Rule public NoBeforeRule mNoBeforeRule = new NoBeforeRule(this);
@Before
@Override
public void setup() {
}
@Test
public void testNeedSetup() {
// this should run setup()
}
@NoBefore
@Test
public void testNoSetup() {
// this should NOT run setup()
}