【kotlin】Spring boot controllerはどのようにユニットテストをしますか???


テストが必要なコントローラにforwardがある場合は、ここを見て、テスト方法を理解してください.
Web環境を統合してテストするには、次の方法でMockMvcを作成します.
@Autowired private lateinit var indexViewAction: indexViewAction
private lateinit var mockMvc: MockMvc

@Before
fun before(){
    mockMvc = MockMvcBuilders
            .standaloneSetup(indexViewAction)
            .build()
}

必要なコントロールを注入してstandaloneSetupに入れます
これは独立したテスト方法で行います
まず、ユニットテストを行うクラスを用意し、postとgetのバンドメソッドテストで、直接最後にひっくり返すことができます.
@RunWith(SpringRunner::class)/@SpringBootTest(classes=[Application::class])は、外部の構成が必要です/@SpringBootTest/*特別な構成は必要ありません*/class IndexViewTest{
private lateinit var mockMvc: MockMvc
@Autowired
private lateinit var webApplicationContext: WebApplicationContext

@Before
fun init(){
    /*    mock mvc*/
    mockMvc = MockMvcBuilders
            .webAppContextSetup(webApplicationContext)
            .build()
}

}
皆さんはMockMvcBuildersを見たと思います.このクラスは私たちがMockMvcを作成するのに役立ちます.nameはMockMvcとは何ですか.
//これはMockMvcのドキュメントの説明で、spring mvcのテストサポートを提供します.
/**
  • Main entry point for server-side Spring MVC test support.

  • Example

  • import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  • import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  • import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
  • // …
  • WebApplicationContext wac = …;
  • MockMvc mockMvc = webAppContextSetup(wac).build();
  • mockMvc.perform(get("/form"))
  • .andExpect(status().isOk())
  • .andExpect(content().mimeType(“text/html”))
  • .andExpect(forwardedUrl("/WEB-INF/layouts/main.jsp"));
  • @author Rossen Stoyanchev
  • @author Rob Winch
  • @author Sam Brannen
  • @since 3.2
    */
    现在我们可以开始测试了,先来简单的测试一个get post方法
  • @RunWith(SpringRunner::class)
    /@SpringBootTest(classes = [Application::class]) 需要配外的配置/
    @SpringBootTest /* 不需要格外的配置 */
    class IndexViewTest {

    private lateinit var mockMvc: MockMvc
    @Autowired
    private lateinit var webApplicationContext: WebApplicationContext
    
    @Before
    fun init(){
        /*    mock mvc*/
        mockMvc = MockMvcBuilders
                .webAppContextSetup(webApplicationContext)
                .build()
    }
    
    /*  get  */
    @Test
    fun get(){
        mockMvc
                .get("/index.html") /*  get      url  */
                .andReturn() /*    */
                .response /*    */
                .contentAsString /*    html  */
                .apply(::println) /*    */
    }
    
    /*  post  */
    @Test
    fun post(){
        mockMvc
                .post("/index.html") /*  get      url  */
                .andReturn() /*    */
                .response /*    */
                .contentAsString /*    html  */
                .apply(::println) /*    */
    }
    

    }しかし、多くの場合、私たちのテストはどこがこんなに簡単で、一般的にはパラメータ付きですが、パラメータ付きはどのようにテストしますか?
    ポイント、これがポイント
    @RunWith(SpringRunner::class)/@SpringBootTest(classes=[Application::class])は、外部の構成が必要です/@SpringBootTest/*特別な構成は必要ありません*/class IndexViewTest{
    private lateinit var mockMvc: MockMvc
    @Autowired
    private lateinit var webApplicationContext: WebApplicationContext
    
    @Before
    fun init(){
        /*    mock mvc*/
        mockMvc = MockMvcBuilders
                .webAppContextSetup(webApplicationContext)
                .build()
    }
    
    /*  get  */
    @Test
    fun get(){
        mockMvc
                .get("/index.html") /*  get      url  */
                .andReturn() /*    */
                .response /*    */
                .contentAsString /*    html  */
                .apply(::println) /*    */
    }
    
    /*  post  */
    @Test
    fun post(){
        mockMvc
                .post("/index.html") /*  get      url  */
                .andReturn() /*    */
                .response /*    */
                .contentAsString /*    html  */
                .apply(::println) /*    */
    }
    
    /*    get  */
    @Test
    fun getAndParam(){
        mockMvc
                .perform(
                        MockMvcRequestBuilders.get("/index.html")/*  url  */
                                .param("page","1")/*    */
                                .cookie(Cookie("name","lemon"))/*  cookie*/
                                .header("username","lemon")/*     */
                /*           , .      */
                )
                .andExpect ( /*      */
                    matchAll( /*         ,         andExpect     */
                            status().isOk, /*      ,      */
                            header().exists("Content-Type"), /*       ,             ,          ,      (            )*/
                            content().encoding("utf-8") /*       */
                    )
                )
                .andReturn() /*    */
                .response /*    */
                .contentAsString /*    html  */
                .apply(::println) /*    */
    }
    
    /*    post  */
    @Test
    fun postAndParam(){
        mockMvc
                .perform(
                        MockMvcRequestBuilders.post("/index.html")/*  url  */
                                .param("page","1")/*    */
                                .cookie(Cookie("name","lemon"))/*  cookie*/
                                .header("username","lemon")/*     */
                        /*           , .      */
                )
                .andExpect ( /*      */
                        matchAll( /*         ,         andExpect     */
                                status().isOk, /*      ,      */
                                header().exists("Content-Type"), /*       ,             ,          ,      (            )*/
                                content().encoding("utf-8") /*       */
                        )
                )
                .andReturn() /*    */
                .response /*    */
                .contentAsString /*    html  */
                .apply(::println) /*    */
    }
    

    注意してください.もしあなたが間違って報告してから見に来たら、もっと注意してください.
    import org.springframework.test.web.servlet.ResultMatcher.matchAll import org.springframework.test.web.servlet.request.MockMvcRequestBuilders import org.springframework.test.web.servlet.result.MockMvcResultMatchers.* これらのものは必ず導入しなければならない.
    Spring bootのインタフェーステストの詳細
    ありがとうございます