Last active
July 29, 2016 00:09
-
-
Save arahansa/20777ea80ad15094cc5e04decd3bff04 to your computer and use it in GitHub Desktop.
Spring MVC테스트에서 mocikto와 assertj 로 테스트해보는 연습을 하고 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 밸리데이션이나 이늄 세팅같은 부분은 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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")))*/ | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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