Allow to inject mocked @ConfigProperties classes in quarkus test
See original GitHub issueDescription
It’s currently not possible to mock a config class that was annotated with the @ConfigProperties
annotation. Let’s assume, we have the following setup:
1.) config properties class
@ConfigProperties(prefix = "user-settings")
class UserConfigSnapshot {
lateinit var minAge: Int
}
2.) service that uses the config class
@ApplicationScoped
class UserService(private val configSnapshot: UserConfigSnapshot) {
}
3.) a test
@QuarkusTest
class UserConfigServiceTest {
private val configSnapshot = mockk<UserConfigSnapshot>()
@Inject
lateinit var service: UserConfigService
@BeforeEach
fun setupMocks() {
QuarkusMock.installMockForType(configSnapshot, UserConfigSnapshot::class.java)
}
}
The test fails with the message UserConfig@2b4a250d is not a normal scoped CDI bean, make sure the bean is a normal scope like @ApplicationScoped or @RequestScoped
. If I annotate the UserConfig with a @ApplicationScoped
annotation, another error message appears though.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Testing Your Application - Quarkus
Quarkus supports this by allowing you to inject CDI beans into your tests via the @Inject ... I could mock it with the...
Read more >Mocking CDI beans in Quarkus
Starting with Quarkus 1.4 , users have the ability to create and inject per-test mocks for normal scoped CDI beans using io.quarkus.test.junit.
Read more >Mapping configuration to objects - Quarkus
The Server interface is able to map configuration properties with the ... The Server can be injected as a mock into a Quarkus...
Read more >Quarkus now supports test profiles
With the release of Quarkus 1.6 Quarkus now supports test profiles, allowing you to easily test multiple different configurations inside the ...
Read more >Contexts and Dependency Injection - Quarkus
dependencies referenced by quarkus.index-dependency in application.properties ,. and Quarkus integration code. Bean classes that don't have a bean defining ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ll go ahead and close this for now as
won't fix
, but let’s keep it in mind if needed for the future@maslakov you have a couple of options:
You can use
@TestProfile
like documented here: https://quarkus.io/guides/getting-started-testing#testing_different_profilesOr you can make a proxy of
@ConfigMapping
like this: https://github.com/ivannov/mocking-config-mappers/pull/1/files. I’ll document this piece later.@ConfigProperties
was deprecated for 2.0 in favour of@ConfigMapping
.