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
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.
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
. 馃槈
@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.
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() { }
}
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.
@sdeleuze Is there any plan to support per-method ApplicationContext?
Isolating all test cases sounds like a quite desirable thing.
I do a small up
on this one, due to the date of the last message.
Please use the 馃憤 and 馃憥 reactions on the issue description to indicate your interest rather than spamming the issue with comments.
Most helpful comment
Indeed my use case is only for constructor parameters.
Versus