35

サブクラスに継承されるテスト実行リスナーを使用して、spring、logging、jndiなどの一般的な構成をセットアップするいくつかの基本テストクラスがあります。これは、テストコードを実行する前に、jndiやロギングサービスを配置することを心配することなく、テストでコードを実行できるようにするためです。

Intellijを使用し、プロジェクトベースから「すべてのテストを実行」を呼び出すと、IDEはベーステストクラスを単体テストとして実行しようとし、「実行可能なメソッドがありません」というエラーが表示されます。

空の実行可能なメソッドを基本クラスに入れることができることは知っていますが、誰かがより良いアイデアを持っていることを望んでいました。

基本クラスは次のとおりです。

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {
            "classpath:spring-jndi.xml"
    })
    @TestExecutionListeners({
            Log4JSetupListener.class,
            JndiSetupListener.class,
            DependencyInjectionTestExecutionListener.class,
            DirtiesContextTestExecutionListener.class,
            TransactionalTestExecutionListener.class
    })
    public class SpringAppTestCase extends Assert implements ApplicationContextAware {

        protected JndiTemplate jndiTemplate = new JndiTemplate();

        @Autowired
        protected JdbcTemplate jdbcTemplate;

        protected ApplicationContext applicationContext;

        public void setApplicationContext(ApplicationContext ac) {
            this.applicationContext = ac;
        }

    // 
    //    @Test
    //    public void doit(){
    //      // this would prevent blow up but 
    // all subclass tests would run an extra method
    //    }

        protected Logger log = Logger.getLogger(getClass().getName());

}

エラー:

java.lang.Exception: No runnable methods
    at org.junit.internal.runners.MethodValidator.validateInstanceMethods(MethodValidator.java:32)
    at org.junit.internal.runners.MethodValidator.validateMethodsForDefaultRunner(MethodValidator.java:43)
    at org.junit.internal.runners.JUnit4ClassRunner.validate(JUnit4ClassRunner.java:36)
    at org.junit.internal.runners.JUnit4ClassRunner.<init>(JUnit4ClassRunner.java:27)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:76)
4

7 に答える 7

82

親クラスを作成するか、またはabstractで終わらないように名前を変更します。TestTestCase

于 2009-06-11T06:32:15.850 に答える
2

私は同じ問題を抱えていましたが、クラスの継承がなく、@Test注釈が存在していました。問題は、テスト クラスが public であることpublicでした。修飾子を削除すると、問題が修正されました。

于 2020-11-17T11:41:46.213 に答える
0

私のテストクラスが公開されていないため、このエラーが発生しました。

于 2021-09-12T06:49:45.243 に答える