I want to interpolate validation messages based on JSR 303 specification into single file instead of ValidationMessages.properties. Just for simplicity. I have read that it is possible with setting of config class which extends from WebMvcConfigurerAdapter:
@Bean(name = "messageSource")
public MessageSource messageSource()
{
ReloadableResourceBundleMessageSource bean = new ReloadableResourceBundleMessageSource();
bean.setBasename("classpath:messages");
bean.setDefaultEncoding("UTF-8");
return bean;
}
@Bean(name = "validator")
public LocalValidatorFactoryBean validator()
{
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource());
return bean;
}
@Override
public Validator getValidator()
{
return validator();
}
where the file text.properties should be used for validation messages.
MAX_SIZE_MESSAGE = You must enter max 20 characters
Then I have entity where is used validation message e.g.
@Size(max = 20, message = "{MAX_SIZE_MESSAGE}")
However the message is not visible by spring boot after running of tests.
What is wrong? Must be the config file available only in one place or it must be a part of test package?
Update 30.1 It seems that the message bundle is not recognized after debugging. I have test profile where is located messages.properties file but not used.