首先编写一个测试基类:
package com.kingnet.xydb.web.basetest;然后就可以编写测试用例啦,比如我们现在有一个HelloService和HelloController:
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();
}
}
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;我们对service进行测试,编写一个HelloServiceTest,继承SpringTestCase:
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;
}
}
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"));
}
}