spring webmvc如何编写service 和controller的单元测试
2016-06-17 10:33
465 查看
首先编写一个测试基类:
然后就可以编写测试用例啦,比如我们现在有一个HelloService和HelloController:
我们对service进行测试,编写一个HelloServiceTest,继承SpringTestCase:
对controller测试同理,编写一个HelloControllerTest,继承SpringTestCase:
package com.kingnet.xydb.web.basetest; import org.junit.Before; 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.ContextHierarchy; 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.web.context.WebApplicationContext; import java.nio.charset.Charset; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; @WebAppConfiguration(value = "src/main/webapp") //指定bean注入的配置文件 @ContextHierarchy({ @ContextConfiguration(name = "parent", locations = "classpath:spring/applicationContext.xml"), @ContextConfiguration(name = "child", locations = "classpath:spring-servlet.xml") }) //使用标准的JUnit @RunWith注释来告诉JUnit使用Spring TestRunner @RunWith(SpringJUnit4ClassRunner.class) public class SpringTestCase extends AbstractJUnit4SpringContextTests { public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); @Autowired protected WebApplicationContext wac; protected MockMvc mockMvc; @Before public void setup() throws Exception { this.mockMvc = webAppContextSetup(this.wac).build(); } }
然后就可以编写测试用例啦,比如我们现在有一个HelloService和HelloController:
package com.kingnet.xydb.web.service; import org.springframework.stereotype.Service; /** * Created by xiaoj on 2016/5/31. */ @Service public class HelloService { public String sayHello(String name) { return "hello:" + name; } }
package com.kingnet.xydb.web.controller; import com.kingnet.xydb.web.service.HelloService; import com.kingnet.xydb.web.util.ResultCode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by xiaojun on 2016/1/14. */ @Controller public class HelloController extends BaseController { @Autowired private HelloService helloService; @RequestMapping(value = "/hello") public @ResponseBody Object hello() { System.out.println(helloService.sayHello("jacky")); return ResultCode.SUCCESS.toMap(); } }
package com.kingnet.xydb.web.controller; import com.kingnet.xydb.web.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; /** * Created by xiaojun on 2016/1/14. */ @Controller public class HelloController extends BaseController { @Autowired private HelloService helloService; @RequestMapping(value = "/hello") public @ResponseBody Object hello() { System.out.println(helloService.sayHello("jacky")); HashMap<String,Object> result = new HashMap<>(); result.put("resultCode", "00"); result.put("description", "成功"); return result; } }
我们对service进行测试,编写一个HelloServiceTest,继承SpringTestCase:
package com.kingnet.xydb.web.service; import com.kingnet.xydb.web.basetest.SpringTestCase; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import static org.junit.Assert.*; /** * Created by xiaoj on 2016/6/17. */ public class HelloServiceTest extends SpringTestCase { @Autowired private HelloService helloService; @Test public void sayHelloTest(){ String result = helloService.sayHello("jacky"); System.out.println(result); assertEquals("sayHello test error","hello:jacky",result); } }
对controller测试同理,编写一个HelloControllerTest,继承SpringTestCase:
package com.kingnet.xydb.web.controller; /** * Created by xiaoj on 2016/5/30. */ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.SerializationFeature; import com.kingnet.xydb.web.basetest.SpringTestCase; 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.ContextHierarchy; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; import java.nio.charset.Charset; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; public class HelloControllerTest extends SpringTestCase { @Test public void hello() throws Exception { this.mockMvc.perform(get("/hello")).andExpect(content().contentType(APPLICATION_JSON_UTF8)).andExpect(jsonPath("$.resultCode").value("00")); } }
相关文章推荐
- 第一章 Web MVC简介 —— 跟开涛学SpringMVC
- 使用eclipse自带的工具访问webService
- Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念
- Mac系统下执行hadoop jar 运行在某包内的程序 提示java.lang.ClassNotFoundException的解决方法
- socket长连接客户端管理
- java中用Windows默认程序打开文件
- java.sql 接口 Connection
- Eclipse 导入netty 示例程序
- java.sql 接口 PreparedStatement
- 《Java编程思想》--面向对象和JVM基础
- Eclipse自动生成UML插件ModelGoon教程
- java根据ftl模板生成word文档
- ElasticSearch学习一:搜索实例演示(Java API)
- JAVA运行时报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- Java NIO使用及原理分析 (四)
- Java NIO使用及原理分析(三)
- Java NIO使用及原理分析(二)
- Struts2三种传参方式(从jsp页面传到Action)
- SpringMVC从数据库导入导出 CSV文件(利用multipart/form-data实现文件上传)
- Eclipse findbugs插件使用及简单介绍