No Runnable方法基本测试类错误

gbe*_*ley 30 junit spring intellij-idea

我有一些基本测试类,它们使用测试执行监听器为spring,logging,jndi等设置常用配置,然后由子类继承.这样做是为了测试可以只运行代码而不必担心在运行测试代码之前获取jndi和日志服务.

使用intellij并从项目库调用"运行所有测试",IDE尝试将基本测试类作为单元测试运行,并为我提供"无可运行方法"错误.

我知道我可以在基类中放置一个空的runnable方法,但我希望有一个人有更好的主意.

Base类是:

    @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());

}
Run Code Online (Sandbox Code Playgroud)

错误:

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)
Run Code Online (Sandbox Code Playgroud)

Rob*_*anu 70

创建父类abstract或重命名它,例如它不以Test或结束TestCase.

  • 从课堂名称中取出"考试"并不适用于我.但添加"抽象"确实如此. (2认同)

Sou*_*ain 8

我遇到过同样的问题。我在没有测试方法的情况下进行测试。

@Test
public void setupTest() {
}
Run Code Online (Sandbox Code Playgroud)

以上方法解决了问题。

  • 或者确保`@Test`注释导入是`org.junit.Test`而不是像juniper这样的其他东西 (2认同)
  • 我使用了错误的导入 `import org.junit.jupiter.api.Test;` 而不是 `import org.junit.Test;`。你的回答让我验证了导入,干杯! (2认同)

aki*_*ail 7

确保 @Test 注释导入是 org.junit.Test 而不是像 juniper 这样的其他东西,我认为这里的评论对我有用。想将此添加为答案,以便其他人可以看到