英文:
Super class and abstract test method with JUnit5 tests
问题
我将单元测试从JUnit4迁移到JUnit5,并且我使用IntelliJ作为IDE。在JUnit4中,我有一些测试实现了测试超类的两种方法。似乎我可以在抽象方法上放置@Test
,然后只需在子类中重写它们。
import org.junit.jupiter.api.Test;
@SpringBootTest(classes = {TestContext.class})
public abstract class AbstractTest {
@Test
public abstract void testOk();
@Test
public abstract void testKo();
}
以及测试
public class SomeTest extends AbstractTest {
// 使用JUnit5的@Test注解
@Override
public void testOk() {
//
}
// 使用JUnit5的@Test注解
@Override
public void testKo() {
//
}
}
但是在JUnit5中,似乎我必须在子类SomeTest
的重写方法上放置@Test
注解,否则无法找到测试。是否有一种方法可以在JUnit5中实现以前的配置,避免为所有子类方法添加@Test
?
英文:
I migrated unit tests from JUnit4 to JUnit5 and I use Intellij as IDE
With JUnit4, I had some tests implementing two methodes of a test superclass. It seems like I could put the @Test
on the abstract methods and just override them in subclasses.
import org.junit.jupiter.api.Test;
@SpringBootTest(classes = {TestContext.class})
public abstract class AbstractTest {
@Test
public abstract void testOk();
@Test
public abstract void testKo();
}
And the test
public class SomeTest extends AbstractTest {
// @Test with JUnit5
@Override
public void testOk() {
//
}
// @Test with JUnit5
@Override
public void testKo() {
//
}
}
But with JUnit5, it seems like I have to put @Test
annotation on the overriding method of the subclass SomeTest
, otherwise tests are not found. Is there a way to achieve my previous configuration with JUnit5, avoiding to add @Test
to all subclasses methods ?
答案1
得分: 0
根据M. Deinum的建议,我最终得到了这个:
import org.junit.jupiter.api.Test;
@SpringBootTest(classes = {TestContext.class})
public abstract class AbstractTest {
public abstract void testOk();
public abstract void testKo();
@Test
public void junitTestOk() {
this.testOk();
}
@Test
public void junitTestKo() {
this.testKo();
}
}
英文:
Following M. Deinum recommandation, I ended up with this
import org.junit.jupiter.api.Test;
@SpringBootTest(classes = {TestContext.class})
public abstract class AbstractTest {
public abstract void testOk();
public abstract void testKo();
@Test
public void junitTestOk() {
this.testOk();
}
@Test
public void junitTestKo() {
this.testKo();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论