时髦的spock嘲笑春天autowired豆 [英] groovy spock mocking spring autowired beans
问题描述
我有bean:
@Service
公共类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 =模拟(EQueueBookingClient);
def setup(){
testedService.eQueueBookingClient = eQueueBookingClient;
}
和测试方法:
...
setup:
CancelBookingResponse response = new CancelBookingResponse();
...
eQueueBookingClient.cancelBooking(_,_)>>响应;
当:
def result = testedService.cancelBooking(request);
then:
result!= null&& result.bookId == bookId
为什么eQueueBookingClient不会模拟?
当我调试它时:在测试中 - 我看到Mock实例,当我进入方法时 - 我看到了真正的bean实例
非常感谢!
我找到了解决方案!
对于这个客户端,并在设置中使用它:
pre $私人EQueueBookingClient
def setup(){
testedService.setBookingClient(eQueueBookingClient);
$ / code>
如果将客户端定义为public并且使用=它不起作用;
例如:
$ $ p $ testedService.eQueueBookingClient = eQueueBookingClient; //模拟实例不起作用
I have bean:
@Service
public class EQueueBookingService {
@Autowired
public EQueueBookingClient eQueueBookingClient;
And I try to write some test for this bean EQueueBookingService using Spock. https://code.google.com/p/spock/wiki/SpockBasics
My mock is
class EQueueBookingServiceTest extends Specification {
@Autowired
EQueueBookingService testedService;
EQueueBookingClient eQueueBookingClient = Mock(EQueueBookingClient);
def setup() {
testedService.eQueueBookingClient = eQueueBookingClient;
}
and test method:
...
setup:
CancelBookingResponse response = new CancelBookingResponse();
...
eQueueBookingClient.cancelBooking(_, _) >> response;
when:
def result = testedService.cancelBooking(request);
then:
result != null && result.bookId == bookId
Why eQueueBookingClient doesn't mock?
When I debug it: in test - I see Mock instance, when I go to method - I see real bean instance.
Thanks a lot!
I've found solution!
need implement setter for this client and use it in setup like:
private EQueueBookingClient eQueueBookingClient = Mock(EQueueBookingClient);
def setup() {
testedService.setBookingClient(eQueueBookingClient);
}
If define client in service as public and use = it doesn't work; For example:
testedService.eQueueBookingClient = eQueueBookingClient;//mocked instance doesn't work
这篇关于时髦的spock嘲笑春天autowired豆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!