-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a ConversionService bean if missing #87
Comments
We're aware of the difficulties in manually configuring the I'm happy to take a look at what |
I played around with my tests to get a easily reusable context configuration for tests that need ConversionServiceAdapter, but do not have Spring Web autoconfigured. How about something like this: Annotation to be placed on test classes: @ComponentScan
@ImportAutoConfiguration(classes = MapStructAutoConfiguration.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MapStructTest {
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] basePackages();
} AutoConfiguration: @AutoConfiguration
public class MapStructAutoConfiguration {
@Bean
@ConditionalOnMissingBean(ConversionService.class)
ConversionService fallbackConversionService(List<Converter<?,?>> converters) {
var conversionService = new DefaultConversionService();
converters.forEach(conversionService::addConverter);
return conversionService;
}
} Then test can look like: @DataJpaTest
//this also works: @SpringBootTest(classes = MyTestConfig.class)
@MapStructTest(basePackages = "com.example.mappers")
class Test {
@Autowired
private ConversionServiceAdapter adapter;
} This disadvantage of this would be that one needs to provide packages(s) of Didn't spend too much time on the example, possibly some rough edges/useless annotations somewhere, but at least I have now a setup that can be easily reused between tests. EDIT: Could also be worthwhile to investigate the option of enabling this autoconfiguration without explicitly adding the annotation. EDIT2: Can use regular EDIT3: Come to think of it, having to define the packages for the annotation might not be such a bad thing. If we add |
This looks nice. We can consider a separate module for testing purposes and provide this kind of thing. I'd be even more on board if we could indeed avoid using things like |
I think it can work with plain Spring. At least it didn't need the |
I've already started a branch. 😃 Could you do me a favor and play around with it in order to see whether this is what you're looking for? I tried doing it all with Spring core mechanisms for now which means e.g. there's no |
There's a problem with this when Possibly some changes needed in how Perhaps instead of: public ConversionServiceAdapter(
@Qualifier("valueFromConversionServiceBeanName") @Lazy final ConversionService conversionService) {
this.conversionService = conversionService;
} You do: public ConversionServiceAdapter(@Lazy Map<String, ConversionService> conversionServices) {
this.conversionService = // select bean from Map
} Where selection logic could be something like:
|
|
Problem:
Setting up a valid Spring context is quite difficult in tests. Most often the ConversionService provided via
WebMvcAutoConfiguration
is being used. When it is not present, for example when testing service & persistence layer interaction, can get quite ugly to get all plumbing done. Something we've had to do:Without this, instantiating Spring context fails in
ConversionServiceAdapter
due to missingConversionService
bean.Duplicating this to several test cases gets old quite quick. It is hard to make it portable/re-usable due to how different test context configuration options work: for example, setup with @DataJpaTest looks quite different.
Suggestion:
If detecting that
ConversionService
bean is missing ANDConversionServiceAdapter
base package gets component scanned, should provide a fallback bean with available Converters (at least the ones detected as MapStruct Mappers).Can be opt-in via a property in
SpringMapperConfig
or a separate annotation to be placed on test classes.OR
Provide an example reference documentation on how to conveniently set up a valid Spring context for the
ConversionServiceAdapter
when Spring web/reactive-web is not present.The text was updated successfully, but these errors were encountered: