如何替换对被测试类的私有方法的调用

发布于 2024-10-22 02:20:44 字数 1111 浏览 1 评论 0原文

好吧,我现在正在测试遗留代码。而且,我已经接近通过这个测试了,但是它被困在了有评论的线路上。这是代码片段

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲凉≈ 2024-10-29 02:20:44

解决了,用另一种方式:)。模拟了原始方法——仅在底层调用 Collections.max() 的方法。

    new MockUp<TaskSASCustomerReading>()
    {
        @Mock
        // This original method is invoking Collections.max(). 
        // Therefore, I just mocked this one, other methods are all original
        String findLatestSelectionDate(Collection customerTOs) {
           return "";
        }
    };

    new Expectations(){
        SASCustomerDataAssemblerBD assembleBd;
        CustomerTOs tos;         
        SelectionJobLogBD logBd;
        {
            try {
                SASCustomerDataAssemblerBD.getInstanceUsingEjbRef(); result = assembleBd;
                assembleBd.getData();  result = tos;
                SelectionJobLogBD.getInstanceUsingEjbRef();  result = logBd;                                
            }catch(Exception e){}
        }
    };

    new TaskSASCustomerReading().execute(); 

尽管如此,当我问这个问题时,我一开始就完全误解了这件事。在我原来的问题中,我实际上是在尝试调用一个方法,而不是替换它。 (PS 切勿在下班后工作。;)

Solved it, in another way :). Mocked the original method -- only the method that calls Collections.max() under the hood.

    new MockUp<TaskSASCustomerReading>()
    {
        @Mock
        // This original method is invoking Collections.max(). 
        // Therefore, I just mocked this one, other methods are all original
        String findLatestSelectionDate(Collection customerTOs) {
           return "";
        }
    };

    new Expectations(){
        SASCustomerDataAssemblerBD assembleBd;
        CustomerTOs tos;         
        SelectionJobLogBD logBd;
        {
            try {
                SASCustomerDataAssemblerBD.getInstanceUsingEjbRef(); result = assembleBd;
                assembleBd.getData();  result = tos;
                SelectionJobLogBD.getInstanceUsingEjbRef();  result = logBd;                                
            }catch(Exception e){}
        }
    };

    new TaskSASCustomerReading().execute(); 

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. ;))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文