Spring Junit Supportで実行されるテストケースが多数あり、各テストに次のアノテーションが付いています。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext.xml")
@TransactionConfiguration(transactionManager="transactionManager")
@Transactional
@ActiveProfiles("test")
これらすべてのアノテーションを各テストクラスに配置する代わりに、カスタムアノテーションを作成して使用したいと思います。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext.xml")
@TransactionConfiguration(transactionManager="transactionManager")
@Transactional
@ActiveProfiles("test")
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface SpringJUnit4TestConfig {
}
しかし、このカスタムアノテーションを使用すると、SpringInjectionはまったく発生しません。
@SpringJUnit4TestConfig
public class UserServiceTest
{
}
私がここで欠けているものは何ですか?
PS:しかし、JUnitの@RunWithとSpringの@Transactional、@ContextConfigurationはすべて@Explicit..なので、動作するはずだと思いました。しかし今のところ、私は回避策を通してそれを乗り越えます。Based Abstractクラスを作成し、それらすべてのアノテーションとそのBaseクラスを拡張するテストケースを配置しました。