Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@DataJpaTest: how to stop loading unneeded @ConfigurationProperties

In a SpringBoot (2.2.1,2.2.2) application a sliced @DataJpaTest doesn't run because a bean holding @ConfigurationProperties could not be created.

The test:

@DataJpaTest
public class FooCustomerServiceTest
{
    @Autowired
    FooCustomerRepository repo;

    @Test
    void findAll()
    {
       repo.findAll();
    }
}

When I try to run the test I get a

No qualifying bean of type 'foo.appconfig.AppInfoConfig' available

foo.appconfig.AppInfoConfig

@ConfigurationProperties(prefix = "app.info")
public class AppInfoConfig
{
    private String name;

    ...
}

It is referenced by one @Component not related to the test.

The foo.Application that is used has no special annotations

@SpringBootApplication
@ComponentScan
@ConfigurationPropertiesScan
public class Application
{
    public static void main( final String[] args )
    {
        SpringApplication.run( Application.class, args );
    }
}

As sliced tests only scan for certain components (@Entity and @Repository in case of @DataJpaTest) 'AppInfoConfig' is not scanned. That's ok but why is it tried to be injected to another controller that is neither a entity nor a repository?

How can I enable the correct creation of AppInfoConfig or disable the creation at all?

NB: A @TestConfigurationhaving a @Bean method does work but is not really handy if you have a lot of those @ConfigurationProperties classes.

like image 733
Alex Avatar asked Oct 20 '25 07:10

Alex


1 Answers

Why do you have @ComponentScan on your SpringBootApplication? Doing so overrides the configuration that is applied on it, including the filter that customizes classpath scanning in slice tests.

See the documentation for more details.

like image 111
Stephane Nicoll Avatar answered Oct 22 '25 21:10

Stephane Nicoll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!