Skip to content

Add JUnit 5 parameter resolver for @MockBean #13113

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

Closed
sdeleuze opened this issue May 9, 2018 · 13 comments
Closed

Add JUnit 5 parameter resolver for @MockBean #13113

sdeleuze opened this issue May 9, 2018 · 13 comments
Labels
for: external-project For an external project and not something we can fix status: declined A suggestion or change that we don't feel we should currently apply theme: testing Issues related to testing type: enhancement A general enhancement

Comments

@sdeleuze
Copy link
Contributor

sdeleuze commented May 9, 2018

Like there is a parameter resolver for @Autowired, it would be useful to have one for @MockBean, especially in Kotlin where it would enable using val instead of lateinit var.

cc @sbrannen

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 9, 2018
@sbrannen
Copy link
Member

sbrannen commented May 9, 2018

I can imagine Boot users requesting similar support for other Boot testing features such as @LocalServerPort, so the team might want to review all DI annotations and consider whether it makes sense to support them via a ParameterResolver for JUnit Jupiter.

@sbrannen
Copy link
Member

sbrannen commented May 9, 2018

Oh wait... I just realized that @LocalServerPort is simply meta-annotated with @Value("${local.server.port}").

So there's nothing special to do there: the SpringExtension already supports parameter resolution for @Value. 😉

@sbrannen
Copy link
Member

sbrannen commented May 9, 2018

@sdeleuze, the challenge with supporting @MockBean and @SpyBean via a JUnit Jupiter ParameterResolver is that org.springframework.boot.test.mock.mockito.DefinitionsParser would have to be revised to search for those annotations on parameters instead of the status quo (i.e., only on classes or fields).

The question is what Boot would ultimately want to support for those annotations.

Since the Spring TestContext Framework currently does not support loading of an ApplicationContext on a per-method basis, I imagine that Boot would want to limit support for declaring @MockBean and @SpyBean on parameters to test class constructors.

In other words, I don't think it would make much sense to support those annotations on parameters for test methods or test lifecycle methods.

@sdeleuze
Copy link
Contributor Author

sdeleuze commented May 9, 2018

Indeed my use case is only for constructor parameters.

@ExtendWith(SpringExtension::class)
@WebMvcTest
class HttpApiTests(@Autowired val mockMvc: MockMvc) {

	@MockBean
	private lateinit var userRepository: UserRepository

	@Test
	fun foo() { }
}

Versus

@ExtendWith(SpringExtension::class)
@WebMvcTest
class HttpApiTests(@Autowired val mockMvc: MockMvc,
                   @MockBean val userRepository: UserRepository) {

	@Test
	fun foo() { }
}

@philwebb
Copy link
Member

Since the Spring TestContext Framework currently does not support loading of an ApplicationContext on a per-method basis, I imagine that Boot would want to limit support for declaring @MockBean and @SpyBean on parameters to test class constructors.

Yeah, we'd need to limit this because mocks == new context.

@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 15, 2018
@philwebb philwebb added this to the Icebox milestone Jun 15, 2018
@krzyk
Copy link
Contributor

krzyk commented Jun 29, 2018

@sdeleuze Is there any plan to support per-method ApplicationContext?
Isolating all test cases sounds like a quite desirable thing.

@philwebb philwebb added the theme: testing Issues related to testing label Oct 9, 2018
@davinkevin

This comment has been minimized.

@wilkinsona
Copy link
Member

Please use the 👍 and 👎 reactions on the issue description to indicate your interest rather than spamming the issue with comments.

@xenoterracide

This comment was marked as off-topic.

@xenoterracide
Copy link
Contributor

It would seem currently that the only option is to write a second test class since I only wanted to mock this for one test method.

@wilkinsona
Copy link
Member

Given the plan to deprecate @MockBean in Spring Boot 3.4 and the introduction of @MockitoBean in Spring Framework 6.2, I don't think we should do anything here. A parameter resolver for @MockitoBean could be added to Spring Framework if there's still demand for this.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2024
@wilkinsona wilkinsona removed this from the General Backlog milestone May 31, 2024
@wilkinsona wilkinsona added status: declined A suggestion or change that we don't feel we should currently apply for: external-project For an external project and not something we can fix labels May 31, 2024
@xenoterracide
Copy link
Contributor

@wilkinsona do you guys have the ability to move tickets between spring projects? I know that GitHub added that ability a while back. Would it be possible simply to move (And reopen ) this issue over to the spring framework instead of having to have someone rewrite the whole thing?

@wilkinsona
Copy link
Member

I don't, but some of the team do as they're commiters projects.

@bclozel can please transfer this if appropriate or would the Framework team prefer a fresh issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: declined A suggestion or change that we don't feel we should currently apply theme: testing Issues related to testing type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

8 participants