由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - EasyMock 真的有用么?
相关主题
请教高手如何用JUnit模拟真实的Servlet容器 (e.g. Jboss里的tomcat), 具体需求请进Urgent help! Java relative file path
A question on easyMock2是否可以通过Java的程序直接导入网页?
在AWS如何作测试紧急求教,JAVA程序如何启动浏览器
问一个blocking IO的程序BufferedWriter里的write()
jmock ?? or any other mocking tools?关于char和int的问题
Re: 如何从键盘输入获得一个float值?谢谢!javaMail的问题
Stupid IBM JDKNo decent way for input password from command line.
怎么从键盘输入整数或float?傻问题,关于java里的资源释放
相关话题的讨论汇总
话题: currency话题: eur话题: string话题: amount
进入Java版参与讨论
1 (共1页)
h*********e
发帖数: 247
1
import java.io.IOException;
public interface ExchangeRate {
double getRate(String inputCurrency, String outputCurrency) throws
IOException;
}
import java.io.IOException;
public class Currency {
private String units;
private long amount;
private int cents;
public Currency(double amount, String code) {
this.units = code;
setAmount(amount);
}
private void setAmount(double amount) {
this.amount = new Double(amount).longValue();
this.cents = (int) ((amount * 100.0) % 100);
}
public Currency toEuros(ExchangeRate converter) {
if ("EUR".equals(units)) return this;
else {
double input = amount + cents/100.0;
double rate;
try {
rate = converter.getRate(units, "EUR");
double output = input * rate;
return new Currency(output, "EUR");
} catch (IOException ex) {
return null;
}
}
}
public boolean equals(Object o) {
if (o instanceof Currency) {
Currency other = (Currency) o;
return this.units.equals(other.units)
&& this.amount == other.amount
&& this.cents == other.cents;
}
return false;
}
public String toString() {
return amount + "." + Math.abs(cents) + " " + units;
}
}
import junit.framework.TestCase;
import org.easymock.EasyMock;
import java.io.IOException;
public class CurrencyTest extends TestCase {
public void testToEuros() throws IOException {
Currency testObject = new Currency(2.50, "USD");
Currency expected = new Currency(3.75, "EUR");
ExchangeRate mock = EasyMock.createMock(ExchangeRate.class);
EasyMock.expect(mock.getRate("USD", "EUR")).andReturn(1.5);
EasyMock.replay(mock);
Currency actual = testObject.toEuros(mock);
assertEquals(expected, actual);
}
}
不管真正的ExchangeRate concrete class在不在, 有没有bug,
上面的Junit根本就没有测试真正的class/method, 其文档说验证方法的调用种类、次
数、顺序,可以令 Mock 对象返回指定的值或抛出指定异常。这我看到了, 但这种测
试到底价值何在?
y***d
发帖数: 2330
2
这个测试的是 Currency 的 toEuros,不是 ExchangeRate

【在 h*********e 的大作中提到】
: import java.io.IOException;
: public interface ExchangeRate {
: double getRate(String inputCurrency, String outputCurrency) throws
: IOException;
: }
: import java.io.IOException;
: public class Currency {
: private String units;
: private long amount;
: private int cents;

g*****g
发帖数: 34805
3
EasyMock does a few things. It mocks your object easily.
It can check both input and output of your method. And
it can verify the sequence of called methods.

【在 h*********e 的大作中提到】
: import java.io.IOException;
: public interface ExchangeRate {
: double getRate(String inputCurrency, String outputCurrency) throws
: IOException;
: }
: import java.io.IOException;
: public class Currency {
: private String units;
: private long amount;
: private int cents;

p******w
发帖数: 62
4
mock是把你不要测的东西mock出去,只是让他提供最基本的返回,在这个例子里面,这
个unit test主要是测toEuro里的逻辑,不用测exchangerate是否正确,所以mock了
exchangerate,让他返回一个你可以控制的值,从而让你判断toEuro是否做了应该做的
事情。

【在 h*********e 的大作中提到】
: import java.io.IOException;
: public interface ExchangeRate {
: double getRate(String inputCurrency, String outputCurrency) throws
: IOException;
: }
: import java.io.IOException;
: public class Currency {
: private String units;
: private long amount;
: private int cents;

1 (共1页)
进入Java版参与讨论
相关主题
傻问题,关于java里的资源释放jmock ?? or any other mocking tools?
我是不是得了某种综合症?Re: 如何从键盘输入获得一个float值?谢谢!
JAVA文本文件读写问题Stupid IBM JDK
刚刚开始学习java,麻烦帮我看一下我哪里错了行吗?谢谢怎么从键盘输入整数或float?
请教高手如何用JUnit模拟真实的Servlet容器 (e.g. Jboss里的tomcat), 具体需求请进Urgent help! Java relative file path
A question on easyMock2是否可以通过Java的程序直接导入网页?
在AWS如何作测试紧急求教,JAVA程序如何启动浏览器
问一个blocking IO的程序BufferedWriter里的write()
相关话题的讨论汇总
话题: currency话题: eur话题: string话题: amount