具有继承性的单元测试方法控制器调用

Unit test method controller call with inheritance

我有一个 XXXController,它是 baseController 的子类。 我想测试 xxxController,我需要存根来自 baseController 的一个方法调用。

我试过类似的东西:

controller.xxxMethod(_) >> false 

但它不起作用。

有人知道如何使用 Spock 在子类中存根 baseController 方法调用吗?

您正在尝试 mock/stub 正在测试的 class,这是一种反模式。我会告诉您查看 Spies 和部分模拟 (http://spock-framework.readthedocs.org/en/latest/interaction_based_testing.html#partial-mocks),但如您所见,即使是官方文档也说 "Think twice before using this feature."

所以我的建议是重构您的代码,其中被测试的 class 是真实的 class,只有合作者是 mocked/stubbed。