使用mockMvc测试项目的controller层

1.需要配置,在测试用例跑起来之前,先启动spring和springmvc对应的容器(不然没法真正测试,)

具体的可行性代码如下

package com.hgh.laweye.controller;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
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.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.WebApplicationContext;

import com.itrus.laweye.controller.user.UserController;

/**
 * 集成web环境测试
 * @author heguanhong
 *
 */
//@RunWith(JUnit4ClassRunner.class)
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:config/applicationContext*.xml")
public class UserControllerTest2 extends AbstractJUnit4SpringContextTests{

	@Autowired
	public WebApplicationContext webApplicationContext;
	
	public MockHttpSession session;
	
	private static final Logger log = LoggerFactory.getLogger(UserControllerTest2.class);
	private MockMvc mockMvc;
	@Before
	public void Init(){
		mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
	}
	@Test
	public void testTest() {
		MvcResult mvcResult = null;
		try {
			mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/user/test"))
					.andExpect(MockMvcResultMatchers.status().is(200))
					.andDo(MockMvcResultHandlers.print())
					.andReturn();
			log.info("请求返回的状态码:" + mvcResult.getResponse().getStatus());
			String result = mvcResult.getResponse().getContentAsString();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		fail("Not yet implemented");
	}

}


仅仅测试controller,即:独立测试的情况

package com.hgh.laweye.controller;

import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.itrus.laweye.controller.user.UserController;

/**
 * 独立测试的方式
 * @author heguanhong
 *
 */
public class UserControllerTest {

	private static final Logger log = LoggerFactory.getLogger(UserControllerTest.class);
	private MockMvc mockMvc;
	@Before
	public void Init(){
		UserController userController = new UserController();
		mockMvc = MockMvcBuilders.standaloneSetup(userController).build();
	}
	@Test
	public void testTest() {
		MvcResult mvcResult = null;
		try {
			mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/user/test"))
					.andExpect(MockMvcResultMatchers.status().is(200))
					.andDo(MockMvcResultHandlers.print())
					.andReturn();
			log.info("请求返回的状态码:" + mvcResult.getResponse().getStatus());
			String result = mvcResult.getResponse().getContentAsString();
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		
		fail("Not yet implemented");
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值