springcloud使用mockmvc或powermock做单元测试

使用mockmvc做单元测试

创建BaseTest 类

package com.cn21.baichuan.cc.controller.admin;


import com.cn21.baichuan.cc.CommodityCentreApplication;

import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes=CommodityCentreApplication.class)
public class BaseTest {

}

 

 创建测试类SpuAPIControllerTest

public class SpuAPIControllerTest extends BaseTest{
    private MockMvc mockMvc;

    // api接口路径
    private static final String url = "/cc/spu/";
    private static String api;

    @Autowired
    private WebApplicationContext context;

    @Before
    public void setupMockMvc() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
    }

    @Test
    public void testSearch() throws Exception {
        ResCodeAndMsg resCodeAndMsg = new ResCodeAndMsg();
        api = "search.do";

        //参数正确
        MultiValueMap paramMap = new LinkedMultiValueMap();

        paramMap.add("categoryId","2");
        paramMap.add("name","zoutt");
        paramMap.add("brand","2");
        paramMap.add("smartLogo","zzz");
        paramMap.add("description","zoutt");
        paramMap.add("attribute","zoutt");

        RequestBuilder request = MockMvcRequestBuilders.post(url + api).
        param("page","1").param("pageSize","10").params(paramMap);


        MvcResult mvcResult = mockMvc.perform(request)
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();

        String content = mvcResult.getResponse().getContentAsString();
        Map stringObjectMap = JsonUtil.jsonToMap(content);
        Assert.assertEquals(resCodeAndMsg.getSucccessresmsg(),stringObjectMap.get("resMsg"));



        //参数错误
        MultiValueMap paramMapError = new LinkedMultiValueMap();


        paramMapError.add("name","zoutt");
        paramMapError.add("brand","2");
        paramMapError.add("smartLogo","zzz");
        paramMapError.add("description","zoutt");
        paramMapError.add("attribute","zoutt");
        RequestBuilder requestError = MockMvcRequestBuilders.post(url + api).
                param("page","").param("pageSize","10").params(paramMap);
        MvcResult mvcResultError = mockMvc.perform(requestError)
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String contentError = mvcResultError.getResponse().getContentAsString();

        Map stringObjectMapError = JsonUtil.jsonToMap(contentError);

        Assert.assertEquals(resCodeAndMsg.getFailresmsg(),stringObjectMapError.get("resMsg"));

    }

Assert断言:Assert.assertEquals("期望值","实际值"); 

 

 

 

使用powermock做单元测试

测试controller层 创建SkuControllerTest

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.mockito.Matchers.any;

@RunWith(PowerMockRunner.class)
public class SkuControllerTest {
    //mock service层,可指定service的返回值,单纯测试controller层
    @Mock
    SkuInfoService skuInfoService;
    @Mock
    SpuInfoService spuInfoService;


    @InjectMocks
    SkuInfoController skuInfoController = new SkuInfoController();
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testAddSku(){
        Map result = new HashMap<>();
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setAttributeStr("{\n" +
                "\t\t\"非定向属性1\":\"shuxing1\",\n" +
                "\t\t\"非定向属性2\":\"shuxing2\"\n" +
                "\t}");
        skuInfo.setState(1);
        skuInfo.setReturnRule(1);
        skuInfo.setUnit(1);
        skuInfo.setIsVirtualOperator(1);
        skuInfo.setProvince("广东省");
        skuInfo.setBillType(1);
        skuInfo.setType(1);
        skuInfo.setName("爱奇艺会员");
        skuInfo.setBgName("爱奇艺会员包月");
        skuInfo.setSpuId(10001);

        SpuInfo spuInfo = new SpuInfo();
        spuInfo.setSpuId(10001);
        spuInfo.setCategoryId(1);
        Mockito.when(spuInfoService.selectByPrimaryKey(skuInfo.getSpuId())).thenReturn(spuInfo);
        Mockito.when(skuInfoService.insertSelective(skuInfo)).thenReturn(1);
        result = skuInfoController.addSku(skuInfo);
        Assert.assertEquals(result.get(Constants.RES_CODE), 10000);
        System.out.println(result.get(Constants.RES_CODE));


        // 参数错误
        SkuInfo skuInfoError = new SkuInfo();
        skuInfoError.setAttributeStr("{\n" +
                "\t\t\"非定向属性1\":\"shuxing1\",\n" +
                "\t\t\"非定向属性2\":\"shuxing2\"\n" +
                "\t}");
        skuInfoError.setState(1);
        skuInfoError.setReturnRule(1);
        skuInfoError.setUnit(1);
        skuInfoError.setIsVirtualOperator(1);
        skuInfoError.setProvince("广东省");
        skuInfoError.setBillType(1);
        skuInfoError.setType(1);
        skuInfoError.setName("爱奇艺会员");
        skuInfoError.setBgName("爱奇艺会员包月");
        skuInfoError.setSpuId(10001);

        SpuInfo spuInfoError = new SpuInfo();
        spuInfoError.setSpuId(10001);
        spuInfoError.setCategoryId(1);
        Mockito.when(spuInfoService.selectByPrimaryKey(skuInfoError.getSpuId())).thenReturn(spuInfoError);
        Mockito.when(skuInfoService.insertSelective(skuInfoError)).thenReturn(0);
        result = skuInfoController.addSku(skuInfoError);
        Assert.assertEquals(result.get(Constants.RES_CODE), 11003);
        System.out.println(result.get(Constants.RES_CODE));

    }

