??????????????????????У????????Maven?н????????????????????????????????????д???????飬???????е????????????????е????С?
???????Controller??Service??Dao??????????????????Service??Dao???е??????????Controller?????????????????Tomcat?????н??????????????????????????????????????????Controller??????е??????????????????Controller??????????
????????????SpringMVC??? ??SpringMVC?????????????????
????1.????????????
????2.????Spring??????????

????@RunWith(SpringJUnit4ClassRunner.class)? //???????Spring?????????
????@WebAppConfiguration//????javaWEB?????????????????ServletContext Bean???
????@ContextConfiguration(locations = {"classpath:spring-context.xml"??"classpath:spring-mvc.xml"})//????Spring???????
????public class HealthArticleControllerTest {...??????? }//?????????
???????
????@WebAppConfiguration????????????????????WEB???Щ???????????????????????
????@ContextConfiguration?У??????????Spring?????????????????????????е??????Spring ??xml?????????? ?????xml?????resources??????С?
????3.????spring????Controller??????????????Server?????У??????class?н???WEB????????????
@Autowired
HealthArticleController healthArticleController;
@Autowired
ServletContext context;
MockMvc mockMvc;
@Before
public void setup(){
mockMvc = MockMvcBuilders.standaloneSetup(healthArticleController).build();
}
????HealthArticleController????????????Controller
????MockMvc??SpringMVC????Controller??????
??????ν??е???????????????????@Before?е?setup???????????healthArticleController????????????
???????????????????Controller???????MockMvcBuilders.standaloneSetup(xxxxController).build(); ??????????????@RequestMapping·????????No mapping found for HTTP request with URI [/cms/app/getArticleList] in DispatcherServlet
????4.????????????????????????д?????????????
@org.junit.Test
public void getArticleListTest(){
String postJson = healthArticleController.findPage();
//????????
ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/cms/app/getArticleList").accept(MediaType.APPLICATION_JSON).param("criJson"??postJson));
MvcResult mvcResult = resultActions.andReturn();
String result = mvcResult.getResponse().getContentAsString();
System.out.println("=====??????÷???????:" + result);
}
????ResultAction?????????Browser????FORM?????????post()???????????accept()????????? param()??????????????ж??????????????????param()
????MvcResult???÷???????Response?????
????5.???????????????Controller?????????ɡ????????????????
package com.chinare.health.modules.cms;
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.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.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import javax.servlet.ServletContext;
import java.util.Map;
/**
* author: Samuel
* Date:2015-07-04
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring-context.xml"?? "classpath:spring-context-jedis.xml"?? "classpath:spring-mvc.xml"?? "classpath:spring-context-shiro.xml"?? "classpath:spring-context-activiti.xml"})
public class HealthArticleControllerTest {
@Autowired
HealthArticleController healthArticleController;
@Autowired
ServletContext context;
MockMvc mockMvc;
@Before
public void setup(){
mockMvc = MockMvcBuilders.standaloneSetup(context).build();
}
@org.junit.Test
public void getArticleListTest() throws Exception{
//???????
String postJson = "{"mac":"h"??"dtClient":"2015-06-03 13:20:10"}";
//????????
ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/cms/app/getArticleList")
.accept(MediaType.APPLICATION_JSON).param("criJson"??postJson));
MvcResult mvcResult = resultActions.andReturn();
String result = mvcResult.getResponse().getContentAsString();
System.out.println("=====??????÷???????:" + result);
}
}