How to organize Spring Boot tests in a multi-module Maven dependency

Viewed 41

I'm new to Spring Boot and I have the following multi-module project and I'm wondering what the best practice is to organizing SpringBootTests in a package which doesn't have a main class. This is the structure of my project:

.
|-- README.md
|-- api
|-- core
|-- pom.xml

I have api module which has a main Application class which is a REST API service and I have a core package which includes data layer, business logic layer, metrics/tracing beans. I want to write SpringBootTests which would test specific beans, for example TracingService which is a Spring bean which generates traces. It doesn't depend on any other of my Spring beans. However in order to make a SpringBootTest for it I have to configure my environment such that I have a real data source because my data layer beans also reside in the same package (add a real Postgres instance, add data source configuration class) which forces me into making this into an integration test. This is my test code:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class TracingServiceIntegrationTest {
  //... testing methods
}

and this is the main configuration class of core package:

package com.myproject.core;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@SpringBootConfiguration
@EnableAutoConfiguration
class CoreConfig {}

and this is the structure if my core module:

├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   ├── com
│   │   │   │   └── my
│   │   │   │       └── project
│   │   │   │           └── core
│   │   │   │               ├── api
│   │   │   │               ├── model
│   │   │   │               ├── tracing
│   └── test
│       ├── java
│       │   └── com
│       │       └── my
│       │           └── project
│       │               └── core
│       │                   ├── CoreConfig.java
│       │                   └── tracing

I'm wondering what is the best practice to have SpringBootTest load just specific TracingService bean without all the data layer beans which adds a lot of complexity.

0 Answers
Related