    @Test
    public void testAddSkuList(){
        Map result = new HashMap<>();
        File file = new File("C:\\Users\\ZtTao\\Desktop\\sku信息表.xlsx");
    }

    @Test
    public void testLowerSku(){
        Map result = new HashMap<>();
        SkuInfoVo skuInfo = new SkuInfoVo();
        skuInfo.setSkuId(100001);
        skuInfo.setState(1);
        Mockito.when(skuInfoService.lowerSkuBySkuInfo(any())).thenReturn(1);
        SkuInfoVo skuInfoVo = new SkuInfoVo();
        skuInfoVo.setOption(1);
        skuInfoVo.setSkuId(100001);
        result = skuInfoController.lowerSku(skuInfoVo);

        Assert.assertEquals(result.get(Constants.RES_CODE), 10000);
        System.out.println(result.get(Constants.RES_CODE));


        //参数错误
        SkuInfoVo skuInfoError = new SkuInfoVo();
        skuInfoError.setSkuId(100011);
        skuInfoError.setState(1);
        Mockito.when(skuInfoService.lowerSkuBySkuInfo(any())).thenReturn(0);
        SkuInfoVo skuInfoVoError = new SkuInfoVo();
        skuInfoVoError.setOption(1);
        skuInfoVoError.setSkuId(100011);
        result = skuInfoController.lowerSku(skuInfoVoError);

        Assert.assertEquals(result.get(Constants.RES_CODE), 11003);
        System.out.println(result.get(Constants.RES_CODE));

    }

    @Test
    public void testEditSku(){
        Map result = new HashMap<>();
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setSkuId(100001);
        skuInfo.setAttributeStr("{\n" +
                "\t\t\"非定向属性1\":\"shuxing1\",\n" +
                "\t\t\"非定向属性2\":\"shuxing2\"\n" +
                "\t}");
        skuInfo.setState(1);
        skuInfo.setReturnRule(1);
        skuInfo.setUnit(1);
        skuInfo.setIsVirtualOperator(1);
        skuInfo.setProvince("广东省");
        skuInfo.setBillType(1);
        skuInfo.setType(1);
        skuInfo.setName("爱奇艺会员");
        skuInfo.setBgName("爱奇艺会员包月");

        Mockito.when(skuInfoService.updateByPrimaryKeySelective(skuInfo)).thenReturn(1);
        result = skuInfoController.editSku(skuInfo);
        Assert.assertEquals(result.get(Constants.RES_CODE), 10000);
        System.out.println(result.get(Constants.RES_CODE));


        // 参数错误
        SkuInfo skuInfoError = new SkuInfo();
        skuInfoError.setSkuId(100011);
        skuInfoError.setAttributeStr("{\n" +
                "\t\t\"非定向属性1\":\"shuxing1\",\n" +
                "\t\t\"非定向属性2\":\"shuxing2\"\n" +
                "\t}");
        skuInfoError.setState(1);
        skuInfoError.setReturnRule(1);
        skuInfoError.setUnit(1);
        skuInfoError.setIsVirtualOperator(1);
        skuInfoError.setProvince("广东省");
        skuInfoError.setBillType(1);
        skuInfoError.setType(1);
        skuInfoError.setName("爱奇艺会员");
        skuInfoError.setBgName("爱奇艺会员包月");

        Mockito.when(skuInfoService.updateByPrimaryKeySelective(skuInfoError)).thenReturn(0);
        result = skuInfoController.editSku(skuInfoError);
        Assert.assertEquals(result.get(Constants.RES_CODE), 11003);
        System.out.println(result.get(Constants.RES_CODE));

    }

