- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 Spock 集成测试来测试我的 Java 应用程序。对于其中一种方法,调用将转到真正的方法“bMethod()”,而不是返回 stub 值。它对于另一个方法“aMethd()”工作正常,并且返回 stub 值并且不调用实际方法。这两个方法都在同一个类 Calc 中。有没有办法调查 Spock 正在做什么以及为什么这样做?
public interface CalcIntf {
public int aMethod(int a, int b);
public int bMethod(int a, int b, int c);
}
MyTestSpec extends Specification {
def "aMethod test"() {
given:
...
CalcIntf calcIntf = Mock(CalcIntf)
calcIntf.aMethod(_,_) >> { return 100; }
when:
//service call
then:
// asserts go here
}
def "bMethod test"() {
given:
...
CalcIntf calcIntf = Mock(CalcIntf)
calcIntf.bMethod(_,_,_) >> { return 100; }
when:
// service call
then:
// asserts go here
}
}
最佳答案
所以首先我让 m1
和 m2
返回 int
而不是 void
。那么至少你可以检查结果。然后我还确保这些方法在没有参数的情况下正确调用,正如克里斯已经提到的那样。所以现在的代码如下所示:
package de.scrum_master.stackoverflow.q62269054
import spock.lang.Specification
class MyTestSpec extends Specification {
def "testing addition using aMethod"() {
given:
Math math = new Math()
CalcIntf calcIntf = Mock(CalcIntf)
calcIntf.aMethod(_, _) >> { return 123 }
expect:
math.m1() == 123
}
def "testing addition using bMethod"() {
given:
Math math = new Math()
CalcIntf calcIntf = Mock(CalcIntf)
calcIntf.bMethod(_, _, _) >> { return 456 }
expect:
math.m2() == 456
}
interface CalcIntf {
int aMethod(int a, int b)
int bMethod(int a, int b, int c)
}
static class Calc implements CalcIntf {
int aMethod(int a, int b) {
return a + b
}
int bMethod(int a, int b, int c) {
return a + b + c
}
}
static class Math {
Calc calc = new Calc()
int m1() {
calc.aMethod(1, 2)
}
int m2() {
calc.bMethod(1, 2, 3)
}
}
}
当然,测试失败了:
Condition not satisfied:
math.m1() == 123
| | |
| 3 false
...
Condition not satisfied:
math.m2() == 456
| | |
| 6 false
...
失败的原因是您没有在任何地方注入(inject)模拟,但是 Math
通过 Calc calc = new Calc 使用硬编码的
.Calc
实例()
所以我们需要一种方法来注入(inject)模拟,例如通过构造函数或 setter 。您还应该将字段类型从 Calc
更改为 CalcIntf
,否则无法注入(inject)接口(interface)模拟。如果您不在代码中使用接口(interface)而是针对具体子类进行编码,为什么还要创建接口(interface)?
package de.scrum_master.stackoverflow.q62269054
import spock.lang.Specification
class MyTestSpec extends Specification {
def "testing addition using aMethod"() {
given:
CalcIntf calcIntf = Mock(CalcIntf)
calcIntf.aMethod(_, _) >> { return 123 }
Math math = new Math(calcIntf)
expect:
math.m1() == 123
}
def "testing addition using bMethod"() {
given:
CalcIntf calcIntf = Mock(CalcIntf)
calcIntf.bMethod(_, _, _) >> { return 456 }
Math math = new Math(calcIntf)
expect:
math.m2() == 456
}
interface CalcIntf {
int aMethod(int a, int b)
int bMethod(int a, int b, int c)
}
static class Calc implements CalcIntf {
int aMethod(int a, int b) {
return a + b
}
int bMethod(int a, int b, int c) {
return a + b + c
}
}
static class Math {
CalcIntf calcIntf
Math(CalcIntf calcIntf) {
this.calcIntf = calcIntf
}
int m1() {
calcIntf.aMethod(1, 2)
}
int m2() {
calcIntf.bMethod(1, 2, 3)
}
}
}
现在测试通过了,但它们不会重现您所描述的行为:
For one of the methods, the call is going to the real method 'bMethod()', instead of the stubbed valued being returned.
我可以想象发生这种情况的一个原因:您想要 stub 的方法是final
,这意味着实现模拟的动态代理无法覆盖它,因此不会 stub 它。但这与您在代码中描述的情况完全不同。
看到了吗?如果您的示例代码没有重现问题,那么它是完全无用的,这就是为什么在我的评论中我告诉您始终提供 MCVE 而不是一些随机伪代码。如果有人报告您的代码中存在错误,您还想知道如何重现它。
因此,为了重现您报告的问题,我必须做出有根据的猜测,因为您的代码没有帮助。只需在之前的版本中更改以下内容即可:
int bMethod(int a, int b, int c)
→ final int bMethod(int a, int b, int c)
Mock(CalcIntf)
→ Mock(Calc)
在 2 个位置现在测试结果是:一种通过,一种失败的功能方法:
Condition not satisfied:
math.m2() == 456
| | |
| 6 false
...
关于java - Spock模拟调用模拟类的真实方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62269054/
我正在寻找x86 Assembly中pow(real, real)的实现。我也想了解算法的工作原理。 最佳答案 只需将其计算为2^(y*log2(x))即可。 有一个x86指令FYL2X计算y *
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
风格指南的最后一点 http://www.python.org/dev/peps/pep-0008 读... 不要使用 == 将 bool 值与 True 或 False 进行比较。 为什么? 编辑只
我似乎无法完成这件事。我仍然在我的日志中看到 cloudflare IP。目前,我有一个负载均衡器,它位于 Cloudflare 后面。 目前,这是与 forwardfor 相关的块: opt
此代码行选择任何类名不是“id”和“quantity”的 div 内的所有子输入:: $("div.item > div:not(.id,.quantity) > :input").live("key
我在测试真实产品时遇到错误。当我单击购买按钮时,出现错误“此版本的应用程序未配置为市场计费”。 我用这个例子https://github.com/robotmedia/AndroidBillingLi
到目前为止我能找到的所有答案都建议调用 omp_set_num_threads。虽然在大多数情况下这是一个正确的答案,但它对我不起作用。在内部,调用 omp_set_num_threads 会导致创建
假设我有 3 个显示器。如何仅通过其索引获取第二个句柄? EnumDisplayMonitors() 不会工作,因为它也枚举了伪设备,而 EnumDisplayDevices() 没有给我句柄。 最佳
我一直在尝试制作一个简单的小游戏来测试我的逻辑,这是一个简单的迷宫,它很丑,而且到目前为止很糟糕。 引擎工作得很好,考虑到迷宫已经存在(矩阵),它甚至可以愉快,但我无意绘制一堆 map ,这可能是在
Cloudflare 代理包含一个名为 CF-Connecting-IP 的 header 和用户的真实 IP。我想让 traefik 读取这个 header 并用它的内容创建一个 X-Real-Ip
我想要上下文菜单中的不同菜单项,具体取决于我在 JTable 中单击的行 大多数示例并没有真正显示上下文菜单(应该根据上下文 - 所选行进行填充) 我尝试过这个: popupMenu = ne
我有一个对象 callInst。如何获取函数的真实名称而不是 IR 代码中的名称?如果我在我的通行证中运行此代码(Useless 在另一个问题中发布) StringRef get_function_n
我在 Appium 和 iPad 2 上使用了以下所需的功能 DesiredCapabilities capabilities = new DesiredCapabilities(); capabil
根据documentation 我们可以通过以下方式在模拟器上运行我们的 android 项目: cordova 运行 android 或 cordova emulate android 但是如何在真
在 ASP.NET 中,有没有办法获得真正的原始 URL? 例如,如果用户浏览到“http://example.com/mypage.aspx/%2F”,我希望能够获得“http://example.
我的 NSInputStream 遇到问题。这是我的代码: case NSStreamEventHasBytesAvailable: printf("BYTE AVAILABLE\n"
$(selector).click() 结果没有任何反应。 this answer在浏览器控制台中工作,javascript 上下文设置为 iframe,但不是主页: simulateMouseCli
我在我的 AB Micro820 PLC 中设置了 modbus 映射。我在 40001 中有一个数组用于写入,在 42001 中有一个数组用于读取。两者都是 200 个元素和 REAL 类型(32
我正在寻找有关设计契约(Contract)管理数据模型的建议。因此,合约的一般生命周期是: 契约(Contract)已创建并处于“草稿”状态。它可以在内部查看,并且可以进行更改。 契约(Contrac
我正在尝试让我的 WebView fullscreen 达到标准,我知道如何将它拉伸(stretch)到“全屏”,但我想做的是当您长按 WebView 并选择全屏选项时,创建全屏。有谁知道该怎么做?
我是一名优秀的程序员,十分优秀!