通过Junit和Spring-Test测试SpringMVC的web应用
来源:互联网 发布:l800清零软件 编辑:程序博客网 时间:2024/05/01 16:21
通过Junit和Spring-Test测试SpringMVC的web应用
1、通过Spring-Test测试web应用
可以避免多次启动web 容器带来的烦恼,每次执行特定的测试方法。
再也不用通过web容器(如tomcat,jetty等)启动整个web应用,尤其是当项目复杂,比较庞大时,能够节省很多时间。
2、maven依赖
maven依赖除了需要用到的junit和spring以及其他的项目使用的依赖之外,需要额外增加
spring-test的jar包。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Used for Hibernate4 LocalSessionFactoryBean -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
3、具体测试类
这里只贴上了对应测试类,controller类只实现简单的syso。
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration(value = "src/main/webapp") @ContextConfiguration({"classpath*:/applicationContext.xml" }) //@ContextConfiguration("file:src/main/resources/applicationContext.xml")public class TestRunController {@Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Autowired TestController testController ; @Autowired ServletContext context; @Before public void setUp() { // mockMvc = MockMvcBuilders.webAppContextSetup( wac ).build(); mockMvc = MockMvcBuilders.standaloneSetup( testController).build() ; } @Test public void test() throws Exception { assertNotNull(mockMvc); mockMvc.perform( MockMvcRequestBuilders.get("/test/testA.action?a=1") ) .andReturn() ; } @Test public void testB() throws Exception { assertNotNull(mockMvc); mockMvc.perform( MockMvcRequestBuilders.post("/test/testB.action" ).content("aaa") ) .andReturn() ; }}
参考资料:
http://my.oschina.net/u/2007041/blog/418748?fromerr=AKpkgCGL
http://jinnianshilongnian.iteye.com/blog/2004660
https://www.ibm.com/developerworks/cn/java/j-lo-springunitest/
springmvc-test的api: http://www.csdn123.com/html/mycsdn20140110/a7/a75383fcc7d869a7627583ada5e76e46.html
- 通过Junit和Spring-Test测试SpringMVC的web应用
- 【Java.JUnit】Spring Test, JUnit, Mockito, Hamcrest 集成 Web 测试
- Junit测试的羽翼Spring-test
- Spring Test, JUnit, Mockito, Hamcrest 集成 Web 测试
- SpringMVC+Spring+hibernate+junit的Dao测试
- Junit 测试之 Spring Test
- junit 测试spring-test 代码!在项目中运行测试通过,留着以后直接使用
- spring test---测试SpringMvc初识
- Spring的Junit测试
- Spring的Junit测试
- Spring框架使用内幕之IOC&Bean的生命周期&整合Web项目和Junit测试
- JUNIT+SPRINGMVC的mock测试
- spring+springmvc+mybatis进项junit测试
- Junit和Spring、SpringBoot测试
- junit测试和spring整合
- junit test用来测试hibernate的配置
- 基于Spring的Junit测试
- spring下的junit测试
- Android开发中一些被冷落但却很有用的类和方法
- -webkit-background-size的使用
- how to download the souce code for trdp?
- Eclipse颜色主题插件:Eclipse Color Theme
- 关于Tomcat的Pipeline机制
- 通过Junit和Spring-Test测试SpringMVC的web应用
- Maven学习 (五) Elipse中发布一个Maven项目到Tomcat
- KM(N^3)模板(HDU2255)
- android中画虚线--.PathEffect类简单认识
- 安全测试学习笔记二(对于top 10 漏洞的分析)
- poj 2236 Wireless Network
- ARC 类型转换:显示转换 id 和 void *
- 根据CI模式弄的一个python访问数据的class
- VS批生成(Batch Build)不可使用的问题