Java单元测试--如何编写Controler接口的单元测试

Java单元测试--如何编写Controler接口的单元测试,第1张

Java单元测试--如何编写Controler接口的单元测试

在UTDD的流程中,通常会从上到下进行模拟,流程如下:

Controller接口单元测试
–》Controller接口代码实现
–》Api接口单元测试
–》Api接口代码实现

那么我们先从Controller的单元测试模拟开始:
一、MockMvc功能介绍:
MockMvc是由spring-test包提供,实现了对Http请求的模拟,能够直接使用网络的形式,转换到Controller的调用,使得测试速度快、不依赖网络环境。同时提供了一套验证的工具,结果的验证十分方便。

接口MockMvcBuilder,提供一个唯一的build方法,用来构造MockMvc。
主要有两个实现:StandaloneMockMvcBuilder和DefaultMockMvcBuilder,分别对应两种测试方式,即独立安装和集成Web环境测试(并不会集成真正的web环境,而是通过相应的Mock API进行模拟测试,无须启动服务器)。
MockMvcBuilders提供了对应的创建方法standaloneSetup方法和webAppContextSetup方法,在使用时直接调用即可。

二、代码示例:
首先是pom坐标:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.example
    loan-server
    0.0.1-SNAPSHOT
    loan-server
    loan-server
    
        1.8
        1.16.0
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            com.alibaba
            fastjson
            1.2.24
        
        
            org.springframework.boot
            spring-boot-starter-test
            
                
                    org.junit.jupiter
                    junit-jupiter
                
            
            test
        
        
            org.junit.vintage
            junit-vintage-engine
            test
        
        
        
            junit
            junit
            4.12
            test
        
        
        
            org.jmockit
            jmockit
            1.19
            test
        
        
            com.h2database
            h2
            1.4.197
            test
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.3.4
        
    
    
        
            
                org.testcontainers
                testcontainers-bom
                ${testcontainers.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2.Controller接口测试类:

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest(classes = LoanServerApplication.class)//这里放启动类
public class LoanControllerTest {

    MockMvc mockMvc;
    @Autowired
    private WebApplicationContext webApplicationContext;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                .addFilter(((request, response, chain) -> {
                    response.setCharacterEncoding("UTF-8");
                    chain.doFilter(request, response);
                })).build();
    }

        @Test
    public void should_get_userLoanPlan_success_byGet() throws Exception {
        String result = mockMvc.perform(
                get("/loan-plans/{idCard}", "412233333333"))
                .andExpect(status().isOk())
                .andReturn()
                .getResponse()
                .getContentAsString();
        String expected = "{"createdAt":"2021-11-23T00:41:49.257479","gender":"MALE","houseMaterial":{"age":0,"price":10000},"id":1,"idCard":"412233333333","income":10000,"lenderAge":35,"name":"王一","togetherLender":{"idCard":"111111111","income":10000},"updatedAt":"2021-11-23T00:41:49.257512"}";
        JSONComparator comparator = new CustomComparator(JSONCompareMode.LENIENT,
                new Customization("id", (o1, o2) -> true),
                new Customization("createdAt", (o1, o2) -> true),
                new Customization("updatedAt", (o1, o2) -> true)
        );
        JSONAssert.assertEquals(expected, result, comparator);
    }

    @Test
    public void should_get_userLoanPlan_success_byPost() throws Exception {
        String result = mockMvc.perform(
                post("/loan-plans/{idCard}", "412233333333"))
                .andExpect(status().isOk())
                .andReturn()
                .getResponse()
                .getContentAsString();
        String expected = "{"createdAt":"2021-11-23T00:41:49.257479","gender":"MALE","houseMaterial":{"age":0,"price":10000},"id":1,"idCard":"412233333333","income":10000,"lenderAge":35,"name":"王一","togetherLender":{"idCard":"111111111","income":10000},"updatedAt":"2021-11-23T00:41:49.257512"}";
        JSONComparator comparator = new CustomComparator(JSONCompareMode.LENIENT,
                new Customization("id", (o1, o2) -> true),
                new Customization("createdAt", (o1, o2) -> true),
                new Customization("updatedAt", (o1, o2) -> true)
        );
        JSONAssert.assertEquals(expected, result, comparator);
    }
 }

3、Controller接口代码

@RestController
@RequestMapping("loan-plans")
public class LoanController {
    

    @GetMapping("/{idCard}")
    public JSONObject getUserLoadPlanMaterial(@PathVariable String idCard) {
        String result = "{"createdAt":"2021-10-23T00:41:49.257479","gender":"MALE","houseMaterial":{"age":0,"price":10000},"id":1,"idCard":"412233333333","income":10000,"lenderAge":35,"name":"王一","togetherLender":{"idCard":"111111111","income":10000},"updatedAt":"2021-10-23T00:41:49.257512"}";

        return JSONObject.parseObject(result);
    }
  
  @PostMapping("/{idCard}")
    public JSONObject Test(@PathVariable String idCard) {
        String result = "{"createdAt":"2021-10-23T00:41:49.257479","gender":"MALE","houseMaterial":{"age":0,"price":10000},"id":1,"idCard":"412233333333","income":10000,"lenderAge":35,"name":"王一","togetherLender":{"idCard":"111111111","income":10000},"updatedAt":"2021-10-23T00:41:49.257512"}";
        return JSONObject.parseObject(result);
    }
 }

这里我们通过MockMvc实现了一个简单的get/post请求接口的模拟及单元测试的编写~ 有兴趣的可以尝试一下~ 还可以拓展一些其他接口的单元测试~

喜欢的朋友欢迎一键三连。我们一起学习,进步~~

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5562830.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存