SpringMVC 测试 mockMVC案列

编写一个简单案例,各种属性之后在网上查找

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

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.mock.web.MockSessionCookieConfig;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import com.ai.lawyer.action.lawyer.TbLawyerManageService;

@RunWith(SpringJUnit4ClassRunner.class)   
@ContextConfiguration(locations = {"classpath:spring/applicationContext.xml","classpath:spring/springview.xml"})  
@WebAppConfiguration
public class MockMVCTest {

	 @Autowired
    private WebApplicationContext webApplicationContext;

    private MockMvc mockMvc;

    @Before
    public void  setUp( ) {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }
    /**
	 * perform:执行一个RequestBuilder请求,会自动执行SpringMVC的流程并映射到相应的控制器执行处理;
	 * get:声明发送一个get请求的方法。MockHttpServletRequestBuilder get(String urlTemplate, Object... urlVariables):根据uri模板		和uri变量值得到一个GET请求方式的。另外提供了其他的请求的方法,如:post、put、delete等。
	 * param:添加request的参数,如上面发送请求的时候带上了了pcode = root的参数。假如使用需要发送json数据格式的时将不能使用这种		方式,可见后面被@ResponseBody注解参数的解决方法
	 * andExpect:添加ResultMatcher验证规则,验证控制器执行完成后结果是否正确(对返回的数据进行的判断);
	 * andDo:添加ResultHandler结果处理器,比如调试时打印结果到控制台(对返回的数据进行的判断);
	 * andReturn:最后返回相应的MvcResult;然后进行自定义验证/进行下一步的异步处理(对返回的数据进行的判断)
	 * @throws Exception
	 */
    @Test
	public void getAllBanners() throws Exception{
	/*	 String responseString = mockMvc.perform(get("/goodField")    //请求的url,请求的方法是get
	                        .contentType(MediaType.APPLICATION_JSON)  //数据的格式
	                        .param("goodField","房地产")         //添加参数
	        ).andExpect(status().isOk())    //返回的状态是200
	                .andDo(print())         //打印出请求和相应的内容
	                .andReturn().getResponse().getContentAsString();   //将相应的数据转换为字符串
	        System.out.println("--------返回的json = " + responseString);*/
    	
    	ResultActions andDo = mockMvc.perform(get("/tbLawyerManage/goodField")    //请求的url,请求的方法是get
                .contentType(MediaType.APPLICATION_JSON)  //数据的格式
                .param("goodField","房地产")         //添加参数
			).andExpect(status().isOk())    //返回的状态是200
			        .andDo(print());
			  
    	MvcResult andReturn = andDo.andReturn();
			System.out.println("--------返回的json = "+ andReturn.getResponse().getContentAsString() );
	}

}

详情参考 [https://www.cnblogs.com/xd03122049/p/6001457.html ]
[https://yq.aliyun.com/articles/565494 ]

其他测试

/**
 * 接口测试
 * @author xzb_l
 *
 */
public class TestMapper {
	
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("provider.xml");
		String feeType = "1";
		String formulaType = "4";
		Double amountOfMoney = 110000.0;
		Double isHalve = 0.5;
		
		TbCivilFeeFormulaService feeMapper = ac.getBean("tbCivilFeeFormulaService",TbCivilFeeFormulaService.class);
		Double feeFormulaByParameters= 0.0;
		try {
			feeFormulaByParameters = feeMapper.getFeeFormulaByParameters(feeType,formulaType,amountOfMoney,isHalve);
			System.out.println(feeFormulaByParameters);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

SpringBoot测试方式

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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 org.springframework.test.web.servlet.ResultActions;
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 static org.hamcrest.core.IsEqual.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@RunWith(SpringRunner.class)
public class HelloTest {

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.standaloneSetup(new ReadController()).build();
    }
    @Test
    public void gethello() throws Exception {
       mockMvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().string("hello spring boot!"));
        MockMvcResultHandlers.print();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值