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或重命名它,例如它不以Testor结尾TestCase

于 2009-06-11T06:32:15.850 回答
11

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

于 2020-06-18T18:14:16.227 回答
10

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

@Test
public void setupTest() {
}

上面的方法解决了这个问题。

于 2016-09-27T10:10:23.410 回答
2

我遇到了同样的问题,但我没有类继承并且@Test存在注释。问题是我的测试类是公开的,我删除了public修饰符并修复了问题。

于 2020-11-17T11:41:46.213 回答
0

您要确保没有从 Jupiter 导入 @Test,混合 JUnit 4 和 JUnit 5,可能会导致同样的问题。我为我的 SpringRunner 导入了正确的 JUnit,问题得到了解决

于 2021-05-10T01:55:49.050 回答
0

我收到此错误是因为我的测试课程不是公开的。

于 2021-09-12T06:49:45.243 回答
0

确保不要混合使用 JUnit4 和 JUnit5。

例如尝试使用@BeforeEach (来自 JUnit5)而不是@Before(来自 JUnit4)等。

并确保您的@Test注释是从而org.junit.jupiter.api.Test不是从org.junit.Test.

于 2021-09-23T07:35:34.110 回答