Skip to content

Instantly share code, notes, and snippets.

@arahansa
Last active July 29, 2016 00:09
Show Gist options
  • Save arahansa/20777ea80ad15094cc5e04decd3bff04 to your computer and use it in GitHub Desktop.
Save arahansa/20777ea80ad15094cc5e04decd3bff04 to your computer and use it in GitHub Desktop.
Spring MVC테스트에서 mocikto와 assertj 로 테스트해보는 연습을 하고 있습니다.
// 밸리데이션이나 이늄 세팅같은 부분은 AOP로 빠졌다.
@PostMapping("/{boardType}/form")
public String postCreate(
@Valid @ModelAttribute BoardArticle article,
BindingResult result,
@PathVariable BoardType boardType){
final BoardArticle save = boardArticleService.save(article);
log.debug("saved Article : {}", save);
return article.getBoardType().getRedirectionPage();
}
public class BoardArticleFormControllerTest extends TestWebConfig {
@Autowired
private BoardArticleRepository boardArticleRepository;
final static private String basicPath="/board/halloffame";
@Test
public void writeTest() throws Exception{
final long count = boardArticleRepository.count();
final ResultActions perform = mockMvc.perform(
post(basicPath+"/form")
.param("anonymousUserName", "hello")
.param("title", "hello title")
.param("content", "helloworld")
.with(csrf())
);
check매처들받아테스트하고_프린트(perform, status().is3xxRedirection());
assertEquals("카운트 1 증가 ", count + 1, boardArticleRepository.count(), 1L);
final ResultActions afterPerform = mockMvc.perform(get(basicPath));
check매처들받아테스트하고_프린트(afterPerform
, status().isOk()
, view().name(BoardType.HALLOFFAME.getListPageName())
, model().attributeExists("boardType")
, model().attribute("boardType", is(BoardType.HALLOFFAME))
, model().attributeExists("pageinfo")
// pageinfo의 content 안에 게시글 컬렉션이 있는데 어떻게 테스트 할지 감이 안 오네요.
/*,model().attribute("pageinfo",
hasProperty("content", hasValue("hello title")))*/
);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {UnitTestContext.class})
public class BoardArticleFormControllerMockTest {
@Resource
private Validator validator;
private BoardArticleFormController baFormController;
private BoardArticleService baServiceMock;
private ModelMapper modelMapper= new ModelMapper();
@Before
public void setUp() {
baFormController = new BoardArticleFormController();
baServiceMock = mock(SimpleBoardArticleService.class);
ReflectionTestUtils.setField(baFormController, "boardArticleService", baServiceMock);
}
@Test
public void createTest() throws Exception{
BoardArticle boardArticle = new BoardArticle("test title", "test content", "test user", BoardType.HALLOFFAME);
BoardArticle savedArticle = modelMapper.map(boardArticle, BoardArticle.class);
savedArticle.setId(1L);
when(baServiceMock.save(boardArticle)).thenReturn(savedArticle);
MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", "/board/halloffame/form");
BindingResult bindingResult = bindAndValidate(mockRequest, boardArticle);
final String s = baFormController.postCreate(boardArticle, bindingResult, BoardType.HALLOFFAME);
verify(baServiceMock).save(boardArticle);
assertThat(s).isEqualTo(BoardType.HALLOFFAME.getRedirectionPage());
}
private BindingResult bindAndValidate(HttpServletRequest request, Object formObject) {
WebDataBinder binder = new WebDataBinder(formObject);
binder.setValidator(validator);
binder.bind(new MutablePropertyValues(request.getParameterMap()));
binder.getValidator().validate(binder.getTarget(), binder.getBindingResult());
return binder.getBindingResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment