我正在使用Spock fw和Mockito。我有一個名爲'HostController'的控制器和一個名爲'HostService'的服務。基於spock的測試中mockito模擬對象的殘差
'HostController'具有名爲host(Long id)
的方法,'HostService'具有名爲findOne(Long id)
的方法。
我想測試HostController#host(Long id)
,所以我想到了剔除findOne(Long id)
的方法。
遵循的是測試代碼:
class MockTest extends Specification {
@Mock
private HostService mockedService;
@InjectMocks
private HostController controller;
def setup() {
MockitoAnnotations.initMocks(this);
}
def "mock test"() {
given:
def host = new Host(id: 1, ipAddress: "127.0.0.1", hostName: "host1")
mockedService.findOne(_) >> host
when:
Map<String, Object> result = controller.host(1)
then:
result.get("host") != null
}
}
在上面的測試,controller.host(1)
返回Map<String, Object>
類型,它有一個名爲host
的關鍵。我假設這個鍵沒有空值,但它有空值。
爲什麼不按我的想法工作?
它工作得很好。但我想用Mockito代替Spock Mock。 – Devcken
抱歉,抱歉我的堅持,但是,爲什麼? –