您的位置:首页 > 编程语言 > Java开发

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

2016-06-17 10:33 465 查看
首先编写一个测试基类:

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"));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: