常用的Java测试框架有JUnit和TestNG。
下面是一个使用 JUnit 编写的简单自动化测试的例子:
首先,在项目中添加 JUnit 依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
编写测试类,例如:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ExampleTest {
@Test
public void testAdd() {
int a = 1;
int b = 2;
int expected = 3;
int result = Example.add(a, b);
assertEquals(expected, result);
}
}
运行测试,可以使用