2017-09-24 83 views
0

我想用@WithMockUser測試來測試我的CommentController,但它似乎沒有找到用戶(因爲用戶id(author_id)爲空)或評論的內容。我不確定測試是否以正確的方式完成(我仍然是初學者),但是我試圖實現的是可能的最高代碼覆蓋率,但是我之前的測試不包括搜索經過身份驗證的用戶和創建評論:春季啓動測試 - 張貼的內容爲null與@WithMockUser

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}/")); 


     } 


    } 

堆棧跟蹤

https://pastebin.com/2SSKRjwd

該測試是否正確?我希望這個項目能夠在測試驅動的開發環境中完成,但我失敗了。 我搜索了關於在Spring Boot中測試的資源,但他們都沒有幫我解決這個問題..

感謝您的幫助!

+1

請查看https://stackoverflow.com/questions/19430365/how-to-pass-modelattrubute-parameters-using-mockmvc – Barath

回答

1

由於Barath發來的鏈接,我設法解決它,並在該控制器上獲得100%的代碼覆蓋率:

@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}/")); 


} 

什麼是愚蠢的我,我沒有注意到,它仍然在讀MYSQL的用戶,所以我不得不從表中插入一個'真正'的用戶。 再次感謝!