Search code examples
javaunit-testinggroovymockingspock

Groovy Spock test for Java class - how to mock a Clob


I'm using Groovy's Spock framework to write a unit test for a Java class that gets a Clob from a database, retrieves an InputStream from that Clob (with java.sql.Clob.getAsciiStream() method) and then passes that InputStream to an ObjectMapper.readValue() method (from Jackson Databind library). Any ideas on how to mock that behavior? I've seen a java library that has a MockClob class, but I want to do this using Spock. The code is something like this:

InputStream inputStream = database.getClob().getAsciiStream();
com.fasterxml.jackson.databind.ObjectMapper objectMapper = new ObjectMapper();
objectMapper.readValue(inputStream);

Solution

  • I found a workaround by creating an instance of a database-specific Clob implementation. The class is DB2ConnectionlessClob for ibm-db2 database.