I want to check that a specific constructor is called with a certain set of params and to check that the params are correct.
The class uses the arbitrary parameters of java ( ... ) like this:
public class MyClass{
public MyClass(Object o, int... params){
//DOSOMETHING
}
}
The class under test has a method that do this:
@RunWith(PowerMockRunner.class) @PrepareForTest(MyClass.class)
public class ClassUnderTest {
private int[] par;
public ClassUnderTest(int... params){
this.par = params;
}
public MyClass methodToTest(){
return new MyClass(null, this.par);
}
}
I want to check that the returned MyClass was actually called with the correct agruments.
What I did is this:
whenNew(MyClass.class).withAnyArguments().thenReturn(null);
ClassUnderTest clazz = new ClassUnderTest(0, 1, 2);
MyClass res = clazz.methodToTest();
verifyNew(MyClass.class).withArguments(eq(null), any(int[].class));
But is not working with this error:
java.lang.AssertionError: Wanted but not invoked my.package.MyClass(
null,
<any>
);
Actually, there were zero interactions with this mock.
Do you have any suggestion or Idea how to test this, I would like to avoid using getters of MyClass.....
I solved the problem just PreparingForTest the class that I was testing so I did:
@PrepareForTest(ClassUnderTest.class)
instead of
@PrepareForTest(MyClass.class)
Monday if I'll have some spare time I will post a more detailed answer
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.