Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行JUnit'的顺序错误;思想方法_Java_Unit Testing_Junit4 - Fatal编程技术网

Java 运行JUnit'的顺序错误;思想方法

Java 运行JUnit'的顺序错误;思想方法,java,unit-testing,junit4,Java,Unit Testing,Junit4,我对Idea 14和JUnit有问题。我不能以正确的顺序运行@BeforeClass和@AfterClass方法(在所有测试之前和之后)。每次的订单都不一样。我试图重新安装IDEA,删除所有设置,但没有任何效果。请帮忙。这是我的测试代码示例: package com.rent.test; import org.junit.AfterClass; import org.junit.BeforeClass; import static org.junit.Assert.*; import org.

我对Idea 14和JUnit有问题。我不能以正确的顺序运行@BeforeClass和@AfterClass方法(在所有测试之前和之后)。每次的订单都不一样。我试图重新安装IDEA,删除所有设置,但没有任何效果。请帮忙。这是我的测试代码示例:

package com.rent.test;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import static org.junit.Assert.*;
import org.junit.Test;

public class testnewTest {
    static int num;
    static int num1;

    @BeforeClass
    public static void OnceExecutedBeforeAll() {
        System.out.println("@BeforeClass: onceExecutedBeforeAll");
        num = 15;
        num1 = 16;
    }

    @AfterClass
    public static void after() throws Exception {
        System.out.println("End");
    }

    @Test
    public void testLogin() throws Exception {
        System.out.println("test");
        assertEquals(15, num);
    }

    @Test
    public void testGetOrdersDate() throws Exception {
        System.out.println("test2");
        assertEquals(16, num1);
    }
}
这是输出:

 test2
 @BeforeClass: onceExecutedBeforeAll
 test
 End

您可能观察到的是,终端中的输出并不总是同步的。测试本身以正确的顺序运行


如果没有,那么您将在
test2
中失败,因为您的
@BeforeClass
方法似乎是在之后启动的。

在我的想法14.1.3和JUnit4.12中运行良好。您使用的是哪个版本?在junit中,没有定义执行顺序,请查看@RafaelGuillen:下面的链接,这是针对单个测试的更多内容。秩序不能保证,但它是确定的。这涉及到
@BeforeClass
@AfterClass
。IDEA的版本:14.1.4有时测试尝试在@Beforclass方法结束之前运行测试,并且由于缺乏适当的上下文,测试失败。