mockito
->테스트를 위한 객체 생성 라이브러리
https://mvnrepository.com/artifact/org.mockito/mockito-core
mockito 사용법(mockito usage)
class RedirectControllerTest
package com.java.web;
import static org.hamcrest.CoreMatchers.any;
import static org.junit.Assert.*;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import javax.inject.Inject;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mock.*;
import org.mockito.*;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.*;
import org.springframework.test.web.servlet.setup.*;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import org.springframework.web.context.WebApplicationContext;
import com.java.dao.users.UserDao;
import com.java.dto.User;
import org.mockito.junit.*;
@RunWith(MockitoJUnitRunner.class)
public class RedirectControllerTest {
@Mock
private UserDao userDao;
@InjectMocks
private UserController userController;
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
this.mockMvc = standaloneSetup(userController).build();
}
@Test
public void crateWhenValid() throws Exception {
this.mockMvc
.perform(post("/users").param("userId", "javagigsi").param("password", "passwords")
.param("name", "자바지기dd").param("email", "test@gmail.com"))
.andDo(print())
// .andExpect(status().isMovedTemporarily())
.andExpect(redirectedUrl("/"));
}
@Test
public void crateWhenInvalid() throws Exception {
this.mockMvc
.perform(post("/users").param("userId", "javagigsi").param("password", "passwords")
.param("name", "자바지기d").param("email", "test"))
.andDo(print()).andExpect(status().isOk()).andExpect(forwardedUrl("users/form"));
}
@Test
public void example() {
User p = mock(User.class);
assertTrue(p != null);
}
// 만약 예외를 던지고 싶으면 어떻게 할까요? 이럴때는 doThrow() 메소드를 활용합시다.
@Test(expected = IllegalArgumentException.class)
public void example2() {
User p = mock(User.class);
doThrow(new IllegalArgumentException()).when(p).setName("JDM");
String name = "JDM";
p.setName(name);
}
// verifiy()는 해당 구문이 호출 되었는지를 체크합니다.
// 단순한 호출뿐만 아니라 횟수나 타임아웃 시간까지 지정해서 체크해 볼 수 있습니다.
@Test
public void example3() {
User p = mock(User.class);
String name = "JDM";
p.setName(name);
// n번 호출했는지 체크
verify(p, times(1)).setName(name); // success
// 호출 안했는지 체크
verify(p, never()).getName(); // success
verify(p, never()).setName("ETC"); // success
verify(p, never()).setName("JDM"); // fail
// 최소한 1번 이상 호출했는지 체크
verify(p, atLeastOnce()).setName(name); // success
// 2번 이하 호출 했는지 체크
verify(p, atMost(2)).setName(name); // success
// 2번 이상 호출 했는지 체크
verify(p, atLeast(2)).setName(name); // fail
// 지정된 시간(millis)안으로 메소드를 호출 했는지 체크
verify(p, timeout(100)).setName(name); // success
// 지정된 시간(millis)안으로 1번 이상 메소드를 호출 했는지 체크
verify(p, timeout(100).atLeast(1)).setName(name); // success
}
}
INFO : org.springframework.mock.web.MockServletContext - Initializing Spring FrameworkServlet '' MockHttpServletRequest: Handler: Async: Resolved Exception: ModelAndView: FlashMap: MockHttpServletResponse: MockHttpServletRequest: Handler: Async: Resolved Exception: ModelAndView: FlashMap: MockHttpServletResponse: |
댓글 ( 4)
댓글 남기기