简体   繁体   中英

Multi-module spring-boot jpa application

I'm in trouble refactoring a monolithic application into a multi-module structure:

-\
 + core (jpa domain entities, services, ...)
 + command-line-utils
 + web-app

I've moved classes and configured the pom files so that everything compiles and the web app executes correctly.

I cannot understand how to move into the core module the basic junit tests I originally had: I cannot stop having errors concerning missing @Autowired repositories.

I understand that I probably should define some sort of configuration class (?) in the core module (I do not have any special configuration concerning persistence in the original moloc except datasource parameters in properties file).

You can define configuration right inside your JUnit tests. Use @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration annotation from org.springframework.test.context package.

Inside your test you can define configuration as needed with Mocks and real classes in static class, eg

    @Configuration
    public static class Config {

        @Bean
        public RestOperations restTemplate() {
            return mock(RestOperations.class);
        }

        ....
    }

I've resolved (this issue) defining a @SpringbootApplication annotated class in src/test/java referenced with @SpringApplicationConfiguration(class = MyTestApp.class ) in test classes.

In this way the usual spring-boot magic :-) works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM