Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Mockito模拟构造函数及其内部的私有函数_Java_Spring_Unit Testing_Testing_Mockito - Fatal编程技术网

Java 使用Mockito模拟构造函数及其内部的私有函数

Java 使用Mockito模拟构造函数及其内部的私有函数,java,spring,unit-testing,testing,mockito,Java,Spring,Unit Testing,Testing,Mockito,我在一个场景中,我需要对类进行单元测试,这个类涉及一些bean的形成,它需要我没有的真实数据,下面的代码是更多的参考 我要模拟的适配器类 public class TIBCOAdapter { public TIBCOAdapter(final GIAFProperties giafProperties) throws Exception { if (giafProperties != null) { this.giafProperties = g

我在一个场景中,我需要对类进行单元测试,这个类涉及一些bean的形成,它需要我没有的真实数据,下面的代码是更多的参考

我要模拟的适配器类

public class TIBCOAdapter {
    public TIBCOAdapter(final GIAFProperties giafProperties) throws Exception {
        if (giafProperties != null) {
            this.giafProperties = giafProperties;
        } else {
            LOG.info("Error: No properties found");
        }
        init();
    }

    public void init() throws IOException {
        factory = initializeQueueConnectionFactory();
        requestQueue = initializeRequestQueue();
    }

    private QueueConnectionFactory initializeQueueConnectionFactory() {

        final DurationRecord start = DurationLog.logBefore();

        final JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        bean.setJndiTemplate(new JndiTemplate(giafProperties.getProperties()));
        bean.setJndiName(GIAFPropertyUtil.getPropertyString(giafProperties, "externalJndiName"));
        try {
            bean.afterPropertiesSet();
        } catch (Exception e) {
            throw new GIAFRuntimeException(e);
        }
        final ConnectionFactory targetConnectionFactory = (ConnectionFactory) bean
                .getObject();
        LOG.info("Got target connection factory: " + targetConnectionFactory);
        final MultiCachingConnectionFactory factoryLocal = new MultiCachingConnectionFactory(
                targetConnectionFactory, giafProperties);

        DurationLog.logAfter(start);

        return factoryLocal;
    }

    private Queue initializeRequestQueue() {

        final JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        bean.setJndiTemplate(new JndiTemplate(giafProperties.getProperties()));
        bean.setJndiName(GIAFPropertyUtil.getPropertyString(giafProperties,
            "request-queue"));
        try {
            bean.afterPropertiesSet();
        } catch (Exception e) {
            throw new GIAFRuntimeException(e);
        }
        return (Queue) bean.getObject();
    }
}
创建其对象的实际类,这是我不想要的,这就是为什么我要模拟创建TIBCOAdapter

public class SomeClass {
    public String getResponse(TestClientFilter testClientFilter) throws ICAException {
        if (!filterValid(testClientFilter)) {
            return null;
        }
        try {
            Properties properties = new Properties(); // Sucess
            GIAFProperties giafProperties = new GIAFProperties(properties, null); // sucess
            addProperties(properties, testClientFilter); // sucess

            TIBCOAdapter tibcoAdapter = new TIBCOAdapter(giafProperties); // ERROR This is the line which I want to mock
            return (String) tibcoAdapter.invokeRequestResponse(testClientFilter.getMessage());

        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new ICAException(e);
        }
    }
}
这是我的测试

public class TestClientBusinessTest {

    @Mock
    private TIBCOAdapter tibco;

    @InjectMocks
    @Autowired
    private SomeClass test;

    @BeforeClass
    public void setUp() throws NamingException {
        MockitoAnnotations.initMocks(this);
    }

    private String returnStatement;

    @Test(dataProvider = "getTestClientResponseBusiness", dataProviderClass = StaticDataProvider.class)
    public void getResponse(TestClientFilter testClientFilter) throws Exception {

        when(tibco.invokeRequestResponse(Matchers.any(TestClientFilter.class))).thenReturn(new Object());

        test.getResponse(testClientFilter);
        tibco.invokeRequestResponse(testClientFilter.getMessage());
    }
}
这些代码行使TIBCOAdapters内部函数出现问题

bean.setJndiTemplate(new JndiTemplate(giafProperties.getProperties()));
bean.setJndiName(GIAFPropertyUtil.getPropertyString(giafProperties, "externalJndiName"));

SomeClass应该将TibcoAdapter或适配器工厂作为依赖项注入,以便您可以模拟它。你能重构这个类吗?不,我不能重构它。我需要在没有任何重构的情况下完成它。那样的话,你就无法真正做到。最好是使用SomeClass的“部分模拟”。这将封装真实对象,您可以告诉它调用真实方法或模拟单个方法,以便模拟getResponse。但实际上您并不是在测试getResponse的实现。请参见编辑。在末尾添加了两行,这两行准确地告诉您哪些行正在引发异常。