-
Notifications
You must be signed in to change notification settings - Fork 31
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
@SpykBean results in NullPointerException when multiple bean candidates and none are primary #100
Comments
I tried to reproduce the issue with the following test case: @SpringBootTest(classes = [MyConfig::class])
class BugTest {
@SpykBean
lateinit var foo1: Foo
@Test
fun test() {
assertThat(foo1).isNotNull()
}
}
@Configuration
class MyConfig {
@Bean
fun foo1(): Foo = Foo1()
@Bean
fun foo2(): Foo = Foo2()
}
interface Foo {
fun foo(): Unit
}
open class Foo1 : Foo {
override fun foo() {
println("foo1")
}
}
open class Foo2 : Foo {
override fun foo() {
println("foo1")
}
} and indeed, I get a similar stack trace. However, the same test also fails (later, and with a better explanation) when Mockito (i.e. SpyBean) is used with the same test. And the fix is the same in both cases: you need to define the name of the bean in the SpykBean (or SpyBean) annotation:
So, I'll leave this open to make sure that I get the same error as in a Mockito test, but resolving the bean by field name won't happen because I want to do the same thing as Spring Boot. |
the way to align is not exactly the same because Spring adds null values as keys and values of its hashmaps, but that looks wrong to me. So, rather than doing that, I chose to avoid registering the spy if no bean name is found, which leads to the same downstream exception refs #100
I released 4.0.2 which now throws the same exception as Spring Boot in that kind of situation. |
When there are multiple beans defined that are the same type and none are marked
@Primary
,@SpykBean
resolution fails with the following error:This should attempt to resolve the bean by the field name when there are multiple beans with the same type and none are marked
@Primary
.Found on version 3.1.1 but it appears that it is not fixed in the latest version as of today: function with error.
The text was updated successfully, but these errors were encountered: