Java JUnit4 BeforeClass标记未调用方法
我目前在设置JUnit测试时遇到问题。我的测试类中有一个方法来初始化测试将使用的对象,但由于某些原因,没有调用该方法 我将JUnit4.13.1与jdk1.8一起使用 我的POM文件依赖项:Java JUnit4 BeforeClass标记未调用方法,java,maven,unit-testing,junit,Java,Maven,Unit Testing,Junit,我目前在设置JUnit测试时遇到问题。我的测试类中有一个方法来初始化测试将使用的对象,但由于某些原因,没有调用该方法 我将JUnit4.13.1与jdk1.8一起使用 我的POM文件依赖项: <dependencies> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artif
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.6</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
但我在maven surefire报告中得到了这一点:
-------------------------------------------------------------------------------
测试集:学生测试
-------------------------------------------------------------------------------
测试运行:1,失败:1,错误:0,跳过:0,经过的时间:0.002
sec您需要这些方法是静态的,因此它们将在运行类的测试之前执行
使用@Before怎么样?他们说只有BeforeClass才需要它,但如果我使用@Before@Before,我会遇到完全相同的问题,因为该方法在每次测试调用之前都会被执行。是的,它应该,但它没有。
import org.joda.time.DateTime;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* @author mohdm
*/
public class StudentTest {
private Student s1, s2;
private Module m1, m2;
private Course c1, c2;
@BeforeClass
public void setup(){
s1 = new Student("Mohamed Moustafa", 32, new DateTime("1988-11-21"), 1728);
s2 = new Student("Mohamed Moustafa", 34, new DateTime("1986-11-01"), 1708);
m1 = new Module("Programming", "CT101");
m2 = new Module("Paradigms", "CT201");
c1 = new Course("CS&IT", new DateTime("2020-08-01"), new DateTime("2021-05-25"));
c2 = new Course("ECE", new DateTime("2020-08-01"), new DateTime("2021-05-25"));
}
@Test
public void testSetup(){
assertNotNull(s1);
}
}
@BeforeClass
public static void setup(){
s1 = new Student("Mohamed Moustafa", 32, new DateTime("1988-11-21"), 1728);
s2 = new Student("Mohamed Moustafa", 34, new DateTime("1986-11-01"), 1708);
m1 = new Module("Programming", "CT101");
m2 = new Module("Paradigms", "CT201");
c1 = new Course("CS&IT", new DateTime("2020-08-01"), new DateTime("2021-05-25"));
c2 = new Course("ECE", new DateTime("2020-08-01"), new DateTime("2021-05-25"));
}