大家好,我刚开始学习Junit,现在遇到一个问题。写了一个简单的测试:
public class MainTests {
private List<Employee> employeeList;
@BeforeAll
public void sayHello(){
System.out.println("hello");
}
@BeforeEach
public void listInit() {
employeeList = new ArrayList<>();
employeeList.add(new Employee(1, "John", "Smith", "USA", 25));
employeeList.add(new Employee(2, "Ivan", "Petrov", "RU", 23));
}
@Test
public void parseCsv() {
String fileName = "data.csv";
String[] columnMapping = new String[]{"id", "firstName", "lastName", "country", "age"};
List<Employee> list = Main.parseCSV(columnMapping, fileName);
Assert.assertEquals(employeeList, list);
}
据我了解,我用 BeforeAll 注释标记的方法应该在所有测试开始之前工作一次(这根本不会发生)。另外,带有BeforeEach注解的方法必须在每次测试之前工作(这也根本不会发生)。不要告诉我为什么这些注解没有被处理?我的pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.guluev</groupId>
<artifactId>CsvJsonParser</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.6</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>