So I have a class that looks like this.
@Inject
AnotherClass anotherClass;
public class Foo {
public Boolean someMethod(){
Holder<Boolean> booleanHolder = new Holder<开发者_Python百科;Boolean>();
//I have no control over this method, but I need it to set booleanHolder
anotherClass.anotherMethodCall(booleanHolder, Boolean.TRUE);
return booleanHolder.value;
}
}
What I am trying to do is test the method. Which seems darn near impossible. I have anotherClass mocked. But I can only tell what variable are passed in. Or tell what anotherMethodCall should return.
What I want to do is be able to set what booleanHolder will be after the method is called.
Any ideas?
Does the Holder class have an accessor (presumably templated) so that you can get the value it holds after the call, then do an assert() against that value? I am assuming that you have a testing version of the "outer" method that calls the other method with the "by reference" emulation parameter.
My problem was actually the way I was using mockito. I was pointing to the wrong value. If anyone is curious as to how this works.
public void test_someMethod(){
Foo myClass = new Foo();
AnotherClass mockClass = mock(AnotherClass.class);
final Boolean testBool;
when(anotherClass.anotherMethodCall((Holder)anyObject(),anyBoolean()).thenAnswer(new Answer(){
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Holder<Boolean> newHolder = (Holder<Boolean>) args[0];
newHolder.value = testBool;
}
}
Boolean retBool = myClass.someMethod();
assertTrue(retBool);
}
This, for obvious reasons, is not exactly what I am doing. But it is a great generalized case.
精彩评论