Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom converters with @DataMongoTest?

I have a test instantiating some entities, saving them to MongoDB and loading them again to make sure the mapping works corretly. I'd like to use the @DataMongoTest annotation on the test class to make sure an embedded MongoDB instance is dynamically created.

This worked just fine until I had to introduce custom converters (org.springframework.core.convert.converter.Converter) for some classes. These are set up like this:

@ReadingConverter
public class MyClassReadConverter implements Converter<Document, MyClass> {
...

@WritingConverter
public class MyClassWriteConverter implements Converter<MyClass, Document> {
...

@Configuration
public class SpringMongoSetup extends AbstractMongoConfiguration {
    @Override
    public Mongo mongo() throws Exception {
        //I don't want that in the test..
        return new MongoClient("localhost"); 
    }

    @Override
    public CustomConversions customConversions() {
        // ..but I need this
        List<Converter<?,?>> converters = new ArrayList<>();
        converters.add(new MyClassWriteConverter());
        converters.add(new MyClassReadConverter());
        return new CustomConversions(converters);
    }
...

For normal (non-test) execution this works just fine. The test also works if I use the @SpringBootTest annotation which makes the test use my configuration. Unfortunately, this configuration also defines the host/port for MongoDB, but I'd like to use the host/port of the embedded MongoDB started by @DataMongoTest.

Can I somehow configure it so that either @DataMongoTest uses the custom converters with the embedded MongoDB, or that I can get the embedded host/port while instantiating my configuration class?

like image 604
Jiří Kraml Avatar asked Feb 07 '17 16:02

Jiří Kraml


1 Answers

To use CustomConverters with @DataMongoTest you need to expose those converters as a Spring bean, e.g.:

@Configuration 
public class CustomConversionsConfiguration {

    @Bean
    public CustomConversions customConversions() {
        List<Converter<?,?>> converters = new ArrayList<>();
        converters.add(new MyClassWriteConverter());
        converters.add(new MyClassReadConverter());
        return new CustomConversions(converters);
    }

}

...and use the configuration in Mongo test classes:

@RunWith(SpringRunner.class)
@DataMongoTest
@Import(CustomConversionsConfiguration.class)
public class MyMongoTest { ... }
like image 177
jrd Avatar answered Oct 30 '22 06:10

jrd