欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Devtools与单元测试

时间:2023-07-09
Devtools与单元测试

1.Devtools

1.1 体验Devtools1.2.静态资源修改后,重启项目的配置1.2.禁用自动重启1.2.Trigger控制重启行为 2.单元测试

2.1 单元测试简介2.2 Spring Boot Mock测试2.3 Spring Boot @MockBean模拟数据测试 1.Devtools 1.1 体验Devtools

1)pom.xml

<?xml version="1.0" encoding="UTF-8"?> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.3 com.yl devtools 0.0.1-SNAPSHOT devtools Demo project for Spring Boot 11 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin

2)测试,没改之前访问接口


3)改动代码后,不用重启项目只需编译一下项目就发现服务会自动重启,注意:devtools只会在dev时才有效


1.2.静态资源修改后,重启项目的配置

1)在application.properties文件中加入这行即可

1.2.禁用自动重启

1)在application.properties配置

2)或者在启动类加

1.2.Trigger控制重启行为

1)加上这行配置

2)再新建一个.reloadTrigger的文本文件,里面什么内容都可以,原理就是这个文本的内容一旦发生改变,项目就会重启

2.单元测试 2.1 单元测试简介

1)单元测试的依赖是在创建项目时,默认就加入的

2)其核心模块就是这两个

2.2 Spring Boot Mock测试

1)实体类

package com.yl.test.domain;import java.io.Serializable;public class User implements Serializable { private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + ''' + ", password='" + password + ''' + '}'; }}

2)controller

package com.yl.test.controller;import com.yl.test.domain.User;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserController { @GetMapping("/user/{id}") public User getUserById(@PathVariable Integer id) { User user = new User(); user.setId(id); user.setUsername("root"); user.setPassword("123"); return user; }}

3)测试,调用controller的get请求

package com.yl.test;import com.yl.test.controller.UserController;import com.yl.test.dao.UserDao;import com.yl.test.domain.User;import com.yl.test.service.UserService;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import org.mockito.Mockito;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.boot.test.mock.mockito.MockBean;import org.springframework.http.MediaType;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.MockMvcBuilder;import org.springframework.test.web.servlet.RequestBuilder;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.result.MockMvcResultHandlers;import org.springframework.test.web.servlet.result.MockMvcResultMatchers;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext;@SpringBootTestclass TestApplicationTests { @Autowired WebApplicationContext webApplicationContext; MockMvc mockMvc; //获取mockMvc实例 @BeforeEach void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc = MockMvcBuilders.standaloneSetup(new UserController()).build(); } @Test void contextLoads() throws Exception { //发送get请求 mockMvc.perform(MockMvcRequestBuilders.get("/user/12").accept(MediaType.APPLICATION_JSON_UTF8)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().string("{"id":12,"username":"root","password":"123"}")) .andDo(MockMvcResultHandlers.print()); }}

结果

2.3 Spring Boot @MockBean模拟数据测试

1)dao

package com.yl.test.dao;import com.yl.test.domain.User;import org.springframework.stereotype.Repository;@Repositorypublic class UserDao { public User getUserById(Integer id) { User user = new User(); user.setId(id); user.setUsername("root"); user.setPassword("root"); return user; }}

2)service

package com.yl.test.service;import com.yl.test.dao.UserDao;import com.yl.test.domain.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class UserService { @Autowired UserDao userDao; public User getUserById(Integer id) { return userDao.getUserById(id); }}

3)测试,发现获取到的user对象并不是userDao提供的,而是MockBean模拟提供的

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。