Mockito。`(....)正在调用实际实现。

时间:2019-05-16 03:36:14

标签: java spring-boot kotlin mockito

我有一个奇怪的问题,我的模拟正在调用代码的实际实现。我正在测试具有简单POST端点的控制器类。在这里,我想逐步解释。请注意,我正在使用Kotlin和Spring靴(春天5)。这是我的控制器类

@RestController
class MyController(val delegate: DelegateTask) { 

    @PostMapping(value = ["/x1/myprofile/xyzz"], produces = [MediaType.APPLICATION_JSON_VALUE])
    fun doSomething(@RequestHeader("id",  required = false) id: String?,
                    @RequestHeader("oid", required = false) oid: String?,
                    @RequestHeader("username") userName: String,
                    @RequestHeader("cache-access", required = false) cacheAccess: String?,
                    @RequestBody(required = true) reqPayload: RequestPayload,
                    request: HttpServletRequest): ResponseObject {
        val result = delegate.myjob(id, oid, userName, cacheAccess, reqPayload)
        return result.responseObject
    }
}

现在来看我的测试用例

@RunWith(SpringRunner::class)
@SpringBootTest
@AutoConfigureMockMvc
class MyControllerTest {

var delegator:DelegateTask = mock()

@Test
    fun testMyController(){
         Mockito.`when`(delegator.myjob("mockid","mockoid","mockUName",
                "cache",mockRequestPayload())).thenReturn(getResourceAsMockResponse().result.responseObj)

 var result = mockMvc.perform(
            MockMvcRequestBuilders.post("/x1/myprofile/xyzz")
                    .header("id", "mockid")
                    .header("oid", "mockoid")
                    .header("username", "someuser")
                    .contentType("application/json")
                    .content(Gson().toJson(mockReqData())))
            .andExpect(MockMvcResultMatchers.status().isOk)
            .andExpect { }
            .andReturn()

}

我打电话时

  

Mockito。when(delegator.myjob(.....))

它正在调用实际方法,即使它应该模拟此方法工作,我也不知道为什么要调用方法的实际实现。

0 个答案:

没有答案