-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Value properties with Duration type cannot be converted in unit test #34661
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Conversion of a Please note that Spring Boot 2.5.x is no longer supported. You should upgrade to 2.7.x or later as soon as possible. |
So do you say Spring won't be supporting this in plain unit tests? I see we already have |
There is an open Spring Framework issue to add support, but currently you need to hook in Spring Boot's If you don't want to do that using |
@philwebb thanks for your tip about @ExtendWith(SpringExtension.class)
@ContextConfiguration(initializers = DurationPropertyTest.ConversionInitializer.class)
@TestPropertySource(properties = {"app.timeout=10s"})
class DurationPropertyTest {
static class ConversionInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.getBeanFactory().setConversionService(new ApplicationConversionService());
}
}
@Value("${app.timeout}")
private Duration timeout;
@Test
void testDurationConversion() {
assertEquals(Duration.ofSeconds(10L), timeout);
}
} And one more way to solve the problem is just to load @ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = DurationPropertyTest.ConversionInitializer.class)
@TestPropertySource(properties = {"app.timeout=10s"})
class DurationPropertyTest {
static class ConversionInitializer {
@Bean
public ConversionService conversionService() {
return new ApplicationConversionService();
}
}
@Value("${app.timeout}")
private Duration timeout;
@Test
void testDurationConversion() {
assertEquals(Duration.ofSeconds(10L), timeout);
}
} |
Uh oh!
There was an error while loading. Please reload this page.
Spring Boot version: 2.5.14
We have value properties like this:
It is working in normal application run, but it's not working in unit test:
This is the structure of my unit test
The text was updated successfully, but these errors were encountered: