question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow to inject mocked @ConfigProperties classes in quarkus test

See original GitHub issue

Description

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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
geoandcommented, Feb 23, 2021

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

0reactions
radcortezcommented, Jun 29, 2021

@maslakov you have a couple of options:

You can use @TestProfile like documented here: https://quarkus.io/guides/getting-started-testing#testing_different_profiles

Or 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found