SpringMVC+JUNIT单元测试

一、引入jar包

  hamcrest-all-1.3.jar
  junit-4.11.jar
  spring-test-4.1.6.RELEASE.jar

二、测试代码

package com.dcits.cms.test;

import com.alibaba.fastjson.JSONObject;
import com.dcits.base.model.ResultObject;
import com.dcits.cms.model.ExchangeEarnings;
import com.dcits.cms.service.ExchangeEarningsNewService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.HashMap;
import java.util.Map;


/**
 * 专门用于项目的单元测试
 * Created by zhouquan on 2017/11/30.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml","classpath:spring_mvc.xml"})

@WebAppConfiguration
public class UnitTesting {

    @Autowired
    ExchangeEarningsNewService exchangeEarningsNewService;

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }


    /**
     *  测试service
     *  @Transactional  @Rollback 加上之后默认事务会被回滚 数据不会被插入数据库
     *  @throws Exception
     */
    @Test
    public void Ctest() throws Exception {
        ExchangeEarnings exchangeEarnings = new ExchangeEarnings();
        exchangeEarnings.setCP_ID("1111");
        ResultObject ro = ResultObject.createInstance();
        exchangeEarningsNewService.saveOrUpdate(ro,exchangeEarnings);

    }


    /**
     * 测试controller -- 1、普通请求
     * @throws Exception
     */
    @Test
    public void demo() throws Exception {
        ExchangeEarnings exchangeEarnings = new ExchangeEarnings();
        exchangeEarnings.setREMARK("1111");
        mockMvc.perform(MockMvcRequestBuilders.post("/financialModule/selectExchangeEarningsList")
                .param("pageNum","2")
                .flashAttr("exchangeEarnings",exchangeEarnings))
                .andDo(MockMvcResultHandlers.print());

    }

    /**
     * 测试controller -- 2、含有json数据的请求
     * @throws Exception
     */
    @Test
    public void demo1() throws Exception {
        Map m = new HashMap();
        m.put("GUID","111");
        String requestJson = JSONObject.toJSONString(m);

        mockMvc.perform(MockMvcRequestBuilders.post("/financialModule/updateBeforeExchangeEarnings")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(requestJson)
                )
                .andDo(MockMvcResultHandlers.print());

    }

    /**
     * 测试controller -- 3、含有json数据的请求(实体类)
     * @throws Exception
     */
    @Test
    public void demo2() throws Exception {
//        ExchangeEarnings exchanceEarnings = new ExchangeEarnings();
//        exchanceEarnings.setREMARK("1111");
//        String requestJson = JSONObject.toJSONString(exchanceEarnings);

        String requestJson = "{\"remark\":\"1111\"}";
        mockMvc.perform(MockMvcRequestBuilders.post("/financialModule/saveOrUpdateExchangeEarnings")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(requestJson)
                )
                .andDo(MockMvcResultHandlers.print());
    }

}

~~~`````````````````````````````````````````````

你可能感兴趣的:(SpringMVC+JUNIT单元测试)