基本测试类没有可运行的方法错误
我有一些基本测试类,它们使用测试执行监听器为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
将父类设为
abstract
或重命名,使其不以Test
或TestCase
结尾。Make the parent class
abstract
or rename it such as it does not end inTest
orTestCase
.确保 @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
我遇到过同样的问题。 我在没有测试方法的情况下进行测试。
以上方法解决了问题。
I had the same issue. I was testing without a Test method.
Above method solved the issue.
我遇到了同样的问题,但我没有类继承,并且存在
@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 deletedpublic
modifier and problem was fixed.确保不要混合使用 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 fromorg.junit.jupiter.api.Test
instead oforg.junit.Test
.我将基本单元测试类设置为抽象类。 然后Junit不再将其视为测试类。
I made the base unit test class an abstract class. Then Junit no longer sees it as a test class.
您要确保没有从 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
我收到此错误是因为我的测试类不是公开的。
I got this error because my test class is not public.
就我而言,使用 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.