我有几个遵循“模板方法”模式的课程。抽象类 A,具体扩展 B 和 C。像这样:
public abstract class A
{
protected abstract String getData() throws SomeException;
public void doWork() throws OtherException
{
try
{
// business logic ...
String data = this.getData();
// more business logic ...
}
catch(SomeException e)
{
log("...", e);
throw new OtherException("...", e);
}
}
}
public Class B extends A
{
protected String getData() throws SomeException
{
// complicated logic relying on lots of dependencies
}
}
public Class C extends A
{
protected String getData() throws SomeException
{
// different but equally complicated logic relying on lots of dependencies
}
}
我想编写一个测试来验证 getData() 何时抛出 SomeException 时抛出 OtherException 。我真的想避免模拟强制 getData() 抛出所需的所有复杂依赖项。我不在乎 getData() 是如何抛出的,我只是想让它抛出。所以我认为部分模拟是我想要的。这就是我所拥有的:
import static org.easymock.EasyMock.*;
....
@Test(expected = OtherException.class)
public void testSomethingOrAnother() throws Exception
{
B target = createMockBuilder(B.class).addMockedMethod("getData").createMock();
expect(target.getData()).andThrow(SomeException.class).once();
replay(target)
try
{
target.doWork(); // expect this to throw OtherException;
}
finally
{
verify(target);
}
}
该测试对我来说看起来不错,但是当我运行它时,我得到了:
java.lang.Exception: Unexpected exception, expected<OtherException> but was<java.lang.RuntimeException>
... deleted for brevity ...
Caused by: java.lang.RuntimeException: Ambiguous name: More than one method are named getData
at org.easymock.internal.ReflectionUtils.findMethod(ReflectionUtils.java:96)
at org.easymock.internal.ReflectionUtils.findMethod(ReflectionUtils.java:64)
at org.easymock.internal.MockBuilder.addMockedMethod(MockBuilder.java:73)
at org.easymock.internal.MockBuilder.addMockedMethods(MockBuilder.java:92)
at com.mycompany.more.packages.BTest(BTest.java:83)
... deleted for brevity ...
... 16 more
需要明确的是:层次结构中的任何地方都没有 getData() 方法的重载。
EasyMock 能做我在这里想做的事情吗?我错过了什么?
相关版本号:
- EasyMock 3.0
- JUnit 4.4
- Java 1.6