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
Edit:
based on @NathanHughes suggestion I created a marker interface in the other packages :package <my other package which is not not in same module with main package...>;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
public interface Config {
}
and now in the test class:
@SpringBootTest
//@ContextConfiguration(classes = Config.class) //doesn't work
//@Import(Config.class) //doesn't work
class AuthServerApplicationTests {
@Test
void contextLoads() {
}
}