测试骆驼路线时使用模拟数据源bean

时间:2020-09-22 14:09:16

标签: spring-boot unit-testing groovy mocking apache-camel

我在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>

现在我想测试一条休息路线,该路线可以通过在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>

我的测试课如下,所以现在可以工作了

@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());
    }
}

但是我希望能够用一些自定义值填充模拟的ResultSet进行测试(目前返回空值)
我已经尝试过类似

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

没有成功。有人可以给我一个提示吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

最后,我找到了问题的根源以及解决方案。
首先,无效的call方法应该用doAnswer模拟,而不是thenReturn(显然)。
其次,我处理了存储函数,因此没有getResultSet上调用callableStatement的方法,而是调用了getObject方法,因此应将此方法模拟为

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