controller는 db까지 연결하지 않고 테스트를 진행한다
@RequestMapping(value = "/memberRegister.do", method = RequestMethod.POST)
public ModelAndView memberRegister(ModelAndView mv, @RequestParam("userId") String userId,
@RequestParam("passwd") String passwd, @RequestParam("name") String name,
@RequestParam("email") String email, @RequestParam("tel") String tel) {
HashMap<String, String> hMap = new HashMap<String, String>();
hMap.put("userId", userId);
hMap.put("passwd", passwd);
hMap.put("name", name);
hMap.put("email", email);
hMap.put("tel", tel);
int result = mService.registerMember(hMap);
String str ="";
if(result>0) {
str ="회원가입 성공";
}else {
str ="회원가입 실패";
}
mv.addObject("msg",str);
mv.setViewName("common/errorPage");
return mv;
}
@Override
public int registerMember(HashMap<String, String> hMap) {
// TODO Auto-generated method stub
return 1;
}
테스트 용 이기에 무조건 성공으로 간주하고 1을 반환한다.
//컨트롤러및 웹 항경에서 사용되는 빈을 자동 생성 등록
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {
"file:src/main/webapp/WEB-INF/spring/root-context.xml",
"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"
})
public class MemberControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mocMvc;
@Before
public void setup() {
this.mocMvc= MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testMemberRegist() throws Exception{
try {
mocMvc.perform(
post("/memberRegister.do")
.param("userId", "adminUser")
.param("passwd", "admin1234")
.param("name", "관리자")
.param("email", "admin@user.com")
.param("tel", "1234566")
).andDo(print()).andExpect(status().isOk());
}catch (Exception e) {
// TODO: handle exception
}
}
}
먼저 클래스에 선언할 어노테이션을 기억하자
//컨트롤러및 웹 항경에서 사용되는 빈을 자동 생성 등록
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {
"file:src/main/webapp/WEB-INF/spring/root-context.xml",
"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"
})
어노테이션
@WebAppConfiguration
가 필요하다@RunWith(SpringJUnit4ClassRunner.class)
테스트 용에 들어가는 어노테이션ContextConfiguration
에는 root-context와 servler-context 2개의 위치가 정화기 입력되어 있어야 한다.
@Before //테스트 수행전에 하는 작업이라고 설정하는 어노테이션
public void setup() {
this.mocMvc= MockMvcBuilders.webAppContextSetup(this.wac).build();
}
MockMvc
가 필요하기에 메소드를 이용하여 해당 변수에 값을 넣어준다.
@Test
public void testMemberRegist() throws Exception{
try {
mocMvc.perform(
post("/memberRegister.do")
.param("userId", "adminUser")
.param("passwd", "admin1234")
.param("name", "관리자")
.param("email", "admin@user.com")
.param("tel", "1234566")
).andDo(print()).andExpect(status().isOk());
}catch (Exception e) {
// TODO: handle exception
}
}
andDo(print()).andExpect(status().isOk());
MockHttpServletRequest:
HTTP Method = POST
Request URI = /memberRegister.do
Parameters = {userId=[adminUser], passwd=[admin1234], name=[관리자], email=[admin@user.com], tel=[1234566]}
Headers = []
Body =
Session Attrs = {}Handler:
Type = com.kh.junspring.member.controller.MemberController
Method = com.kh.junspring.member.controller.MemberController#memberRegister(ModelAndView, String, String, String, String, String)Async:
Async started = false
Async result = nullResolved Exception:
Type = nullModelAndView:
View name = common/errorPage
View = null
Attribute = modelAndView
value = ModelAndView [view="common/errorPage"; model={msg=회원가입 성공}]
errors = []
Attribute = msg
value = 회원가입 성공FlashMap:
Attributes = nullMockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Language:"en"]
Content type = null
Body =
Forwarded URL = /WEB-INF/views/common/errorPage.jsp
Redirected URL = null
Cookies = []