Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note about mocking @Singleton beans #23000

Merged
merged 1 commit into from
Jan 19, 2022

Conversation

glefloch
Copy link
Member

This adds a note about the convertScopes property of @InjectMock that allows mocking @Singleton beans.

@@ -944,6 +944,11 @@ public class MockGreetingServiceTest {
----
<1> Since we configured `greetingService` as a mock, the `GreetingResource` which uses the `GreetingService` bean, we get the mocked response instead of the response of the regular `GreetingService` bean

[NOTE]
By default the `@InjectMock` annotation can be used for any normal CDI scoped bean (e.g. `@ApplicationScoped`, `@RequestScoped`).
Mocking `@Singleton` beans can be done by setting the `convertScopes` property to true (such as `@InjectMock(convertScopes = true`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Mocking `@Singleton` beans can be done by setting the `convertScopes` property to true (such as `@InjectMock(convertScopes = true`).
Mocking `@Singleton` beans can be performed by setting the `convertScopes` property to true (such as `@InjectMock(convertScopes = true`).

[NOTE]
By default the `@InjectMock` annotation can be used for any normal CDI scoped bean (e.g. `@ApplicationScoped`, `@RequestScoped`).
Mocking `@Singleton` beans can be done by setting the `convertScopes` property to true (such as `@InjectMock(convertScopes = true`).
This will convert the `@Singleton` bean to an `@ApplicationScoped` bean for the test.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This will convert the `@Singleton` bean to an `@ApplicationScoped` bean for the test.
This will convert the `@Singleton` bean to an `@ApplicationScoped` bean for the test.
This is considered an advanced option and should only be performed if you fully understand the consequences of changing the scope of the bean

@glefloch
Copy link
Member Author

Thanks for the review, I removed the [Note] section and added the warning.

@gsmet gsmet merged commit e86d60f into quarkusio:main Jan 19, 2022
@quarkus-bot quarkus-bot bot added this to the 2.8 - main milestone Jan 19, 2022
@glefloch glefloch deleted the docs/mock-singleton branch January 19, 2022 10:16
@gsmet gsmet modified the milestones: 2.8 - main, 2.7.0.Final Jan 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants