0

Hi i'm triying to mock a DB interaction because i can't access the development database during the test runned by jenkins.

My plan is to "record" my DB interactions in local, storing the objects returned in json files and use them during the jenkins tests.

To do that i have used the CGLIBG to intercept all query methods of the JdbcTemplate i'm using to query the DB and store its results in json files whose names are a hash derived from the original JdbcTemplate call and its parameter values

This is what i'm triying to do

public static JdbcTemplate createMockJdbcTemplate(DataSource dataSource) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(JdbcTemplate.class);
    
    enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
        if(DbMode.RESTORE.equals(mode) && method.getName().startsWith("query")) {
            return restoreCall(obj, method, args);
        }else {
            Object retorno=proxy.invokeSuper(obj, args);
            if(DbMode.STORE.equals(mode) && method.getName().startsWith("query")) {
                storeCall(obj, method, args, retorno);
            }
            return retorno;
        }
        
    });        
    return (JdbcTemplate)enhancer.create(new Class[] {DataSource.class}, new Object[] {dataSource});
}

My problem is that when i'm going to deserialize the files during the tests, if the expected result object is a collection i'm only going to know that i shoud return a List, a Set, a Map... but not the type of the elements, thanks to the type erasure.

Can I include some info in the serialized json so the deserialzating ObjectMapper can build the correct objects?

Telcontar
  • 4,794
  • 7
  • 31
  • 39
  • 1
    there are a number of ways to do this in jackson, great documentation on this here https://www.baeldung.com/jackson-inheritance – jtahlborn Sep 28 '21 at 14:59
  • I had included the type information with: OBJ_MAPPER.enableDefaultTyping(DefaultTyping.OBJECT_AND_NON_CONCRETE); but running the test the results are not viable when the jdbctemplate is used with complex ResultsetExtractors wich returns Maps. Maybe i'm going to try the same aproach but with java serialization – Telcontar Sep 29 '21 at 13:19

0 Answers0