首页 > 解决方案 > 如何为 int[] 编写 easyMock.expect

问题描述

引发错误 Targetinvocationexception。

public class A{
      public method_name(){
      int[] selections = grid.getSelectedIndices(); // Facing issue here...!
      // Problem occur above line.
      }
}

public class A_test{
    Grid grid = EasyMock.createNicemock(Grid.class);
    EasyMock.expect(grid.getSelectedIndices().andReturn(EasyMock.arEq(new int[] {1})));
    EasyMock.replay(grid);    

// I able to invoke method with the help of reflection
// method.invoke();
}

问题:我无法期待“getSelectedIndices()”。在某些更改中,它给了我 0 个匹配器和 1 个报告的错误。因为无法匹配模拟对象和原始值

标签: javaunit-testingreflectioneasymock

解决方案


类似于另一个答案。它的工作方式是通过匹配参数来期望参数,如果匹配,则返回一个值。所以代码应该是。

public class A_test {
    Grid grid = niceMock(Grid.class);
    expect(grid.getSelectedIndices()).andReturn(new int[] {1});
    replay(grid);    

    A a = new A(grid);
    a.method_name(); 
}

推荐阅读