Reputation: 359
I am trying to test my CommentController with a @WithMockUser test, but it seems like it does not find either the user(since the user id (author_id) is null) or content of the comment. I am not sure if the test is done the right way (I am still a beginner), but what I am trying to achieve is the highest code coverage possible, but my previous test does not cover the search of authenticated user and creation of the comment:
mockMvc.perform(get("/post/3/comment").with(authentication(authentication)).content("content")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/post/{id}/"));
package com.paulthemenace.blog.controllers;
import com.paulthemenace.blog.models.Comment;
import com.paulthemenace.blog.models.User;
import com.paulthemenace.blog.repositories.UserRepository;
import com.paulthemenace.blog.services.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import
org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class CommentController {
@Autowired
private UserRepository userRepo;
@Autowired
private CommentService comSrv;
@RequestMapping(value = "/post/{id}/comment")
public String comment(@PathVariable("id") Long id,
@ModelAttribute("comment") Comment comment, Model model) throws
Exception {
Authentication auth =
SecurityContextHolder.getContext().getAuthentication();
if (auth != null) {
String name = auth.getName();
User currentUser = userRepo.findByUsername(name);
if (comment.getContent() != "") {
comment.setAuthor(currentUser);
comSrv.create(comment);
}
}
return "redirect:/post/{id}/";
}
}
CommentControllerTest
import com.paulthemenace.blog.controllers.CommentController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import
org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
@SpringBootTest
@RunWith(SpringRunner.class)
@WebAppConfiguration
public class CommentControllerTest {
@Autowired
private CommentController commentController;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = standaloneSetup(commentController).build();
}
@Test
public void controllerIsNotNull() {
assertThat(commentController).isNotNull();
}
@Test
@WithMockUser(username = "user", password = "user")
public void checkIfContainsAuthenticatedUserAndCreatesComment()
throws Exception {
mockMvc.perform(post("/post/3/comment")
.content("comment
content")).andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/post/{id}/"));
}
}
stacktrace
Is the test even correct? I want this project to be made in test-driven development but I am failing miserably. I googled for resources regarding testing in Spring Boot but none of them helped me solve this matter..
Thanks for your help!
Upvotes: 0
Views: 1719
Reputation: 359
Thanks to the link Barath sent, I managed to fix it and get 100% code coverage on that controller:
@Test
@WithMockUser(username = "sure", password = "sure")
public void checkIfContainsAuthenticatedUserAndCreatesComment() throws Exception {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post("/post/3/comment");
Comment com = new Comment();
String name = auth.getName();
User currentUser = userRepo.findByUsername(name);
com.setAuthor(currentUser);
com.setContent("ahdsf");
request.flashAttr("comment", com);
mockMvc.perform(request).andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/post/{id}/"));
}
What was stupid of me, I didn't notice that it was still reading the users from MYSQL, so i had to insert a 'real' user from the table. Thanks again!
Upvotes: 1