简体   繁体   English

模拟匿名内部类以更改外部本地最终变量

[英]mock anonymous inner class to change outer local final variable

I have the following code snippet where the inner anonymous class will change the outer local final variable. 我有以下代码片段,其中内部匿名类将更改外部本地最终变量。

I want to write unite test code for this code snippet, I can write a test proxy and define my callStoredProcedure within it, although I can't figure out how I can change the outer final variable as the anonymous class does there 我想为此代码段编写统一的测试代码,我可以编写一个测试代理并在其中定义我的callStoredProcedure,尽管我不知道如何像匿名类那样更改外部最终变量。

I am using Mockito, Any clue? 我正在使用Mockito,有什么线索吗?

        final List<String> result = new ArrayList<String>();

        proxy.callStoredProcedure(param1, param2,
                param3, resolveSQL(param4),
                new IProxyExtractor<GenericDTO>() {

                    @Override
                    public List<GenericDTO> extract(
                            final int resultSetindex, final ResultSet rs)
                            throws SQLException {
                        while (rs.next()) {
                            result.add(rs.getString(1));
                        }

                        return Arrays
                                .asList(new GenericDTO[] { new ABCDTO() });
                    }
                }, param5, param6);

You can do this with using a Mockito Answer (I haven't tested this code though): 您可以使用Mockito答案来做到这一点(尽管我尚未测试此代码):

ProxyClass mock = mock(ProxyClass.class);

when(mock.callStoredProcedure(any(), any(), any(), any(), any(), any(), any())
    .thenAnswer(new Answer<Void>() {
         public Void answer(InvocationOnMock invocation) {
             Object[] args = invocation.getArguments();
             IProxyExtractor<GenericDTO> proxyExtr = (IProxyExtractor<GenericDTO>) args[4];

             // Call the IProxyExtractor with a dummy ResultSet
             ResultSet rs = // mock ResultSet or extend it and populate it with custom data.
             GenericDTO[] dto1 = proxyExtr.extract(0, rs);
             GenericDTO[] dto2 = proxyExtr.extract(1, rs);


             return null;
         }
    });

See: 看到:


If you're implementing the ProxyClass for your tests, then something like this should work: 如果您要为测试实现ProxyClass,则应使用以下方法:

public class TestProxyClass extends ProxyClass {

    public void callStoredProcedure(param1, 
                                    param2, 
                                    param3, 
                                    param4, 
                                    IProxyExtractor<GenericDTO> extr, 
                                    param5,
                                    param6) {

        // do something else..

        ResultSet rs = // mock ResultSet or extend it and populate it with custom data.

        // Call the IProxyExtractor with a dummy ResultSet
        GenericDTO[] dto1 = proxyExtr.extract(0, rs);
        GenericDTO[] dto2 = proxyExtr.extract(1, rs);

        // do something else..
    }

}

Having said that, see if your code can be improved so that the IProxyExtractor implementation returns a list instead of directly modifying an inner variable. 话虽如此,看看是否可以改进您的代码,以便IProxyExtractor实现返回一个列表,而不是直接修改内部变量。 You can also use some sort of Collector (inspired from Java 8 streams) which can be passed as a method argument to IProxyExtractor#extract . 您还可以使用某种收集器(受Java 8流启发),可以将其作为方法参数传递给IProxyExtractor#extract

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM