I have a dataSource bean defined in spring xml as follows
<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
<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)
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!