如何使用 PowerMockito 捕获构造函数参数

Dhr*_*aya 3 junit mockito powermock powermockito

class A {
   public B getB(){
      // somehow get argType1 and argType2
      return new B(argType1, argType2);
   }
}

class B{
   public B(Type1 t1, Type2 t2){
     // something
   }
}
Run Code Online (Sandbox Code Playgroud)

我想测试A,并验证该构造函数B获取调用为预期值argType1argType2

我如何使用 PowerMockito 做到这一点?

有没有办法像这样传递argumentCaptor:

whenNew(B.class).withArguments(argType1Captor, argType2Captor).thenReturn(somemock);
Run Code Online (Sandbox Code Playgroud)

如果这样做,则argType1Captor获得两个值

Dhr*_*aya 5

我通过这样做解决了它

PowerMockito,verifyNew(B.class).withArgument(expectedArgType1, expectedArgType2)
Run Code Online (Sandbox Code Playgroud)

  • ExpectedArgType1 实际上是参数,对吗?不是ArgumentCaptor? (2认同)
  • 您确实没有捕捉到论点。你只是说他们应该是什么论据。有一个微妙但重要的区别。 (2认同)