【问题标题】:Unit test rest controller in kotlin + Spring bootkotlin + Spring boot 中的单元测试休息控制器
【发布时间】:2019-03-27 04:15:11
【问题描述】:

编辑:我创建了另一个类“Utils”并将函数移至该类。

class Utils {
fun isMaintenanceFileExist(maintenanceFile: String) : Boolean {
    /** method to check maintenance file, return True if found else False. */
    return File(maintenanceFile).exists()
}
}

我正在测试 post API 并模拟如下方法:

@Test
fun testMaintenanceMode() {
    val mockUtil = Mockito.mock(Utils::class.java)
    Mockito.`when`(mockUtil.isMaintenanceFileExist("maintenanceFilePath"))
            .thenReturn(true)

    // Request body
    val body = "authId=123&email=a@mail.com&confirmationKey=86b498cb7a94a3555bc6ee1041a1c90a"

    // When maintenance mode is on
    mvc.perform(MockMvcRequestBuilders.post("/post")
            .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
            .content(body))
            .andExpect(MockMvcResultMatchers.status().isBadRequest)
            .andReturn()
    }

但我没有得到预期的结果。

控制器代码:

{
utilObj = Utils()
...
@PostMapping("/post")
fun registerByMail(@Valid data: RequestData) : ResponseEntity<Any>
{

    // check for maintenance mode, if True return (error code : 9001)
    if(utilObj.isMaintenanceFileExist("maintenanceFilePath")) {
        println("-------- Maintenance file exist. Exiting. --------")
        var error = ErrorResponse(Response(ResponseCode.MAINTENANCE,
                ResponseCode.MAINTENANCE.toString()))
        return ResponseEntity.badRequest().body(error)
}
...
}

我想在测试中从 isMaintenanceFileExist() 方法返回 true,并想检查 badRequest。 请指导如何实现这一目标。

【问题讨论】:

    标签: unit-testing spring-boot junit kotlin mockito


    【解决方案1】:

    通过查看您的代码 sn-ps,我猜您实际上并没有在测试中使用模拟的 Controller 实例。控制器由 Spring Boot 测试运行程序实例化,而不是使用您的模拟实例。

    我建议将isMaintenanceFileExist 方法提取到一个单独的bean 中,然后使用@MockBean 模拟它。

    控制器和实用程序 Bean

    @RestController
    class MyController(@Autowired private val utils: Utils) {
    
        @PostMapping("/post")
        fun registerByMail(@RequestBody data: String): ResponseEntity<Any> {
    
            if (utils.isMaintenanceFileExist("maintenanceFilePath")) {
                println("-------- Maintenance file exist. Exiting. --------")
                return ResponseEntity.badRequest().body("error")
            }
            return ResponseEntity.ok("ok")
        }
    
    }
    
    @Component
    class Utils {
        fun isMaintenanceFileExist(maintenanceFile: String) = File(maintenanceFile).exists()
    }
    

    测试类

    @WebMvcTest(MyController::class)
    class DemoApplicationTests {
    
        @MockBean
        private lateinit var utils: Utils
    
        @Autowired
        private lateinit var mvc: MockMvc
    
        @Test
        fun testMaintenanceMode() {
            BDDMockito.given(utils.isMaintenanceFileExist("maintenanceFilePath"))
                    .willReturn(true)
    
            val body = "test"
    
            mvc.perform(MockMvcRequestBuilders.post("/post")
                    .contentType(MediaType.TEXT_PLAIN)
                    .content(body))
                    .andExpect(MockMvcResultMatchers.status().isBadRequest)
        }
    
    }
    

    chapter 44.3.7

    【讨论】:

    • 谢谢。我创建了另一个类,但它仍然没有按预期工作。我已经编辑了上面的内容。抱歉,我在 kotlin 和 spring-boot 方面都是初学者。
    • 我自己快速编写了场景并在我的答案中附上了运行代码。
    猜你喜欢
    • 2018-10-17
    • 1970-01-01
    • 2018-07-31
    • 2018-07-31
    • 2017-03-10
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 2019-02-01
    相关资源
    最近更新 更多