Hi everyone, I was rewriting some JUnit tests to K...
# kotest
r
Hi everyone, I was rewriting some JUnit tests to Kotest and stumbled upon the
@WithMockUser
annotation. I found this old issue: https://github.com/kotest/kotest/issues/337 The solution mentioned there works but in my opinion, it's pretty ugly. Comparing:
Copy code
@WithMockUser(authorities = [EDIT_SCHEDULED_CAMPAIGNS])
        fun `should handle errors`() {
with
Copy code
it("should handle errors") {
            SecurityContextHolder.getContext().authentication =
                PreAuthenticatedAuthenticationToken(null, null, listOf(SimpleGrantedAuthority(EDIT_SCHEDULED_CAMPAIGNS)))
👀 1
Any opinion @Emil Kantis or @Alex Kuznetsov maybe?
e
Normally we would use an extension for injecting behavior. I think it would be possible to do it like this as well:
Copy code
describe("ErrorHandling") {
   extensions(SpringMockUserExtension(EDIT_SOMETHING))
 
   it("should handle errors") {
       // ...
   }
   
   // any other siblings would also have the mock user injected
}
I think you could define it as a
TestCaseExtension
💯 1
r
Thanks for the clarification. I'll create a PR when I have some time 👍
🙌 1
🙌🏼 1