[英]How to Mock Static Java methods with JRuby and Mocha?
我的目标是使用JRuby和Mocha对带有静态实用程序方法的遗留Java代码进行单元测试。 这可能吗?
我正在尝试应用JMockit中可用的类似技术。 一个利用代理来动态更改测试行为的框架。 纯粹主义者可能建议重构静态方法,但目前尚不可能。
我整理了两个单元测试:
测试#1-Ruby Mocking-我的控制。
测试2-Java模拟-看看我是否可以在Java中完成相同的工作。
对于这两个测试,我都将替换“ getTax”方法。
这是代码:
require 'rubygems'
require 'dust'
require 'mocha'
unit_tests do
# Test 1 - Stub out JRuby secondary class and override the "getTax" method
# This is my "control" - I'm overriding the tax multiplier from 1.15 to 1.18.
# It works =)
test "RubyMocking" do
# System Under Test
class RInvoice
def initialize
@util = RInvoiceUtil.new
end
def calculate
@util.getTax * 10.0
end
end
# Dependency
class RInvoiceUtil
# we'll stub out this method and change the tax rate
def getTax
1.15
end
end
invoice = RInvoice.new
# change the tax rate
RInvoiceUtil.any_instance.stubs(:getTax).returns(1.18)
assert_equal(invoice.calculate.to_s, "11.8")
end
end
include Java
require 'rubygems'
require 'dust'
require 'mocha'
require 'invoice.jar'
Invoice = Java::example.Invoice
InvoiceUtil = Java::example.InvoiceUtil
unit_tests do
# Test 2 - Stub out Java class InvoiceUtil and it's *static* getTax method.
# This can be achieved via JMockit, but is it possible in Mocha?
test "JavaMocking" do
invoice = Invoice.new
# this does not work because the ruby objects are only
# proxies to the java objects?
InvoiceUtil.any_instance.stubs(:getTax).returns(1.18)
assert_equal(invoice.calculate, 11.8)
end
end
. 测试2失败,并出现: 。 好的,所以我不能这样做。 嗯 是不是因为ruby对象只是java对象的代理而不能工作的原因?
package example;
public class Invoice {
public double calculate() {
return InvoiceUtil.getTax() * 10.0;
}
}
public class InvoiceUtil {
public static double getTax() {
return 1.15;
}
}
我要做的就是将静态方法模拟与JRuby结合起来,以利用(1)轻松编写脚本以及(2)灵活测试隔离。
预先感谢您的回复!
我知道这个问题是很久以前提出的,但是我遇到了一个非常类似的问题,终于使它起作用了。 我认为问题出在您使用any_instance ...
至少在我的方案中,解决方案是直接在包含静态方法的类上调用存根:
InvoiceUtil.stubs(:getTax).returns(1.18)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.