测试传入的值......而不仅仅是返回的值
所以我有一个看起来像这样的课程。
@Inject
AnotherClass anotherClass;
public class Foo {
public Boolean someMethod(){
Holder<Boolean> booleanHolder = new Holder<Boolean>();
//I have no control over this method, but I need it to set booleanHolder
anotherClass.anotherMethodCall(booleanHolder, Boolean.TRUE);
return booleanHolder.value;
}
}
我想做的是测试该方法。这看起来几乎是不可能的。我嘲笑了另一个班级。但我只能告诉传入什么变量。或者告诉 anotherMethodCall 应该返回什么。
我想要做的是能够在调用方法后设置 booleanHolder 的值。
有什么想法吗?
So I have a class that looks like this.
@Inject
AnotherClass anotherClass;
public class Foo {
public Boolean someMethod(){
Holder<Boolean> booleanHolder = new Holder<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Holder 类是否有一个访问器(大概是模板化的),以便您可以在调用后获取它所保存的值,然后对该值执行 assert() ?我假设您有一个“外部”方法的测试版本,该方法使用“按引用”模拟参数调用另一个方法。
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.
我的问题实际上是我使用mockito的方式。我指向了错误的值。如果有人好奇这是如何工作的。
出于显而易见的原因,这并不完全是我正在做的事情。但这是一个非常普遍的案例。
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.
This, for obvious reasons, is not exactly what I am doing. But it is a great generalized case.