[英]groovy spock mocking spring autowired beans
我有豆:
@Service
public class EQueueBookingService {
@Autowired
public EQueueBookingClient eQueueBookingClient;
我嘗試使用Spock為此bean EQueueBookingService編寫一些測試。 https://code.google.com/p/spock/wiki/SpockBasics
我的模擬是
class EQueueBookingServiceTest extends Specification {
@Autowired
EQueueBookingService testedService;
EQueueBookingClient eQueueBookingClient = Mock(EQueueBookingClient);
def setup() {
testedService.eQueueBookingClient = eQueueBookingClient;
}
和測試方法:
...
setup:
CancelBookingResponse response = new CancelBookingResponse();
...
eQueueBookingClient.cancelBooking(_, _) >> response;
when:
def result = testedService.cancelBooking(request);
then:
result != null && result.bookId == bookId
為什么eQueueBookingClient不模擬?
當我調試它時:在測試中-我看到了Mock實例,當轉到方法時-我看到了真正的bean實例。
非常感謝!
我找到了解決方案!
需要該客戶端的實現設置器,並在設置中使用它,例如:
private EQueueBookingClient eQueueBookingClient = Mock(EQueueBookingClient);
def setup() {
testedService.setBookingClient(eQueueBookingClient);
}
如果將服務中的客戶端定義為公共客戶端並使用=則無法使用; 例如:
testedService.eQueueBookingClient = eQueueBookingClient;//mocked instance doesn't work
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.