栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Devtools与单元测试

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Devtools与单元测试

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



    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;

@RestController
public 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;


@SpringBootTest
class 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;

@Repository
public 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;

@Service
public class UserService {

    @Autowired
    UserDao userDao;

    public User getUserById(Integer id) {
        return userDao.getUserById(id);
    }
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/731560.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号