• 软件测试:黑盒测试、白盒测试
  • 单元测试属于白盒测试
  • 使用 Junit 进行单元测试:导包,生成测试类,run as junit

# JUnit 3

  • 执行顺序:setUp() -> testXxx() -> tearDown()
// JUnit 3
// 测试类继承 TestCase 类
public class EmployeeDAOTest extends TestCase {
    // 初始化操作
    protected void setUp() throws Exception {
    }
    // 扫尾操作
    protected void tearDown() throws Exception {
    }
    // 测试单元,public 修饰、无返回、无参数、方法名以 test 作为前缀的方法
    public void testXxx() throws Exception {
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# JUnit 4 (opens new window)

# 注解

  • @BeforeClass、@AfterClass 只能修饰静态方法
  • 执行顺序:@BeforeClass ->(@Before -> @Test -> @After 多个测试方法)--> @AfterClass
// JUnit 4
public class CalculatorTest {
    @Before
    public void init() throws Exception {
    }
    @After
    public void destory() throws Exception {
    }
    // 测试单元,public 修饰、无返回、无参数、@Test 标注的方法
    @Test
    public void testXxx() throws Exception {
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 断言

  • org.junit.Assert 类 ,断言失败提示 message

    1. Assert.assertEquals(message, expected, actual):断言相等
    2. Assert.assertSame(message, expected, actual):断言是同一个对象
    3. Assert.assertNotSame(message, expected, actual):断言不是同一个对象
    4. Assert.assertTrue(message, condition):断言 condition 应该为 TRUE
    5. Assert.assertFalse(message, condition):断言 condition 应该为 FALSE
    6. Assert.assertNull(message, object):断言对象 object 为 null
    7. Assert.assertNotNull(message, object):断言对象 object 不为 null
    8. Assert.assertThat(T actual, Matcher<? super T> matcher):org.hamcrest.Matcher
    9. Assert.void assertThat(String reason, T actual, Matcher<? super T> matcher)
  • @Test 注解
    @Test(expected = ArithmeticException.class):期望该方法出现 ArithmeticException 异常
    @Test(timeout = 400):期望该方法在 400 毫秒之内执行完成

# JUnit 5 (opens new window)

  • JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
  • JUnit Platform:用于 JVM 上启动测试框架的基础服务,提供命令行,IDE 和构建工具等方式执行测试的支持
  • JUnit Jupiter:包含 JUnit 5 新的编程模型和扩展模型,主要用于编写测试代码和扩展代码
  • JUnit Vintage:用于在 JUnit 5 中兼容运行 JUnit 3 和 JUnit 4 的测试用例

# 注解

  • @Test
  • @DisplayName、@DisplayNameGeneration
  • @BeforeEach、@AfterEach
  • @BeforeAll、@AfterAll 只能修饰静态方法
  • @Disabled:禁用执行测试
  • @Nested:修饰非静态内部测试类
  • @RepeatedTest:重复性测试
  • @Timeout
  • @ParameterizedTest
  • @TestFactory
  • @TestTemplate
  • @TestClassOrder
  • @TestMethodOrder(MethodOrderer.OrderAnnotation.class)、@Order
  • @TestInstance
  • @Tag
  • @ExtendWith
  • @RegisterExtension
  • @TempDir

# 断言

  • org.junit.jupiter.api.Assertions

# Mockito (opens new window)

  • documentation (opens new window)
  • ArgumentMatchers 类
  • Mockit 类,extends ArgumentMatchers
  • BDDMockito 类,extends Mockito
  • You can use doThrow(), doAnswer(), doNothing(), doReturn() and doCallRealMethod() in place of the corresponding call with when(), for any method. It is necessary when you
    • stub void methods
    • stub methods on spy objects
    • stub the same method more than once, to change the behaviour of a mock in the middle of a test.

# 基准测试

JMH (opens new window)(Java Microbenchmark Harness),OpenJDK 团队开发的一款基准测试工具,一般用于代码的性能调优,精度甚至可以达到纳秒级别,适用于 Java 以及其它基于 JVM 的语言。

Updated at: 2022-11-09 19:52:32