【问题标题】:How to write easyMock.expect for int[]如何为 int[] 编写 easyMock.expect
【发布时间】:2019-10-31 06:29:21
【问题描述】:

抛出错误目标调用异常。

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 个报告的错误。因为无法匹配模拟对象和原始值

【问题讨论】:

    标签: java unit-testing reflection easymock


    【解决方案1】:

    andReturn 方法将返回值作为参数,但EasyMock.aryEq 返回 null(请参阅Source)。您可以只使用.andReturn(new int[] {1}) 而不是.andReturn(EasyMock.arEq(new int[] {1})))

    【讨论】:

      【解决方案2】:

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

      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(); 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多