简体   繁体   English

测试 Camel 路由时使用模拟数据源 bean

[英]Using mocked dataSource bean when testing Camel route

I have a dataSource bean defined in spring xml as follows我在 spring xml 中定义了一个 dataSource bean,如下所示

<bean id="apacheBasicDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" >
    <property name="url" value="jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = myservice)))" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />
    ...
</bean>

Now I want to test a rest route which do his job by calling stored procedure within a groovy script现在我想测试一个休息路线,它通过在 groovy 脚本中调用存储过程来完成他的工作

<get uri="/utils/foo" produces="text/xml">
    <route id="utils.foo">
        <setBody>
            <groovy>
                import groovy.sql.Sql
                def sql = Sql.newInstance(camelContext.getRegistry().lookupByName('apacheBasicDataSource'))
                res = request.getHeader('invalues').every { invalue ->
                    sql.call("{? = call my_pkg.mapToSomeValue(?)}", [Sql.VARCHAR, invalue]) {
                        outValue -> do some stuff..
                    }
                }
                sql.close()
                new StringBuilder().append(
                    some stuff..
                )
            </groovy>
        </setBody>
    </route>
</get>

I have my test class as follows, so now it works我的测试类如下,所以现在它可以工作了

@SpringBootTest
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = {"classpath:camel.xml"})
public class EdiapiApplicationNewTest {

    @Autowired
    private CamelContext context;

    @MockBean private DataSource dataSource;
    @Mock private CallableStatement callableStatement;
    @Mock private Connection connection;
    @Mock private ResultSet resultSet;

    @Test
    public void testSimple() throws Exception {

        when(callableStatement.getResultSet()).thenReturn(resultSet);
        when(dataSource.getConnection()).thenReturn(connection);
        when(connection.prepareCall(anyString())).thenReturn(callableStatement);

        context.getRegistry().bind("apacheBasicDataSource", dataSource);

        TestRestTemplate restTemplate = new TestRestTemplate();
        ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/utils/foo?invalues=foo,bar", String.class);
        Assert.assertEquals("<value>false</value>", response.getBody());
    }
}

But I want to be able to populate mocked ResultSet with some custom value to test against (at the moment it returns null)但我希望能够用一些自定义值填充模拟 ResultSet 以进行测试(此时它返回 null)
I have tried something like我试过类似的东西

when(resultSet.next()).thenReturn(true).thenReturn(false);
when(resultSet.getString(anyInt())).thenReturn("some value");

With no success.没有成功。 Could someone give me a hint about it?有人可以给我一个提示吗? Thanks!谢谢!

Finally I found the roots of the issue as well as a solution.最后我找到了问题的根源以及解决方案。
First, void call method should be mocked with doAnswer and not with thenReturn (obviously).首先,应该使用doAnswer而不是thenReturn void call方法(显然)。
Second, I dealt with stored function, so there was no getResultSet method invoking on callableStatement , instead the getObject method was invoked, so this is the one should be mocked as其次,我处理了存储函数,所以没有调用callableStatement getResultSet方法,而是调用了getObject方法,所以这应该被嘲笑为

when(callableStatement.getObject(1)).thenAnswer(inv -> "some custom value");

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

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