单元测试Junit5

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage,添加依赖

    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.5.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-launcher</artifactId>
      <version>1.6.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
      <version>5.5.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-junit-jupiter</artifactId>
      <version>3.5.15</version>
      <scope>test</scope>
    </dependency>

注解

常用注解

  • @Test 表示方法是一种测试方法。 与JUnit 4的@Test注解不同,此注释不会声明任何属性。

  • @ParameterizedTest 表示方法是参数化测试

  • @RepeatedTest 表示方法是重复测试模板

  • @TestFactory 表示方法是动态测试的测试工程

  • @DisplayName 为测试类或者测试方法自定义一个名称

  • @BeforeEach 表示方法在每个测试方法运行前都会运行 ,@AfterEach 表示方法在每个测试方法运行之后都会运行

  • @BeforeAll 表示方法在所有测试方法之前运行 ,@AfterAll 表示方法在所有测试方法之后运行

  • @Nested 表示带注解的类是嵌套的非静态测试类,@BeforeAll和@AfterAll方法不能直接在@Nested测试类中使用,除非修改测试实例生命周期。

  • @Tag 用于在类或方法级别声明用于过滤测试的标记

  • @Disabled 用于禁用测试类或测试方法

  • @ExtendWith 用于注册自定义扩展,该注解可以继承

  • @FixMethodOrder(MethodSorters.NAME_ASCENDING),控制测试类中方法执行的顺序,这种测试方式将按方法名称的进行排序,由于是按字符的字典顺序,所以以这种方式指定执行顺序会始终保持一致;不过这种方式需要对测试方法有一定的命名规则,如 测试方法均以testNNN开头(NNN表示测试方法序列号 001-999)

常用断言 Assertions

  • assertEquals 断言预期值和实际值相等

  • assertAll 分组断言,执行其中包含的所有断言

  • assertArrayEquals 断言预期数组和实际数组相等

  • assertFalse 断言条件为假

  • assertNotNull 断言不为空

  • assertSame 断言两个对象相等

  • assertTimeout 断言超时

  • fail 使单元测试失败

MockMvc

MockMvc是由spring-test包/Junit5提供,实现了对Http请求的模拟,能够直接使用网络的形式,转换到Controller的调用,使得测试速度快、不依赖网络环境。同时提供了一套验证的工具,结果的验证十分方便。

  • mock接口/类,验证,测试桩

        LinkedList mockedList = Mockito.mock(LinkedList.class);
        mockedList.add("one");
        //验证
        Mockito.verify(mockedList).add("one");
        //测试桩
        //当调用mockList.get(0)的时候,返回first
        //当调用mockList.get(1)的时候,抛出一个运行时异常
        when(mockedList.get(0)).thenReturn("first");
        when(mockedList.get(1)).thenThrow(new RuntimeException());
  • 使用mock进行接口测试

@RunWith(SpringJUnit4ClassRunner.class)     //表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class UserTest {
    private MockMvc mockMvc;
    @Autowired
    private WebApplicationContext webApplicationContext;
    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void test() throws Exception {
        /*
         * 1、mockMvc.perform执行一个请求。
         * 2、MockMvcRequestBuilders.get("XXX")构造一个请求。
         * 3、ResultActions.param添加请求传值
         * 4、ResultActions.accept(MediaType.TEXT_HTML_VALUE))设置返回类型
         * 5、ResultActions.andExpect添加执行完成后的断言。
         * 6、ResultActions.andDo添加一个结果处理器,表示要对结果做点什么事情
         *   比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息。
         * 7、ResultActions.andReturn表示执行完成后返回相应的结果。
         */
        mockMvc.perform(MockMvcRequestBuilders
                .get("/user")
                .accept(MediaType.APPLICATION_JSON_VALUE)
                .param("id", "2"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("Hello Tom!"))
                .andDo(MockMvcResultHandlers.print());
    }
}

 

参考  https://zhuanlan.zhihu.com/p/55648107

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/yjh1995/p/13996493.html