Kotlin+SpringBoot+Junit5 代码片段

1,232 阅读1分钟
package com.cms.core.controller

import com.cms.core.CoreApplicationTests
import com.cms.fileserver.FileServerApplication
import org.junit.jupiter.api.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import java.util.logging.Logger

/**
 *
 * @author  Yonglong Xue
 * @date    2020-02-04
 */

@SpringBootTest(classes = [FileServerApplication::class])
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class FileControllerTest {

    @Autowired
    private lateinit var mockMvc : MockMvc

    private val log: Logger = Logger.getLogger(CoreApplicationTests::class.java.getName())

    @Test
    fun contextLoads() {
        mockMvc.perform(post("/file/upload").accept(MediaType.MULTIPART_FORM_DATA_VALUE))
                .andExpect(status().isOk)
    }

    @BeforeAll
    fun setup() {
        log.info("@BeforeAll - executes once before all test methods in this class")
    }

    @BeforeEach
    fun init() {
        log.info("@BeforeEach - executes before each test method in this class")
    }

    @DisplayName("Single test successful")
    @Test
    fun testSingleSuccessTest() {
        log.info("Success")
    }

    @Test
    @Disabled("Not implemented yet.")
    fun testShowSomething() {
    }

    @AfterEach
    fun tearDown() {
        log.info("@AfterEach - executed after each test method.")
    }

    @AfterAll
    fun done() {
        log.info("@AfterAll - executed after all test methods.")
    }

}