Spring 4.0 JUnit????Controller????
???????????? ???????[ 2015/7/2 16:04:47 ] ???????????????
??????Dao??Service?????????????????????д???BasicWebTest?????????????
????@WebAppConfiguration
????@ContextConfiguration(locations= {"classpath:spring/applicationContext.xml"??"classpath:spring/spring-servlet.xml"})
????public class BasicWebTest extends AbstractTransactionalJUnit4SpringContextTests {
????@Autowired
????protected WebApplicationContext wac;
????protected MockMvc mvc;
????@Before
????public void setUp() {
????this.mvc=MockMvcBuilders.webAppContextSetup(wac).build();
????}
????}
????BasicWebTest ???????? @WebAppConfiguration ?????????????????Щ????????Ρ???????ɡ?
???????????????£?
package com.suyin.controller;
import java.util.List;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import com.jayway.jsonpath.JsonPath;
import com.suyin.BasicWebTest;
import com.suyin.pojo.People;
@SuppressWarnings("unchecked")
public class PeopleControllerTest extends BasicWebTest {
@Test
public void queryAll() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/people/queryAll"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("people/list"))
.andExpect(MockMvcResultMatchers.request().attribute("list"?? new MatcherAdapter() {
@Override
public void describeMismatch(Object item?? Description mismatchDescription) {
mismatchDescription.appendText("wrong list size");
}
@Override
public boolean matches(Object item) {
List<People> list=(List<People>) item;
return list.size()==4;
}
}));
}
@Test
public void asynQueryAll() throws Exception {
MvcResult mr=mvc.perform(MockMvcRequestBuilders.get("/people/asynQueryAll").content("{"id":1}"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.id").exists())
.andReturn();
MockHttpServletResponse response=mr.getResponse();
MockHttpServletRequest request=mr.getRequest();
}
@Test
public void update1() throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/people/update").param("id"?? "29")
.param("name"?? "Jack")
.param("address"?? "japan")
.param("age"?? "19"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("people/detail"));
}
@Test
public void update2() throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/people/update")
//.param("name"?? "Jack")
.param("address"?? "japan")
.param("age"?? "19"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("error"));
}
@Test
public void add1()throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/people/add")
.param("name"?? "JJJJ")
.param("address"?? "")
.param("age"?? "1")
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("people/detail"));
}
@Test
public void add2()throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/people/add")
.param("address"?? "")
.param("age"?? "1")
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("error"));
}
abstract class MatcherAdapter<T> implements Matcher<T>{
@Override
public void describeTo(Description arg0) {
// TODO Auto-generated method stub
}
@Override
public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
// TODO Auto-generated method stub
}
@Override
public abstract void describeMismatch(Object item??
Description mismatchDescription);
@Override
public abstract boolean matches(Object item) ;
}
}
???????????????Controller??Spring???????????????????MockMvc??API?????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11