    @Test
    public void testSearchSkuByPage(){
        Map result = new HashMap();
        Map pageInfo = new HashMap();
        pageInfo.put("page",1);
        pageInfo.put("pageSize",4);
        pageInfo.put("bgName","优酷定向流量");
        pageInfo.put("skuId",100001);
        pageInfo.put("state",1);
        pageInfo.put("type",1);
        pageInfo.put("province","广东省");
        pageInfo.put("spuId",100010);
        Page pages = new Page<>();
        pages.setPageNum(1);
        pages.setSize(5);

        SkuInfoVo skuInfo = new SkuInfoVo();
        skuInfo.setSkuId(100010);
        skuInfo.setSpuId(100010);
        skuInfo.setAttributeStr("{\n" +
                "\t\t\"非定向属性1\":\"shuxing1\",\n" +
                "\t\t\"非定向属性2\":\"shuxing2\"\n" +
                "\t}");
        skuInfo.setBgName("爱奇艺会员一个月");
        skuInfo.setSmartLogo("logo.jpg");
        skuInfo.setState(1);
        skuInfo.setName("爱奇艺会员");
        skuInfo.setBillType(1);
        skuInfo.setCategoryId(12);
        skuInfo.setUnit(1);
        skuInfo.setOperator(1);
        skuInfo.setProvince("广东省");
        skuInfo.setIsVirtualOperator(1);
        skuInfo.setReturnRule(1);
        pages.setBean(skuInfo);
        result = skuInfoController.searchSkuByPage(pageInfo);
        Assert.assertEquals(result.get(Constants.RES_CODE), 10000);
        System.out.println(result);

        // 参数错误
        Map pageInfoError = new HashMap();

        pageInfoError.put("pageSize",4);
        pageInfoError.put("bgName","优酷定向流量");
        pageInfoError.put("skuId",100001);
        pageInfoError.put("state",1);
        pageInfoError.put("type",1);
        pageInfoError.put("province","广东省");
        pageInfoError.put("spuId",100010);
        Page pagesError = new Page<>();
        pagesError.setPageNum(1);
        pagesError.setSize(5);

        SkuInfoVo skuInfoError = new SkuInfoVo();
        skuInfoError.setSkuId(100010);
        skuInfoError.setSpuId(100010);
        skuInfoError.setAttributeStr("{\n" +
                "\t\t\"非定向属性1\":\"shuxing1\",\n" +
                "\t\t\"非定向属性2\":\"shuxing2\"\n" +
                "\t}");
        skuInfoError.setBgName("爱奇艺一个月");
        skuInfoError.setSmartLogo("logo.jpg");
        skuInfoError.setState(1);
        skuInfoError.setName("爱奇艺会员");
        skuInfoError.setBillType(1);
        skuInfoError.setCategoryId(12);
        skuInfoError.setUnit(1);
        skuInfoError.setOperator(1);
        skuInfoError.setProvince("广东省");
        skuInfoError.setIsVirtualOperator(1);
        skuInfoError.setReturnRule(1);
        pagesError.setBean(skuInfoError);
        result = skuInfoController.searchSkuByPage(pageInfoError);
        Assert.assertEquals(result.get(Constants.RES_CODE), 11002);
        System.out.println(result);
    }

    @Test
    public void testSearchByCategoryName(){
        Map result = new HashMap<>();
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setCategoryId(1);
        List skuInfos = new ArrayList<>();
        Mockito.when(skuInfoService.searchSkuByCategoryId(skuInfo.getCategoryId())).thenReturn(skuInfos);
        result = skuInfoController.searchByCategoryName(skuInfo);
        Assert.assertEquals(result.get(Constants.RES_CODE), 10000);
        System.out.println(result.get(Constants.RES_CODE));

        //参数错误
        SkuInfo skuInfoError = new SkuInfo();
        skuInfoError.setCategoryId(-1);
        Mockito.when(skuInfoService.searchSkuByCategoryId(skuInfoError.getCategoryId())).thenReturn(null);
        result = skuInfoController.searchByCategoryName(skuInfoError);
        Assert.assertEquals(result.get(Constants.RES_CODE), 11003);
        System.out.println(result.get(Constants.RES_CODE));

        //参数错误
        SkuInfo skuInfoErrorSec = new SkuInfo();
        skuInfoErrorSec.setSkuId(1001);

        result = skuInfoController.searchByCategoryName(skuInfoErrorSec);
        Assert.assertEquals(result.get(Constants.RES_CODE), 11002);
        System.out.println(result.get(Constants.RES_CODE));
    }

