SpringBoot 整合 单元测试

简介: springboot 单元测试的介绍使用,Junit 。Mock

一、junit基本参数介绍

参数 解释
@BeforeClass 在单元测试类中执行一次,在所有测试方法前执行一次
@AfterClass 在单元测试类中执行一次,在所有测试方法后执行一次,通常在其中写上销毀和释放资源的代码
@Before 在每个测试方法前执行,一股用来初始化方法(比如我们在測试别的方法时,类中与其 他测试方法共享的值已经被改变,为了保证测试结果的有效性,我们会在@Before注解 的方法中重置数据)
@After 释放资源 ,对于每一个测试方法都要执行一次
@Test{timeout =1000) 测试方法执行超过1000室秒后算超时,测试将失败
@Test(expected=Exception.class) 测试方法期里得到的异常类,如果方法执行设有抛出指走的异常,则测试失败
@lgnore(i!not ready yet,,) 执行測试时将忽K掉此方法,如果用于修饰类,则忽15整个类 编写一股测试用例
@Test 编写一般测试用例
@RunWith 在JUnit中有很多个Runner,他们负责堝用你的测试代码,每一^Runne谢有各自的待 殊功能,你要根据需要选择不同的Runner来运行你的測试代码。如果我们只是简单的做 音通Java测试,不涉及Spring Web顷目,你可以省略@RunWith注解,这祥系统会目动 使用默认Flunn6「来运行你的代码。

二、 各个参数的执行顺序

  • 一个JUnit4的单元测试用例执行顺序为:

    @BeforeClass -> @Before -> @Test -> @After -> @AfterClass;

  • 每一个测试方法的调用顺序为:

    @Before -> @Test -> @After;

三、Springboot 单元测试

  • 依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
  • Controller 测试

    @RunWith(SpringRunner.class)
    //ItodoApplication springboot的启动类
    @SpringBootTest(classes = ItodoApplication.class)
    @AutoConfigureMockMvc
    public class ApplicationTests {
        @Autowired
        private MockMvc mvc;
    
        // 注入Spring容器
        @Autowired
        private WebApplicationContext wac;
    
        @Before
        public void setupMockMvc(){
      // 初始化MockMvc对象
      mvc = MockMvcBuilders.webAppContextSetup(wac).build();
        }
        @Test(timeout = 100000)
        public void testUserController() throws Exception {
      //ObjectMapper 是一个可以重复使用的对象
      String exposeHeaders = "access-control-expose-headers";
      String allowMethods = "Access-Control-Allow-Methods";
      String allowHeaders = "Access-Control-Allow-Headers";
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = "{\"todoOpenId\":\"oKiWu4iI0wKFxmiE-BLiZ1kud26Q\"}";
      //将JSON字符串值转换成 Girl对象里的属性值
      WxUserView girl = mapper.readValue(jsonString, WxUserView.class);
      MvcResult result = mvc.perform(
              //这里是post请求,如果get,替换即可。
              MockMvcRequestBuilders.post("/api/wxUser/detailUser")
                      .contentType(MediaType.APPLICATION_JSON)
                      .header("authToken","wx")
                      .header("Origin","chrome-extension://mdbgchaihbacjfjeikflfbelidihhmfn")
                      .header(exposeHeaders,"111")
                      .header(allowHeaders,"222")
                      .header(allowMethods,"3333333333")
                       //post请求传参数
                     .content(mapper.writeValueAsString(girl))
                     //get请求,传参数用这个
                      //.param();
              .accept(MediaType.APPLICATION_JSON)   //断言返回结果是json
      )
              .andExpect(MockMvcResultMatchers.status().isOk())
              .andReturn();
      MockHttpServletResponse response = result.getResponse();
               //拿到请求返回码
               int status = response.getStatus();
              //拿到结果
               String contentAsString = response.getContentAsString();
      System.out.println(result);
        }
    }
  • Service 测试

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes  =ItodoApplication.class)
    public class ServiceTest {
    
        @Autowired
        private WxUserServiceImpl wxUserService;
    
        @Test
        public void usertest(){
      WxUser wxUser = wxUserService.getWxUserByOpenId("oKiWu4iI0wKFxmiE-BLiZ1kud26Q");
        }
    }
    

四、@MockBean 和 @SpyBean

  • 在写测试时,对于一些应用的外部依赖需要进行一些Mock 处理,比如:Redis 等;对于这些外部依赖,统一在配置层完成 Mock;
  • @MockBean:mock的是本地的代码(自己写的代码),对于储存在库中并且是以 Bean 的形式装配到代码中的类无能为力;而且会导致spirngboot多次重启,因为会导致applicationContext的缓存失效。
  • @SpyBean:会监听一个Bean 中某些特定的方法,并在调用这些方法时给出指定的映射。

五、@Profile(value = "dev")

  • 这个注解,注解到类上,用于在不同的环境使用
相关文章
|
4月前
|
Java 测试技术 数据库
如何做SpringBoot单元测试?
如何做SpringBoot单元测试?
|
4月前
|
XML SQL Java
ClickHouse【SpringBoot集成】clickhouse+mybatis-plus配置及使用问题说明(含建表语句、demo源码、测试说明)
ClickHouse【SpringBoot集成】clickhouse+mybatis-plus配置及使用问题说明(含建表语句、demo源码、测试说明)
167 0
|
18天前
|
Java 测试技术
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
21 0
|
1月前
|
Java 测试技术 数据库
springboot大学生体质测试管理系统
springboot大学生体质测试管理系统
|
3月前
|
Java 测试技术
SpringBoot整合Junit进行单元测试
SpringBoot整合Junit进行单元测试
32 0
|
3月前
|
监控 Java 测试技术
基于springboot实现的个人性格测试系统(分前后端)
基于springboot实现的个人性格测试系统(分前后端)
|
3月前
|
前端开发 Java 测试技术
SpringBoot - 应用程序测试方案
SpringBoot - 应用程序测试方案
54 0
|
4月前
|
Java Linux 开发工具
MinIO【部署 01】MinIO安装及SpringBoot集成简单测试
MinIO【部署 01】MinIO安装及SpringBoot集成简单测试
124 0
|
4月前
|
监控 Java
Pinpoint【部署 02】Pinpoint Agent 安装启动及监控 SpringBoot 项目案例分享(添加快速测试math-game.jar包)
Pinpoint【部署 02】Pinpoint Agent 安装启动及监控 SpringBoot 项目案例分享(添加快速测试math-game.jar包)
78 0
|
4月前
|
NoSQL Java API
SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码)推荐使用
SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码)推荐使用
58 0