我有几个类遵循“模板方法”模式。抽象类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()何时抛出抛出OtherException的SomeException。我真的想避免模仿强制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能够做我想在这里做的事情吗?我错过了什么?
相关版本号:
答案 0 :(得分:2)
我认为您的问题可能是使用addMockedMethod(String)。如果没有重载,不确定EasyMock为什么抱怨模糊的方法名称。但以下对我有用:
@Test
public void testSomethingOrAnother() {
B target = null;
try {
target = EasyMock.createMockBuilder(B.class).addMockedMethod(B.class.getDeclaredMethod("getData")).createMock();
EasyMock.expect(target.getData()).andThrow(new SomeException());
EasyMock.replay(target);
} catch (NoSuchMethodException e) {
fail(e.getMessage());
} catch (SomeException e) {
fail(e.getMessage());
}
try {
target.doWork();
fail("doWork should have thrown an exception");
} catch (OtherException e) {
//pass
}
}
答案 1 :(得分:0)
使用Easymock:3.2,您可以指定方法参数的类型。看看IMockBuilder#addMockedMethod(String methodName,Class<?>... parameterTypes)
感谢。