2015-10-26 23 views
10

イム書くためTestCases私の私はJavaのカスタム注釈集約複数の注釈

@WebAppConfiguration 
@RunWith(value = SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = {WebConfig.class, TestAppConfig.class}) 

だから、私は自分の注釈の魔女はこの

のようにすべてのこれらの注釈が含まれて定義することを決定し、次のアノテーションを使用し、各 ControllerTest calssに対して RestControllers
@Target({ElementType.TYPE}) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
@Inherited 
@WebAppConfiguration 
@RunWith(value = SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = {WebConfig.class, TestAppConfig.class}) 
public @interface ControllerTest { 
} 

その後、私はすべてのControllerTest classes

つのだけのアノテーションを使用しました
@ControllerTest 
public class XXControllerTest { 
} 

は、この変更後のテストは

java.lang.IllegalArgumentException: WebApplicationContext is required 
    at org.springframework.util.Assert.notNull(Assert.java:115) 

に失敗し、それを動作させるために、再び、それは私の@ControllerTest注釈はdoesnの理由私の質問があるTest class

@ControllerTest 
@RunWith(SpringJUnit4ClassRunner.class) 
public class XXControllerTest { 
} 

@RunWith(SpringJUnit4ClassRunner.class)を追加するために私を必要とその中に@RunWith(SpringJUnit4ClassRunner.class)アノテーションが含まれていても動作しますか? @RunWithアノテーションについて特別なことはありますか?または私は何かが恋しいですか?

PS:私はSpring config classesと同じ方法を使用していますが、うまく動作します。

答えて

7

他の注釈で注釈された「メタ注釈」を持つことができるこのメカニズムは、メタ注釈を配置するクラスに適用され、Spring Frameworkに固有のものです。 Javaアノテーションの標準的な機能ではありません。

JUnitはこのメカニズムを理解していないため動作しません。 @RunWith注釈はJUnit注釈です。 JUnitは、あなたの@ControllerTestメタアノテーションにあるアノテーションを見なければならないと理解していません。

このメカニズムは、Springで処理されるアノテーションでは機能しますが、JUnitなどの他のツールで処理されるアノテーションでは機能しません。

2

meta-annotationsのスプリング注釈の作成はスプリング機能であり、@RunWithはJUnit注釈です。