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?
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 { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With