博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot(八):测试组件-junit
阅读量:6967 次
发布时间:2019-06-27

本文共 4273 字,大约阅读时间需要 14 分钟。

hot3.png

1、概述

  • junit集成流程
  • 单元测试类实例

2、junit集成流程

1)pom.xml引入

org.springframework.boot
spring-boot-starter-test
test

2)基类编写:

package com.demo.quickStart;import com.ss.quickStart.QuickStartApplication;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.annotation.Rollback;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.transaction.annotation.Transactional;@RunWith(SpringRunner.class)@SpringBootTest(classes = QuickStartApplication.class)@Transactional@Rollbackpublic class BaseJunitTests {}

 

3、单元测试类实例

package com.demo.quickStart.dao;import com.demo.quickStart.BaseJunitTests;import com.ss.quickStart.dao.UserMapper;import com.ss.quickStart.domain.User;import com.ss.quickStart.domain.dto.UserOrdersDTO;import org.junit.Assert;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;public class UserMapperTest extends BaseJunitTests {    @Autowired    private UserMapper userMapper;    @Test    public void testQryUserOrders3(){        UserOrdersDTO userOrdersDTO = userMapper.qryUserOrders3(1L);        Assert.assertTrue(userOrdersDTO != null && userOrdersDTO.getOrderList().size() > 0);    }    @Test    public void testAdd(){        User user = new User();        user.setName("小王");        user.setSex(1);        int count = userMapper.insert(user);        Assert.assertTrue(count == 1);    }}

测试结果如下图:

093941_USi2_2526015.png

 

4、controller层。

package com.szhtxx.etcloud.ovat.controller;import com.alibaba.fastjson.JSON;import com.szhtxx.etcloud.ovat.api.dto.OvatBillRuleDTO;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.http.MediaType;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.test.web.servlet.MockMvc;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;@RunWith(SpringRunner.class)@SpringBootTest@AutoConfigureMockMvcpublic class OvatBillRuleControllerTest {    @Autowired    private MockMvc mvc;    @Test    public void testAdd() throws Exception{        OvatBillRuleDTO ruleDTO = new OvatBillRuleDTO();        ruleDTO.setRuleSchemaCode("rule_001");        ruleDTO.setRuleSchemaName("wsy拆合规则001");        ruleDTO.setDetailSplitRule("0");        ruleDTO.setDetailMergeRule("0");        ruleDTO.setListRule("0");        ruleDTO.setListLimitSpecial(9999);        ruleDTO.setListLimitGeneral(9999);        ruleDTO.setListLimitElectron(9999);        ruleDTO.setNegativeDetailRule("1");        ruleDTO.setDeviationRule("0");        mvc.perform(post("/ovatBillRule/add")                    .contentType(MediaType.APPLICATION_JSON_UTF8)                    .content(JSON.toJSONString(ruleDTO)))                .andExpect(status().isOk())                .andExpect(jsonPath("$.success").value(true));    }    @Test    public void testQryAllRule() throws Exception{        mvc.perform(get("/ovatBillRule/qryAllRule")                    .contentType(MediaType.APPLICATION_JSON_UTF8))                .andExpect(status().isOk())                .andExpect(jsonPath("$.success").value(true));    }    @Test    public void testQryRuleInfo() throws Exception{        mvc.perform(get("/ovatBillRule/qryRuleInfo")                    .contentType(MediaType.APPLICATION_JSON_UTF8)                    .param("ruleCode","rule_001"))                .andExpect(status().isOk())                .andExpect(jsonPath("$.success").value(true));    }    @Test    public void testDelRule() throws Exception{        mvc.perform(get("/ovatBillRule/delRule")                .contentType(MediaType.APPLICATION_JSON_UTF8)                .param("ruleSchemaId","5aa5f4d63415ae521cecdf8d"))                .andExpect(status().isOk())                .andExpect(jsonPath("$.success").value(true));    }}

 

转载于:https://my.oschina.net/u/2526015/blog/1525300

你可能感兴趣的文章
Centos 配置
查看>>
promise
查看>>
es6学习1: 模拟react Comopnent类的实现
查看>>
js继承问题
查看>>
201621123069 《Java程序设计》第十一周学习总结
查看>>
Java进阶篇(一)——接口、继承与多态
查看>>
linux c 链接详解4-共享库
查看>>
冲刺阶段第七天
查看>>
linux下磁盘分区
查看>>
快速获取表的记录数
查看>>
JavaScript_BOM_window
查看>>
Hadoop:The Definitive Guid 总结 Chapter 7 MapReduce的类型与格式
查看>>
WCF 入门之旅(4): 怎样用客户端调用WCF服务
查看>>
oracle12之 多租户容器数据库架构
查看>>
POJ3061 ZOJ3123 Subsequence【前缀和+二分搜索+尺取法】
查看>>
png库结合zlib库使用出现的一个链接问题的解决
查看>>
Hibernate总结(二)
查看>>
TSP问题
查看>>
ubuntu14.06 Lts开启ssh服务
查看>>
对象比较:Comparable 和 Comparator
查看>>