一、方式一
//需要Servlet 3.0,如果项目是Servlet 2.5,引入测试依赖Servlet 3.0即可。
//MockHttpServletRequest request =new MockHttpServletRequest();
//MockHttpServletResponse response = new MockHttpServletResponse();
MockHttpSession session = new MockHttpSession();
依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
如果不引入Servlet3.0会报错:
java.lang.NoClassDefFoundError: javax/servlet/AsyncContext
java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig
二、方式二(未测试)
HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class);
HttpServletResponse responseMock = Mockito.mock(HttpServletResponse.class);
HttpSession sessionMock = Mockito.mock(HttpSession.class);
Mockito.when(requestMock.getSession()).thenReturn(sessionMock);
依赖
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
三、示例代码
package test.cms;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.urthink.framework.page.EuiPage;
import com.urthink.framework.web.UserSession;
import com.urthink.module.cms.entity.Catalog;
import com.urthink.module.cms.service.CatalogService;
import com.urthink.platform.authority.entity.User;
/**
* 栏目测试类
* @author happyqing
*
*/
@RunWith(SpringJUnit4ClassRunner.class) // 整合
@Rollback // 事务自动回滚
@ContextConfiguration(locations = {"classpath:spring-context.xml", "classpath:spring-mybatis.xml"}) // 加载配置
public class CatalogTest extends AbstractTransactionalJUnit4SpringContextTests {
private static final Log log = LogFactory.getLog(CatalogTest.class);
@Autowired
private CatalogService catalogService;
/**
* 测试前执行
*/
@Before
public void beforeTest(){
User user = new User();
user.setId("1");
//需要Servlet 3.0,如果项目是Servlet 2.5,引入测试依赖Servlet 3.0即可。
//MockHttpServletRequest request =new MockHttpServletRequest();
//MockHttpServletResponse response = new MockHttpServletResponse();
MockHttpSession session = new MockHttpSession();
session.setAttribute("user", user);
}
/**
* 测试后执行
*/
@After
public void afterTest(){
}
/**
* 分页查询测试
*/
@Test
public void pageListTest() {
Map paramMap = new HashMap();
paramMap.put("page", 1);
paramMap.put("rows", 10);
paramMap.put("orderBy", "id asc");
EuiPage euiPage = catalogService.getPageList(paramMap);
assertThat(euiPage.getTotal(), greaterThan(0L));
}
/**
* 插入测试
*/
@Test
public void insertTest() {
Catalog catalog = new Catalog();
catalog.setParentId(14);
catalog.setSiteId(2);
catalog.setName("测试栏目");
catalog.setDescription("测试栏目描述");
catalog.setEnable("1");
int r = catalogService.insert(catalog);
assertThat(r, greaterThan(0));
}
}
参考: