@RunWith(MockitoJUnitRunner.class)
public class MockPostControllerTest {
private static final Logger log = LoggerFactory.getLogger(MockPostControllerTest.class);
ObjectMapper objectMapper = new ObjectMapper();
private BlogService blogService;
Pageable pageable = mock(PageRequest.class);
PostController postController;
public static void beforeClass() {
log.debug("==================before class=========================");
public static void afterClass() {
log.debug("==================after class=========================");
log.debug("==================before test case=========================");
MockitoAnnotations.initMocks(this);
mvc = standaloneSetup(postController)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.setViewResolvers(new ViewResolver() {
public View resolveViewName(String viewName, Locale locale) throws Exception {
return new MappingJackson2JsonView();
log.debug("==================after test case=========================");
public void savePost() throws Exception {
PostForm post = Fixtures.createPostForm("First Post", "Content of my first post!");
when(blogService.savePost(any(PostForm.class))).thenAnswer(new Answer<PostDetails>() {
public PostDetails answer(InvocationOnMock invocation) throws Throwable {
PostForm fm = (PostForm) invocation.getArgumentAt(0, PostForm.class);
PostDetails result = new PostDetails();
result.setTitle(fm.getTitle());
result.setContent(fm.getContent());
result.setCreatedDate(new Date());
mvc.perform(post("/api/posts").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(post)))
.andExpect(status().isCreated());
verify(blogService, times(1)).savePost(any(PostForm.class));
verifyNoMoreInteractions(blogService);
public void retrievePosts() throws Exception {
PostDetails post1 = new PostDetails();
post1.setTitle("First post");
post1.setContent("Content of first post");
post1.setCreatedDate(new Date());
PostDetails post2 = new PostDetails();
post2.setTitle("Second post");
post2.setContent("Content of second post");
post2.setCreatedDate(new Date());
when(blogService.searchPostsByCriteria(anyString(), any(Post.Status.class), any(Pageable.class)))
.thenReturn(new PageImpl(Arrays.asList(post1, post2), new PageRequest(0, 10, Direction.DESC, "createdDate"), 2));
MvcResult response = mvc.perform(get("/api/posts?q=test&page=0&size=10"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content[*].id", hasItem(1)))
.andExpect(jsonPath("$.content[*].title", hasItem("First post")))
verify(blogService, times(1))
.searchPostsByCriteria(anyString(), any(Post.Status.class), any(Pageable.class));
verifyNoMoreInteractions(blogService);
log.debug("get posts result @" + response.getResponse().getContentAsString());
public void retrieveSinglePost() throws Exception {
PostDetails post1 = new PostDetails();
post1.setTitle("First post");
post1.setContent("Content of first post");
post1.setCreatedDate(new Date());
when(blogService.findPostById(1L)).thenReturn(post1);
mvc.perform(get("/api/posts/1").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("id").isNumber());
verify(blogService, times(1)).findPostById(1L);
verifyNoMoreInteractions(blogService);
public void removePost() throws Exception {
when(blogService.deletePostById(1L)).thenReturn(true);
mvc.perform(delete("/api/posts/{id}", 1L))
.andExpect(status().isNoContent());
verify(blogService, times(1)).deletePostById(1L);
verifyNoMoreInteractions(blogService);
when(blogService.findPostById(1000L)).thenThrow(new ResourceNotFoundException(1000L));
mvc.perform(get("/api/posts/1000").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
log.debug("exception caught @" + ex);