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

class B{
   public B(Type1 t1, Type2 t2){
     // something
   }
}

I want to Test A, and verify that constructor of B is getting called for expected values of argType1 and argType2.

How can i do this using PowerMockito?

Is there a way to pass argumentCaptor like this:

whenNew(B.class).withArguments(argType1Captor, argType2Captor).thenReturn(somemock);

if this do this, argType1Captor gets both the values

4

2 回答 2

5

我通过这样做解决了它

PowerMockito,verifyNew(B.class).withArgument(expectedArgType1, expectedArgType2)
于 2015-07-10T07:16:09.260 回答
4

Dhrumil Upadhyaya,您的答案(诚然是唯一提供的)并没有解决您提出的问题!我想你可能想做类似的事情:

public class MyTest {
   private ArgType argument;

   @Before
   public void setUp() throws Exception {
       MyClass c = mock(MyClass.class);

       whenNew(MyClass.class).withAnyArguments()
                             .then((Answer<MyClass>) invocationOnMock -> {
                argument = (ArgType) invocationOnMock.getArguments()[0];
                return c;
            } 
        );
    }
}

这不是一个优雅的解决方案,但它会捕获传递给构造函数的参数。您的解决方案的优势在于,如果参数是 DTO,那么您可以检查它以查看它是否包含您期望的值。

于 2018-11-02T15:14:25.220 回答