Fail to instantiate Java Record annotated with @ConfigurationProperties

152 views Asked by At

I was trying to use Java Record as a Configuration Property whose fields like url,username and password comes from external properties file. For this, I have following code:

@EnableConfigurationProperties(DbConfig.class)
@ConfigurationProperties(prefix = "") 
public record DbConfig(String url, String username, String password) {
}

Spring boot app using this DbConfig works perfectly fine like DbConfig class's fields gets initialized correctly from the properties file. However, It fails while writing the Unit test.

Below is my test code:

@TestPropertySource(locations = "classpath:application-test.properties")
@SpringJUnitConfig(DbConfig.class)
class DbConfigTest {

    @Autowired
    DbConfig dbConfig;

    @Test
    void shouldReturnExecutorCorrectly() {
        assertEquals("localhost", this.dbConfig.url());
        assertEquals("Hello", this.dbConfig.username());
        assertEquals("Password", this.dbConfig.password());
    }
}

Test fails with error:

No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate.

Same test runs if I replace existing Record with a Class having a getter and setter. So, seems like during test, Spring is not able map dependency of DbConfig class from application-test.properties.

I have application-test-properties

url=localhost
username=Hello
password=Password

0

There are 0 answers