如何模拟方法参数中的 ArrayList<Integer>?

问题描述

MyClass 带有一个静态方法

MyClass {
     public static String myStaticmethod(HttpResponse httpResponse,ArrayList<Integer> list)
    {
      //Some Business logic
      return "someString";
    }

MyWorkerClass

MyWorkerClass {
    
    public void myWorkerMethod  throws MyCustomException{
      //Some Business Logic
      MyClass.myStaticmethod(httpResponse,list);
      //Some more business logic
      }
    }

编写测试用例来测试 MyWorkerClass 的一个方法

    @RunWith(powermockrunner.class)
    @PrepareForTest({MyClass.class})
    public class MyWorkerClasstest {
        @Test(expected = MyCustomException.class)
        public void myWorkerMethodtest() throws MyCustomException{
          //prepare required
          mockStatic(MyClass.class);
          when(MyClass.myStaticmethod(any(HttpResponse.class),any()).thenThrow(MyCustomException.class); //TARGET LINE
        
          MyWorkerClass objectMyWorkerClass = new MyWorkerClass();
          objectMyWorkerClass.myWorkerMethod();
         
         }
       }

当在 TARGET LINE 使用 any() 作为 ArrayList 的第二个参数时,模拟不起作用。 如果在 TARGET LINE 处用 anyList() 替换 any() 会出现编译错误

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)