1

好吧,我现在正在测试遗留代码。而且,我在某个地方附近可以通过这个测试,但它卡在有评论的地方。这是片段

    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都是模拟的。

4

1 回答 1

2

以另一种方式解决了它:)。模拟了原始方法——只有在后台调用的方法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 千万不要在下班后工作。;)

于 2011-03-16T15:53:54.627 回答