dimanche 3 janvier 2021

How to scan components in other packages in spring boot tests?

I tried the solutions in this very similar question but had no luck.

Configuring base package for component scan in Spring boot test

I have beans in other packages than that of the main app class, but the spring boot test can't load them.

Some of these beans are jpa interfaces or classes that use these interfaces.

main app class:

@SpringBootApplication(scanBasePackages = {packages names here...})
@EnableJpaRepositories(basePackages = packages names here...)
@EntityScan(packages names here...)
public class MyApp {

    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

Test class:

@SpringBootTest
@ContextConfiguration(classes = MyApp.TestConfig.class)
class MyAppTests {

    @Test
    void contextLoads() {
    }





    @ComponentScan(basePackages = packages names here...)
    @EnableJpaRepositories(basePackages = packages names here...)
    @EntityScan(packages names here...)
    public static class TestConfig{
    }

}

The error:

Parameter 0 of constructor in ..."some package other than main one"... required a bean of type ...

Note:

  • the packages are also in different modules
  • the main app runs normally and the beans from different packages from different modules are loaded normally

Aucun commentaire:

Enregistrer un commentaire