EasyMock 3.1 InvocationTargetException

时间:2014-07-16 20:55:56

标签: java unit-testing easymock

我正在使用easymock和junit编写一个应该相当简单的测试。我已经为现有项目添加了功能,我正在测试我添加的方法是否按预期工作。我无法发布详细代码(它的工作原理),但这是一个名称已更改的版本:

public class ATest extends AbstractTestCase {
    private AFactory factory;
    private AServiceClient mockClient;
    private AServiceResult mockResult;
    private AnObject mockObject;
    private AnotherObject mockObject2;
    private Map<String, Object> mockMap;

    @Before
    public void setupMocks() {
        factory = new AFactory();
        mockClient = EasyMock.createMockBuilder(AServiceClient.class)
                .addMockedMethod("getAServiceResult")
                .createMock();
        factory.setClient(mockClient);
        mockResult = EasyMock.createMockBuilder(AServiceResult.class)
                .addMockedMethod("getAnObject")
                .createMock();
        mockObject = EasyMock.createMockBuilder(AnObject.class)
                .addMockedMethod("getAnotherObject")
                .createMock();
        mockObject2 = EasyMock.createMockBuilder(AnotherObject.class)
                .addMockedMethod("getMap")
                .createMock();

        mockMap = new HashMap<String, Object>();
        //Put a bunch of stuff in the map

        EasyMock.expect(mockObject2.getMap())
                .andReturn(mockMap)
                .once();
        EasyMock.expect(mockObject.getAnotherObject())
                .andReturn(mockObject2)
                .once();
        EasyMock.expect(mockResult.getAnObject())
                .andReturn(mockObject)
                .once();
    }

    @Test
    public void aTest throws SomeException {
        AnInstance inst = factory.getInstance();
        SomeInput input = SomeInput();
        //Populate input

        Set<String> expectedInnerInput = new HashSet<String>();
        //Populate expected inner input
        EasyMock.expect(mockClient.getAServiceResult(eq(expectedInnerInput), eq("1")))
                .andReturn(mockResult)
                .once();

        EasyMock.replay(mockClient, mockResult, mockObject, mockObject2);
        Map<SomeKey, Object> attrs = inst.getAttributes("a string", input);
        EasyMock.verify(mockClient, mockResult, mockObject, mockObject2);

        //A bunch of junit asserts on attrs
    }
}

(上面可能有拼写错误。假设这些错误来自我在这里输入。我的代码能够编译并运行,它一直到EasyMock.replay和{{1}之间的界限}。)

这是我的堆栈跟踪(名称已更改):

EasyMock.verify

以下是我的约束:

  • 我无法使用其他版本的EasyMock
  • 我不能使用其他模拟库
  • 我不能不使用模拟(显然)

我已经看到一些事情说这是一个EasyMock错误,但我认为其他人会遇到与我相同的问题并更新到更新的版本。我很确定我做错了什么。

0 个答案:

没有答案