下面是学习代码,按照学习的内容。
预期结果应该是:
globalInit invoked!
before
Test1
destroy
before
Test2
destroy
before
Test3
destroy
globalDestroy invoked!
但是各种顺序都会出现,求解!
globalInit invoked!
before
Test1
destroy
before
destroy
Test2
destroy
before
Test3
globalDestroy invoked!
globalInit invoked!
before
Test1
destroy
before
Test2
destroy
destroy
before
Test3
globalDestroy invoked!
public class TestJUnit4
{
@BeforeClass
public static void globalInit()
{
System.out.println("globalInit invoked!");
}
@AfterClass
public static void globalDestroy()
{
System.out.println("globalDestroy invoked!");
}
@Before
public void setUp()
{
System.out.println("before");
}
@After
public void tearDown()
{
System.err.println("destroy");
}
@Test
public void test1()
{
System.out.println("Test1");
}
@Test
public void test2()
{
System.out.println("Test2");
}
@Test
public void test3()
{
System.out.println("Test3");
}
}
执行顺序 JUnit4? 注解
------解决方案--------------------
http://liqita.iteye.com/blog/1543129
------解决方案--------------------
你在tearDown里面用的是System.err.println("destroy");
err和out不是一个IO流,不会同步的。如果你用System.out.println("destroy"); 每次结果就一样了