繁体   English   中英

Groovy Spock嘲笑弹簧自动装豆

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM