spring 基本测试类中没有可运行的方法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/977017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:53:01  来源:igfitidea点击:

No Runnable methods Error From Base Test class

springjunitintellij-idea

提问by gbegley

I have a A few base test classes that setup common configurations for spring,logging,jndi etc using test execution listeners that are then inherited by subclasses. This is done so tests can just run code without having to worry about getting jndi and logging services in place before being able to run testing code.

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

Using intellij and invoking "run all tests" from the project base, the IDE attempts to run the base test class as a unit test and gives me the "No runnable methods" error.

使用 intellij 并从项目库中调用“运行所有测试”,IDE 尝试将基测试类作为单元测试运行,并给出“无可运行方法”错误。

I know I could put an empty runnable method in the base class, but I was hoping some one has a better idea.

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

The Base class is:

基类是:

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

}

The error:

错误:

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)

回答by Robert Munteanu

Make the parent class abstractor rename it such as it does not end in Testor TestCase.

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

回答by Soumyajit Swain

I had the same issue. I was testing without a Test method.

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

@Test
public void setupTest() {
}

Above method solved the issue.

以上方法解决了问题。