[英]Are there any conventions of extending JUnit4 test classes?
I have a lot of common logic for my test, so I decide to share it by extending.我的测试有很多共同的逻辑,所以我决定通过扩展来分享。 I've wrote two classes:
TestNumberOne
which extends TestBase
.我写了两个类:
TestNumberOne
,它扩展了TestBase
。
TestBase.java测试库
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
/**
* @author Pavel
* @since 2013-03-03
*/
public class TestBase {
@BeforeClass
public static void beforeClass() {
System.out.println("beforeClass() in TestBase");
System.out.flush();
}
@AfterClass
public static void afterClass() {
System.out.println("afterClass() in TestBase");
System.out.flush();
}
@Before
public void before() {
System.out.println("before() in TestBase");
System.out.flush();
}
@After
public void after() {
System.out.println("after() in TestBase");
System.out.flush();
}
}
TestNumberOne.java TestNumberOne.java
import org.junit.*;
/**
* @author Pavel
* @since 2013-03-03
*/
public class TestNumberOne extends TestBase {
@Test
public void anyTest() {
System.out.println("anyTest() in TestNumberOne");
System.out.flush();
}
}
I've got such a strange output when I execute my tests:当我执行测试时,我得到了如此奇怪的输出:
before() in TestBase
anyTest() in TestNumberOne
after() in TestBase
beforeClass() in TestBase
afterClass() in TestBase
Why does it has so strange order?为什么它有这么奇怪的顺序? And are there any conventions of extending JUnit test classes?
是否有任何扩展 JUnit 测试类的约定?
UPDATE:更新:
This is definitely an IntelliJ IDEA issue.这绝对是一个 IntelliJ IDEA 问题。
If I run your code by maven, it runs ok;如果我通过 maven 运行你的代码,它运行正常; if I run it in IntelliJ several times, then I sometimes get the incorrect output as you do.
如果我在 IntelliJ 中多次运行它,那么有时我会像您一样得到不正确的输出。
Actually, I found a way to reproduce it:实际上,我找到了一种重现它的方法:
(also if you run it with sleep, you see that the output is in correct order but gets reordered when the test ends) (此外,如果您使用 sleep 运行它,您会看到输出顺序正确,但在测试结束时会重新排序)
So they are run in correct order, only the output is messed up.所以它们以正确的顺序运行,只有输出混乱。
When I try your code, I just get the expected output, ie当我尝试你的代码时,我只是得到了预期的输出,即
beforeClass() in TestBase
before() in TestBase
anyTest() in TestNumberOne
after() in TestBase
afterClass() in TestBase
(launched with Eclipse). (与 Eclipse 一起启动)。 Which is the convention ^^ Your result is really strange indeed ...
这是惯例^^你的结果确实很奇怪......
don't know of any conventions.不知道任何约定。 in junit 3, the base class was usually abstract.
在junit 3 中,基类通常是抽象的。
the following seems to come out in a sane order (except perhaps for the teardowns).以下内容似乎以合理的顺序出现(也许拆解除外)。
import org.junit.*;
public class BaseTestCase {
public static String method() {
return Thread.currentThread().getStackTrace()[2].getMethodName() + "()";
}
@BeforeClass public static void classSetupBaseClass() {
System.out.println(method());
}
@AfterClass public static void classTeardownBaseClass() {
System.out.println(method());
}
@Before public void setupBaseClass() {
System.out.println(method());
}
@After public void teardownBaseClass() {
System.out.println(method());
}
@Test public void aTestInBaseClass() {
System.out.println(method());
}
}
import static org.junit.Assert.*;
import org.junit.*;
public class So15183669 extends BaseTestCase {
@BeforeClass public static void classSetup() {
System.out.println(method());
}
@AfterClass public static void classTeardown() {
System.out.println(method());
}
@Before public void setup() {
System.out.println(method());
}
@After public void teardown() {
System.out.println(method());
}
@Test public void aTest() {
System.out.println(method());
}
}
classSetupBaseClass()
classSetup()
setupBaseClass()
setup()
aTest()
teardown()
teardownBaseClass()
setupBaseClass()
setup()
aTestInBaseClass()
teardown()
teardownBaseClass()
classTeardown()
classTeardownBaseClass()
I think when you run the tests, your TestBase is also being run as it's own junit test.我认为当您运行测试时,您的 TestBase 也正在运行,因为它是自己的 junit 测试。 Try calling it HelperBase instead.
尝试将其称为 HelperBase。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.