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); }}
测试结果如下图:
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)); }}