引入依赖
使用相关注解,需要首先在 pom.xml
引入依赖。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.10.0</version> <!-- 具体版本号根据需要选择 --> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> </dependencies>
|
使用方法
在引入依赖之后,使用方法如下 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
public class Main { @BeforeEach void setUp() { System.out.println("Execute before every Test method"); }
@Test public void addTest() { int A = 3, B = 2; int result = A + B; assertEquals(5, result, "结果应该是5"); }
@Test public void subTest() { int A = 3, B = 2; int result = A - B; assertEquals(1, result); }
public static void main(String[] args) { }
@AfterEach public void tearDown() { System.out.println("Execute after every Test method"); } }
|