如何替换对被测试类的私有方法的调用
好吧,我现在正在测试遗留代码。而且,我已经接近通过这个测试了,但是它被困在了有评论的线路上。这是代码片段
new NonStrictExpectations(){
SASCustomerDataAssemblerBD assembleBd;
CustomerTOs tos;
CustomerSASTO to;
Another rowTo;
SelectionJobLogBD logBd;
{
SASCustomerDataAssemblerBD.getInstanceUsingEjbRef(); result = assembleBd;
assembleBd.getData(); result = tos;
..
..
//This line is not being invoked.
//Instead the actual line of code is working. Which is,
//Collections.max(someCollection, someComparator);
//Hence I am stuck because getting null in "to"
invoke(Collections.class, "max", new ArrayList(), new MaxDateComparator()); result = to;
to.getSasDataRow(); result = rowTo;
SelectionJobLogBD.getInstanceUsingEjbRef(); result = logBd;
..
}
};
new TaskSASCustomerReading().execute();
,而 result
的所有值都是模拟的。
Well, I am right now testing legacy code. And, I am somewhere near to pass this test, but its stuck at the line having comments on it. Here is the snippet
new NonStrictExpectations(){
SASCustomerDataAssemblerBD assembleBd;
CustomerTOs tos;
CustomerSASTO to;
Another rowTo;
SelectionJobLogBD logBd;
{
SASCustomerDataAssemblerBD.getInstanceUsingEjbRef(); result = assembleBd;
assembleBd.getData(); result = tos;
..
..
//This line is not being invoked.
//Instead the actual line of code is working. Which is,
//Collections.max(someCollection, someComparator);
//Hence I am stuck because getting null in "to"
invoke(Collections.class, "max", new ArrayList(), new MaxDateComparator()); result = to;
to.getSasDataRow(); result = rowTo;
SelectionJobLogBD.getInstanceUsingEjbRef(); result = logBd;
..
}
};
new TaskSASCustomerReading().execute();
Whereas, all the values of result
are mocked up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了,用另一种方式:)。模拟了原始方法——仅在底层调用
Collections.max()
的方法。尽管如此,当我问这个问题时,我一开始就完全误解了这件事。在我原来的问题中,我实际上是在尝试调用一个方法,而不是替换它。 (PS 切勿在下班后工作。;))
Solved it, in another way :). Mocked the original method -- only the method that calls
Collections.max()
under the hood.None the less, I was totally misunderstood the thing in the first place, when I asked the question. In my original question, I am in fact trying to invoke a method, instead of replacing it. (P.S. Never work after hours. ;))