-
Notifications
You must be signed in to change notification settings - Fork 38.5k
@MockitoBean
is not properly initialized in Kotlin test class with Spring Boot 3.4.4
#34832
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
Comments
Please provide a complete sample that reproduces the problem. The attached code can't be run as-is due to some missing classes (e.g., |
this is the stubbed @Service
@EnableConfigurationProperties(value = [TokenConfiguration::class])
class TokenGenerator @Autowired constructor(private val tokenConfiguration: TokenConfiguration) {
fun generateAccessToken(tokenPayLoad: TokenPayLoad): String {
lateinit var token : String
val jwtClaims = buildClaims(tokenConfiguration.issuer, tokenPayLoad)
val jwsObject = tokenHelper.getJWSObjectFromClaims(jwtClaims.toJSONString(), tokenConfiguration.accessTokenCategory)
try {
token = jwsObject.serialize()
} catch (e: JOSEException) {
throw Exception("Access Token : Unable to sign token", INTERNAL_SERVER_ERROR)
} catch (t: Throwable) {
throw Exception("Access Token : Unable to sign token: The JWS object must be in an unsigned state", INTERNAL_SERVER_ERROR)
}
return token
}
fun buildClaims(issuer: String, tokenPayLoad: TokenPayLoad): JSONObject {
val jsonObject = JSONObject()
val ttl: Long = tokenPayLoad.ttl
jsonObject[TIME_TO_LIVE] = ttl
// other token field assignments
return jsonObject
}
} |
The code is still not runnable — some methods and dependencies are missing. To help speed up troubleshooting, please provide a sample project either as a ZIP archive or by sharing a link to a repository. |
I had a feeling you would ask for this. I cant share most of the logic. I can try to extract it as a dummy project but I do feel as if enough is here to point in a direction that something isn't right in the meantime. If something is missing you can likely return a base object/blank where needed. |
@MockitoBean
is not properly initialized in Kotlin test class with Spring Boot 3.4.4
Hi @JayAndromeda, Thanks for reporting the issue, and congratulations on submitting your first issue for the Spring Framework! 👍 Unfortunately, the code snippets you have provided are not enough to go on. Are you saying that your test setup used to work with Spring Boot 3.3.9 when using Does your test setup work with Spring Boot 3.4.4 using Did your test setup ever work using In any case, in order to investigate this further, please provide a minimal application which demonstrates the behavior you are describing, preferably something that we can download and run such as complete Maven or Gradle project supplied as a ZIP file or a public Git repository that we can clone. Thanks |
Hi! Sorry I was travelling the past few days and I do plan on separating this issue into its own repo for you guys soon. Just have to sit down and get it done in the next couple days. Yes I did test with Currently, I have rewritten our tests to longer Autowire the bean in the test case as to avoid the issue all together but still I want to see if this is a true issue or an issue with my old testing philosophy! |
I am running into an issue when upgrading from Spring Boot 3.3.9 to 3.4.4 where my tests with Mocked beans fail to run due to an initialization error on our
lateinit
fields. My Mockito versions are managed by thespring-boot-starter-test
artifact which would be 5.11.0 -> 5.14.2.My exact Mockito error is:
I think this is caused by a Kotlin exception:
I looked at this issue #34516 which is strikingly similar to our issue but of course we do not use spy beans.
These tests work as intended on Spring Boot 3.3.9 and Mockito 5.11.0. They do not work on Spring Boot 3.4.4 and do not work on either Mockito version which leads me to believe it is a possible issue with how spring is initializing these
lateinit
fields via@Autowired
.Thank you for taking a look into and I apologize if it was answered somewhere but I tried to look in the issues list.
Other info:
I attached stubbed versions of our test class, service class that allows the autowire, and object that holds the
lateinit
field in question.The text was updated successfully, but these errors were encountered: