????????????????????????????????????????
????????????????????????web context ???????л?????????module ????DAO??Service??????????applicationContext?????????src/main/resource?????
?????????????????????service?????????????????????Controller????????????????service??DAO??????Service??DAO??applicationContext??????
????Controller ???????
????????????а??????????????????????????????????????????????ContextConfiguration????á?
????@RunWith(SpringJUnit4ClassRunner.class)
????@WebAppConfiguration //?????src/main/webapp
????@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml")
????????????@ContextConfiguration???????servlet-context.xml?????????л?????????????applicationContext??????????????????????????Service??null???
????????
????@ContextConfiguration({
????"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"??
????“classpath*: springxml/**.xml”
????})
??????????????????????????????Controller?????????????????????????????????ο?showcase?е??????
package pairwinter.spring.mvc.controller.test;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.samples.mvc.AbstractContextControllerTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({
"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"??
“classpath*: springxml/**.xml”
})
public class ControllerTests{
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() throws Exception {
this.mockMvc = webAppContextSetup(this.wac).build();
}
@Test
public void controllerExceptionHandler() throws Exception {
this.mockMvc.perform(get("/test"))
.andExpect(status().isOk());
}
}