我是jmock测试的新手,我创建了两个相互使用的类,只是为了让我了解模拟测试... 第一个类叫做计算器,用于计算这是计算器类
public class Calculator {
private int l;
private int u;
public Calculator()
{
l = 0;
u = 0;
}
public int Add(int a, int b)
{
int k;
l = a;
u =b;
k = l+ b;
return k;
}
public int multiply(int a, int b)
{
int o;
l = a;
u = b;
o = l*u;
return o;
}
public int subtract(int a, int b)
{
int y;
l = a;
u = b;
y = l-u;
return y;
}
}
然后使用计算器计算货币的currecny类
public class Currency {
private int yen;
private int euro;
private Calculator ken;
public Currency(Calculator p)
{
yen = 25;
euro = 16;
ken = p;
}
public int getYenvalue(int s)
{
int l = ken.multiply(s,yen);
return l;
}
public int getEuroValue(int pick)
{
int op = ken.multiply(pick, euro);
return op;
}
}
这是我写的JMock测试。问题是我一直收到错误,说从未调用方法add。有任何建议可以帮助解决这个问题吗?
import static org.junit.Assert.*;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(JMock.class)
public class testJMocktest {
Mockery context = new JUnit4Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
@Test
public void testmultipy()
{
final Calculator calc = context.mock(Calculator.class);
Currency currency = new Currency(calc);
final int a = 500;
context.checking(new Expectations() {{
oneOf (calc).multiply(25,a); will(returnValue(12500));
}});
int u = currency.getYenvalue(a);
assertEquals(u,12500);
}
}