    @Test
    public void testSearchSkuByBgName(){
        Map result = new HashMap<>();
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setBgName("爱奇艺会员");
        List skuInfos = new ArrayList<>();
        Mockito.when(skuInfoService.searchSkuByBgName(skuInfo.getBgName())).thenReturn(skuInfos);
        result = skuInfoController.searchSkuByBgName(skuInfo);
        Assert.assertEquals(result.get(Constants.RES_CODE), 10000);
        System.out.println(result.get(Constants.RES_CODE));

        //参数错误
        SkuInfo skuInfoError = new SkuInfo();


        result = skuInfoController.searchSkuByBgName(skuInfoError);
        Assert.assertEquals(result.get(Constants.RES_CODE), 11002);
        System.out.println(result.get(Constants.RES_CODE));

    }

    @Test
    public void testBatchEditSkuState(){
        Map res = new HashMap<>();
        SkuInfoVo skuInfoVo = new SkuInfoVo();
        skuInfoVo.setStates("10001|1,10002|0,10003|1");
        Mockito.when(skuInfoService.batchEditSkuState(any())).thenReturn(1);
        res = skuInfoController.batchEditSkuState(skuInfoVo);
        Assert.assertEquals(res.get(Constants.RES_CODE), 10000);
        System.out.println(res.get(Constants.RES_CODE));

        //参数错误
        SkuInfoVo skuInfoVoError = new SkuInfoVo();
        skuInfoVoError.setStates("10001|1,10002|0,10003|-1");
        Mockito.when(skuInfoService.batchEditSkuState(any())).thenReturn(0);
        res = skuInfoController.batchEditSkuState(skuInfoVoError);
        Assert.assertEquals(res.get(Constants.RES_CODE), 11002);
        System.out.println(res.get(Constants.RES_CODE));

        //参数错误
        SkuInfoVo skuInfoVoErrorSec = new SkuInfoVo();

        Mockito.when(skuInfoService.batchEditSkuState(any())).thenReturn(0);
        res = skuInfoController.batchEditSkuState(skuInfoVoErrorSec);
        Assert.assertEquals(res.get(Constants.RES_CODE), 11002);
        System.out.println(res.get(Constants.RES_CODE));


    }


}

测试service层

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class TestSkuServiceImpl {
    @Mock
    SkuInfoMapper skuInfoMapper;
    @InjectMocks
    private SkuInfoServiceImpl service;
    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testInsertSelective(){
        service.insertSelective(null);
    }
    @Test
    public void testInsertList(){
        service.insertList(null);
    }
    @Test
    public void testLowerSkuBySkuInfo(){
        service.lowerSkuBySkuInfo(null);
    }
    @Test
    public void testSelectByPrimaryKey(){
        service.selectByPrimaryKey(null);
    }

    @Test
    public void testUpdateByPrimaryKeySelective(){
        service.updateByPrimaryKeySelective(null);
    }
    @Test
    public void testSearchSkuByCategoryId(){
        service.searchSkuByCategoryId(0);
    }
    @Test
    public void testSearchSkuByBgName(){
        service.searchSkuByBgName(null);
    }
    @Test
    public void testBatchEditSkuState(){
        service.batchEditSkuState(null);
    }
    @Test
    public void testSearchSkuInfo(){
        Page pages = new Page<>();
        Integer page = 1;
        Integer pageSize = 10;
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setSkuId(10001);
        skuInfo.setBgName("爱奇艺会员包月");
        skuInfo.setSpuId(100001);
        skuInfo.setType(1);
        skuInfo.setState(1);
        pages.setPageNum(page);
        pages.setSize(pageSize);
        pages.setBean(skuInfo);
        service.searchSkuInfo(pages);
    }

}

 

你可能感兴趣的:(spring,cloud)