Skip to content

Commit 8cb5d5e

Browse files
committed
Add auto-configuration for Common Library beans and test support for @WebMvcTest
1 parent 9e55e0c commit 8cb5d5e

File tree

9 files changed

+137
-13
lines changed

9 files changed

+137
-13
lines changed

README.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,8 @@ implementation "com.company.project:springboot-microservice-common-lib:${pnCommo
9595
**Note:** This common library is not yet available in the central repository. To use it locally, clone this repository and execute `gradle publishToMavenLocal` to publish it to your local Maven repository.
9696

9797
### Bean Detection Configuration
98-
To ensure that Spring detects and registers the beans provided by this common library, add the following class to the config package of your microservice:
99-
100-
```java
101-
import org.springframework.context.annotation.ComponentScan;
102-
import org.springframework.context.annotation.Configuration;
103-
104-
@Configuration
105-
@ComponentScan(basePackages = "com.company.project.common")
106-
public class CommonLibScannerConfig {
107-
}
108-
```
109-
This will enable automatic scanning and registration of common library components.
98+
The Common Library provides Spring Boot auto-configuration, so you do not need to manually add any @ComponentScan in your microservice.
99+
When the library is on the classpath all components, services, and configurations inside com.company.project.common are automatically detected and registered.
110100

111101

112102
## Reference Implementation

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ dependencies {
8181
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
8282
implementation "net.ttddyy.observation:datasource-micrometer-spring-boot:${datasourceMicrometerVersion}"
8383
implementation "org.codehaus.janino:janino:${codehausJaninoVersion}"
84+
85+
testImplementation "org.mockito:mockito-core"
86+
testImplementation "org.springframework.boot:spring-boot-starter-test"
8487
}
8588

8689
dependencyManagement {

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
projectVersion = 0.0.1
1+
projectVersion = 0.0.2
22
springBootVersion = 3.3.3
33
springdocStarterVersion = 2.4.0
44
logstashLogbackVersion = 7.4
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.company.project.common.config;
2+
3+
import org.springframework.boot.autoconfigure.AutoConfiguration;
4+
import org.springframework.context.annotation.ComponentScan;
5+
6+
/**
7+
* Auto-configuration entry point for the Common Library.
8+
*
9+
* <p>Registers all Common Library components by performing a component scan on
10+
* {@code com.company.project.common}. This allows applications to use the
11+
* library without manually adding a {@code @ComponentScan} in their own code.
12+
*
13+
* <p>Loaded automatically when the Common Library is on the classpath and
14+
* Spring Boot auto-configuration is enabled.
15+
*/
16+
@AutoConfiguration
17+
@ComponentScan(basePackages = {"com.company.project.common"})
18+
public class CommonLibAutoConfig {
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.company.project.common.test;
2+
3+
import com.company.project.common.config.CommonLibAutoConfig;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.springframework.context.annotation.ImportSelector;
7+
import org.springframework.core.type.AnnotationMetadata;
8+
9+
/**
10+
* Import selector that automatically loads Common Library configuration classes
11+
* during {@code @WebMvcTest} tests.
12+
*
13+
* <p>This class is registered via Spring Boot’s Test Import Selector SPI
14+
* ({@code META-INF/spring/org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest.imports})
15+
* so that the following configurations are applied only in MVC slice tests:
16+
* <ul>
17+
* <li>{@code CommonLibAutoConfig} – main Common Library auto-configuration</li>
18+
* <li>{@code WebMvcTestMockConfig} – test-only mocks/stubs (e.g., Tracer)</li>
19+
* </ul>
20+
* This allows consumer applications to use {@code @WebMvcTest} without manually
21+
* importing Common Library test configuration.
22+
*/
23+
@Slf4j
24+
public class WebMvcTestImportSelector implements ImportSelector {
25+
26+
@NotNull
27+
@Override
28+
public String[] selectImports(@NotNull AnnotationMetadata metadata) {
29+
log.info("WebMvcTestImportSelector loaded");
30+
return new String[] {
31+
CommonLibAutoConfig.class.getName(),
32+
WebMvcTestMockConfig.class.getName()
33+
};
34+
}
35+
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.company.project.common.test;
2+
3+
import com.company.project.common.config.OpenTelemetryConfig;
4+
import io.micrometer.tracing.Tracer;
5+
import java.lang.reflect.Method;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
/**
11+
* Test-only auto-configuration for the Common Library.
12+
*
13+
* <p>Provides Mockito-based mock beans (e.g., {@link Tracer} and
14+
* {@link OpenTelemetryConfig}) required for {@code @WebMvcTest} and other
15+
* slice tests. This configuration activates only when Mockito is present on the
16+
* classpath, thanks to {@link ConditionalOnClass}.</p>
17+
*
18+
* <p>The mocks are created reflectively to avoid introducing a compile-time
19+
* Mockito dependency into the main library module, ensuring they are used only
20+
* in test environments.</p>
21+
*
22+
* <p>This class is typically imported automatically via the
23+
* {@code WebMvcTest.imports} SPI defined in the Common Library's
24+
* {@code META-INF/spring} resources.</p>
25+
*/
26+
@ConditionalOnClass(name = "org.mockito.Mockito")
27+
@Configuration
28+
public class WebMvcTestMockConfig {
29+
30+
@Bean
31+
public Tracer tracer() {
32+
return createMock(Tracer.class);
33+
}
34+
35+
@Bean
36+
public OpenTelemetryConfig openTelemetryConfig() {
37+
return createMock(OpenTelemetryConfig.class);
38+
}
39+
40+
@SuppressWarnings("unchecked")
41+
private <T> T createMock(Class<T> type) {
42+
try {
43+
Class<?> mockitoClass = Class.forName("org.mockito.Mockito");
44+
Method m = mockitoClass.getMethod("mock", Class.class);
45+
Object mocked = m.invoke(null, type);
46+
return (T) mocked;
47+
} catch (Exception ex) {
48+
throw new RuntimeException("Failed to create mock reflectively", ex);
49+
}
50+
}
51+
52+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2025 CommonLibrary
4+
Author: Mushfig Mammadov (mushfiqazeri@gmail.com)
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.company.project.common.config.CommonLibAutoConfig
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.company.project.common.test.WebMvcTestImportSelector

0 commit comments

Comments
 (0)