ITKeyword,专注技术干货聚合推荐

注册 | 登录

spring webmvc如何编写service 和controller的单元测试

xiao_jun_0820 分享于 2016-06-17

推荐:Spring Mock单元测试

针对post和get import org.junit.Before;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframew

2020腾讯云共同战“疫”,助力复工(优惠前所未有!4核8G,5M带宽 1684元/3年),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1053

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
地址https://www.aliyun.com/minisite/goods

首先编写一个测试基类: 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.Req

推荐:spring 和hibernate 集成 时单元测试

首先继承 AbstractTransactionalDataSourceSpringContextTests public class Test extends AbstractTransactionalDataSourceSpringContextTests { @Override pub

uestMapping; 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"));

} }

推荐:如何在Spring MVC工程中进行单元测试

        直接以代码方式演示如何在Spring MVC工程中进行单元测试: package net.chinaedu.projects.dubhe;import java.util.List;import net.chinaedu.proje

首先编写一个测试基类: package com.kingnet.xydb.web.basetest; import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowire

相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。