Spring 4集成Junit4

前言

springmvc4 整合 junit4,测试service层和controller层。

环境
  • Spring-4.2.5.RELEASE
  • Junit-4.11
spring环境整合
  • 项目结构
    Spring 4集成Junit4_第1张图片

  • pom.xml文件

    
    	4.0.0
    	com.sicimike
    	spring-junit-demo
    	war
    	1.0.0-SNAPSHOT
    	spring-junit-demo
    	http://maven.apache.org
    
    	
    		4.2.5.RELEASE
    	
    
    	
    		
    			junit
    			junit
    			4.11
    			test
    		
    
    		
    			org.springframework
    			spring-core
    			${spring.version}
    		
    		
    			org.springframework
    			spring-beans
    			${spring.version}
    		
    		
    			org.springframework
    			spring-aop
    			${spring.version}
    		
    		
    			org.springframework
    			spring-context
    			${spring.version}
    		
    		
    			org.springframework
    			spring-web
    			${spring.version}
    		
    		
    			org.springframework
    			spring-webmvc
    			${spring.version}
    		
    		
    			org.springframework
    			spring-test
    			${spring.version}
    		
    		
    			org.springframework
    			spring-context-support
    			${spring.version}
    		
    
    		
    			org.hamcrest
    			hamcrest-core
    			1.3
    		
    
    		
    			javax.servlet
    			javax.servlet-api
    			3.0.1
    			provided
    		
    
    		
    			com.fasterxml.jackson.core
    			jackson-core
    			2.8.8
    		
    		
    			com.fasterxml.jackson.core
    			jackson-annotations
    			2.8.8
    		
    		
    			com.fasterxml.jackson.core
    			jackson-databind
    			2.8.8
    		
    
    	
    
    	
    		spring-junit-demo
    	
    
    
  • applicationContext.xml文件

    
    
    	
    	
    	
    	
    	
    
    
  • web.xml文件

    
    
    
    	spring-junit
    	
    	
    		contextClass
    		org.springframework.web.context.support.XmlWebApplicationContext
    	
    	
    		contextConfigLocation
    		classpath:applicationContext.xml
    	
    	
    	
    	
    	
    		encodingFilter
    		org.springframework.web.filter.CharacterEncodingFilter
    		
    			encoding
    			UTF-8
    		
    		
    			forceEncoding
    			true
    		
    	
    	
    		encodingFilter
    		*
    	
    	
    
    	
    		SpringServlet
    		org.springframework.web.servlet.DispatcherServlet
    		
    			contextConfigLocation
    			classpath:applicationContext.xml
    		
    		2
    	
    
    	
    		SpringServlet
    		/*
    	
    
    	
    		org.springframework.web.context.ContextLoaderListener
    	
    	
    	
    		org.springframework.web.util.IntrospectorCleanupListener
    	
    	
    
    
测试service

./src/main/java目录下service代码如下:

package com.sicimike.service;

import org.springframework.stereotype.Service;

@Service
public class ExampleService {

	public void show() {
		System.out.println("ExampleService.show()");
	}
}

./src/test/java目录下对应的test类代码如下:
@Test标注的方法(testShow)必须满足:public修饰、返回类型void、没有参数

package com.sicimike.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class ExampleServiceTests {

	@Autowired
	private ExampleService exampleService;

	@Test
	public void testShow() {
		exampleService.show();
	}

}
测试controller

./src/main/java目录下controller代码如下:

package com.sicimike.web;

import java.util.HashMap;
import java.util.Map;

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 com.sicimike.service.ExampleService;

@Controller
public class ExampleController {
	
	@Autowired
	private ExampleService exampleService;
	
	@ResponseBody
	@RequestMapping("/show")
	public Map show(String id) {
		
		System.out.println("id is " + id);
		
		exampleService.show();
		
		Map map = new HashMap();
		map.put("code", "200");
		map.put("message", "success");
		
		return map;
	}

}

请求url:GET http://localhost:8080/show?id=123
返回数据:

{
    "code": "200",
    "message": "success"
}

./src/test/java目录下对应的test类代码如下:
@Test标注的方法(testShow)必须满足:public修饰、返回类型void、没有参数

package com.sicimike.web;

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.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class ExampleControllerTests {

	private MockMvc mockMvc;
	
	@Autowired
	WebApplicationContext webApplicationContext;

    @Before
    public void setUp(){
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }
    
    @Test
    public void testShow() throws Exception {

        String url = "/show?id=123";

        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(url)
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        
        System.out.println(mvcResult.getResponse().getContentAsString());

    }

}

你可能感兴趣的:(其它)