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

发布于 2024-07-24 11:03:52 字数 1909 浏览 5 评论 0原文

我有一些基本测试类,它们使用测试执行监听器为 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)

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.

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.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

幻梦 2024-07-31 11:03:52

将父类设为abstract 或重命名,使其不以TestTestCase 结尾。

Make the parent class abstract or rename it such as it does not end in Test or TestCase.

み格子的夏天 2024-07-31 11:03:52

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

making sure the @Test annotation import is org.junit.Test and not something else like juniper which i saw as a comment by here worked for me. Wanted to add this as an answer so it can be seen by others

神经暖 2024-07-31 11:03:52

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

@Test
public void setupTest() {
}

以上方法解决了问题。

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

@Test
public void setupTest() {
}

Above method solved the issue.

公布 2024-07-31 11:03:52

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

I Got the same problem but I had no class inheritance and @Test annotation was present. Problem was that my test class was public, I deleted public modifier and problem was fixed.

可是我不能没有你 2024-07-31 11:03:52

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

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

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

Make sure you do not mix JUnit4 and JUnit5.

For example try to use @BeforeEach (from JUnit5) instead of @Before (from JUnit4) etc.

And make sure your @Test annotation is imported from org.junit.jupiter.api.Test instead of org.junit.Test.

∞觅青森が 2024-07-31 11:03:52

我将基本单元测试类设置为抽象类。 然后Junit不再将其视为测试类。

I made the base unit test class an abstract class. Then Junit no longer sees it as a test class.

零度℉ 2024-07-31 11:03:52

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

You want to make sure that you are not importing the @Test from Jupiter, mixing the JUnit 4 and JUnit 5, can cause the same issue. I imported the right JUnit for my SpringRunner and the problem was resolved

太傻旳人生 2024-07-31 11:03:52

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

I got this error because my test class is not public.

行雁书 2024-07-31 11:03:52

就我而言,使用 JUnit 5 进行 Spring Boot,我使用 @ExtendWith(SpringExtension.class) 和 @RunWith(MockitoJUnitRunner.class),删除 @RunWith(MockitoJUnitRunner.class) 修复它。

In my case, spring boot with JUnit 5, I use @ExtendWith(SpringExtension.class) along with @RunWith(MockitoJUnitRunner.class), remove @RunWith(MockitoJUnitRunner.class) fixed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文