title: Spring Boot tests - posted content is null with @WithMockUser
body: <p>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:          </p>
  
  <p><code>mockMvc.perform(get("/post/3/comment").with(authentication(authentication)).content("content")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/post/{id}/"));</code></p>
  
  <pre><code>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}/";
  }
  
  }
  </code></pre>
  
  <p>CommentControllerTest</p>
  
  <pre><code>    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}/"));
  
  
          }
  
  
      }
  </code></pre>
  
  <p>stacktrace</p>
  
  <p><a href="https://pastebin.com/2SSKRjwd" rel="nofollow noreferrer">https://pastebin.com/2SSKRjwd</a></p>
  
  <p>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..</p>
  
  <p>Thanks for your help!</p>