Testing controllers

Testing controllers for successful routing and view rendering is a must in order to make sure the web application does what is expected. The following is a test case written for CommentController, which is annotated with @WebMvcTest and has auto-wired MockMvc, which will be used to invoke endpoints for testing:

@RunWith(SpringRunner.class)
@WebMvcTest(CommentController.class)
public class CommentControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private CommentService commentService;

@Test
public void saveComments_HappyPath_ShouldReturnStatus302() throws Exception {
// When
ResultActions resultActions = mockMvc.perform(post("/comment").with(csrf()).with(user("shazin").roles("USER")).param("plusComment", "Test Plus"));

// Then
resultActions
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/"));

verify(commentService, times(1)).saveAll(anyList());
verifyNoMoreInteractions(commentService);
}

@Test
public void getComments_HappyPath_ShouldReturnStatus200() throws Exception {
// Given
Comment comment = new Comment();
comment.setComment("Test Plus");
comment.setType(CommentType.PLUS);
comment.setCreatedBy("Shazin");
comment.setCreatedDate(new Timestamp(System.currentTimeMillis()));

Comment comment2 = new Comment();
comment2.setComment("Test Star");
comment2.setType(CommentType.STAR);
comment2.setCreatedBy("Shahim");
comment2.setCreatedDate(new Timestamp(System.currentTimeMillis()));
List<Comment> comments = Arrays.asList(comment, comment2);
when(commentService.getAllCommentsForToday()).thenReturn(comments);

// When
ResultActions resultActions = mockMvc.perform(get("/").with(user("shazin").roles("USER")));

// Then
resultActions
.andExpect(status().isOk())
.andExpect(view().name("comment"))
.andExpect(model().attribute("plusComments", hasSize(1)))
.andExpect(model().attribute("plusComments", hasItem(
allOf(
hasProperty("createdBy", is("Shazin")),
hasProperty("comment", is("Test Plus"))
)
)))
.andExpect(model().attribute("starComments", hasSize(1)))
.andExpect(model().attribute("starComments", hasItem(
allOf(
hasProperty("createdBy", is("Shahim")),
hasProperty("comment", is("Test Star"))
)
)));

verify(commentService, times(1)).getAllCommentsForToday();
verifyNoMoreInteractions(commentService);
}
}

One test, saveComments_HappyPath_ShouldReturnStatus302, tests the CommentController.createComment method by verifying its successful persistence of comments by invoking the CommentService.saveAll method exactly once, and verifying it does a redirect to the URL /. Furthermore, it verifies there are no more interactions with CommentService after saving all the comments it received.

Another test, getComments_HappyPath_ShouldReturnStatus200, tests the CommentController.index method by verifying it successfully returns mocked comments to the view comment by invoking the CommentService.getAllCommentsForToday method exactly once. Furthermore, it verifies there are no more interactions with CommentService after returning all the comments for the day.

During both tests, a dummy user authentication and authorization are mocked using the following code snippet:

user("shazin").roles("USER")

This code creates a request post processor that will mimic a user with the username shazin and role USER, which will enable successfully accessing protected endpoints. The following Maven dependencies need to be specified to enable testing:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>4.1.4.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.85