1

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() {
    }

}
HII
  • 3,420
  • 1
  • 14
  • 35
  • Multi modules project ? – dm_tr Jan 04 '21 at 01:20
  • @dm_tr yes as written in the note at the end of the question – HII Jan 04 '21 at 01:23
  • easiest way to do it is with the Import annotation. Otherwise you have to add stuff to component scan. – Nathan Hughes Jan 04 '21 at 01:29
  • @NathanHughes but I have many beans, can you please explain more about the other way which lets the test context scan whole packages at a time? – HII Jan 04 '21 at 01:31
  • 1
    you dont need to add every class to component scan. maybe see this answer, https://stackoverflow.com/q/52080294/217324 – Nathan Hughes Jan 04 '21 at 01:32
  • @NathanHughes so I should keep my code as it is, but instead of specifying package names in the `@ComponentScan` above `TestConfig`, I create a marker interface in each package and mention these interfaces names instead of the package names? – HII Jan 04 '21 at 01:35
  • that's one way i've seen it done. – Nathan Hughes Jan 04 '21 at 01:36
  • @NathanHughes Now I get this error `error: package does not exist` – HII Jan 04 '21 at 01:42
  • is it in the classpath? – Nathan Hughes Jan 04 '21 at 04:49
  • @NathanHughes I updated the question to show the approach you suggested but it doesn't work, and yes there is a dependency from the module that contains the main and test classes to the module that contains the configuration classes (I am using gradle) – HII Jan 04 '21 at 11:18
  • 1
    It is quite tedious to set up the multi module spring boot project, so many things may go wrong .. I also use ```@Import``` to pick up the configuration from the other module, however instead of the interface I use class (```public class Config``` in your case). Right now I checked it with interface and it does not work, with class it does. Check it out – jwpol Jan 10 '21 at 21:25
  • @jwpol you're absolutely right, turned out there were more problems than I thought, after reading this tutorial to set up the project correctly the problem was solved without need of any special annotations, `@SpringBootTest` works and is enough. (https://reflectoring.io/spring-boot-gradle-multi-module/) – HII Jan 11 '21 at 02:30

0 Answers0