zoukankan      html  css  js  c++  java
  • springboot2.X版本下写测试用例(基于Junit5)

    今天使用springboot 2.2.2.RELEASE版本创建了一个项目,在写测试用例的时候发现有问题,@RunWith @Before这些注解找不到,或者不起作用,网上搜索后发现,此版本的springboot使用的是Junit5版本,springboot使用Junit4和使用Junit5写测试用例方法是不一样的的。

    Junit5 主要注解:

    1.@BeforeAll 类似于JUnit 4的@BeforeAll,表示使用了该注解的方法应该在当前类中所有使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之前执行,必须为static;
    2.@BeforeEach 类似于JUnit 4的@Before,表示使用了该注解的方法应该在当前类中每一个使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之前执行;
    3.@Test 表示该方法是一个测试方法;
    4.@DisplayName 为测试类或测试方法声明一个自定义的显示名称;
    5.@AfterEach 类似于JUnit 4的@After,表示使用了该注解的方法应该在当前类中每一个使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之后执行;
    6.@AfterAll 类似于JUnit 4的@AfterClass,表示使用了该注解的方法应该在当前类中所有使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之后执行,必须为static;
    7.@Disable 用于禁用一个测试类或测试方法,类似于JUnit 4的@Ignore;
    8.@ExtendWith 用于注册自定义扩展。

    使用区别

    springboot+junit4:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringBootQuickStartApplicationTests {
    
        private MockMvc mvc;
    
        @Before
        public void setUp() throws Exception {
            mvc = MockMvcBuilders.standaloneSetup(new UserController()).build();
        }
    
        @Test
        public void contextLoads() throws Exception {
            RequestBuilder request = null;
           
            request = MockMvcRequestBuilders.get("/")
                    .contentType(MediaType.APPLICATION_JSON);
            mvc.perform(request)
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andDo(MockMvcResultHandlers.print())
                    .andReturn();
       }
    }
    

    springboot+junit5:

    @SpringBootTest
    // 使用spring的测试框架
    @ExtendWith(SpringExtension.class)
    class SpringbootQuickStartApplicationTests {
    
        private MockMvc mockMvc;
    
        @BeforeEach // 类似于junit4的@Before
        public void setUp() throws Exception {
            mockMvc = MockMvcBuilders.standaloneSetup(new UserController()).build();
        }
    
        @Test
        void contextLoads() throws Exception {
            RequestBuilder request = null;
    
            request = MockMvcRequestBuilders.get("/")
                    .contentType(MediaType.APPLICATION_JSON);
            mockMvc.perform(request)
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andDo(MockMvcResultHandlers.print())
                    .andReturn();
        }
    }
    
    一颗安安静静的小韭菜。文中如果有什么错误,欢迎指出。
  • 相关阅读:
    IOS Date, NSDateFormatter, compare, dateFromString:mydatestr
    IOS Retain,nil,alloc,init
    IOS NSArray,NSDictionary
    object c基础, 基本类型(NSString,char*NSDate,NSData),集合NSArray,NSMutableArray,NSDictionary,NSMutableDictionary,NSSet,NSMutableSet
    IOS UIProgressView 用法
    UIButton 用法
    XCode 快捷键,MAC 快捷键
    苹果软件系列产品介绍
    android 之多线程应用[message,messagequeue,handler,looper,asynchtask]
    Linux查看程序被哪个端口占用
  • 原文地址:https://www.cnblogs.com/c-Ajing/p/13448362.html
Copyright © 2011-2022 走看看