Skip to content
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

mockkStatic is not getting unmocked/cleared correctly #797

Open
3 tasks done
mcarleio opened this issue Mar 11, 2022 · 3 comments
Open
3 tasks done

mockkStatic is not getting unmocked/cleared correctly #797

mcarleio opened this issue Mar 11, 2022 · 3 comments

Comments

@mcarleio
Copy link

Prerequisites

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behavior

After unmocking, I would expect that the SecurityContextHolder is back in its normal operation mode.

Current Behavior

When executing anything on SecurityContextHolder like getContext() after unmocking, a NullPointerException is thrown, as the internal strategy field is not initialized.

Failure Information (for bugs)

Steps to Reproduce

  1. You need the dependency for the spring class SecurityContextHolder
  2. Write a Junit5 test like below
  3. Execute it and you will get the NPE

Context

  • MockK version: 1.12.3
  • OS: Windows 10
  • Kotlin version: 1.6.10
  • JDK version: 11-temurin
  • JUnit version: 5.8.2
  • Type of test: unit test

Minimal reproducible code

import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.just
import io.mockk.mockkStatic
import io.mockk.runs
import org.junit.jupiter.api.Test
import org.springframework.security.core.context.SecurityContextHolder

class MyTest {
    @Test
    fun test() {
        // Mock
        mockkStatic(SecurityContextHolder::class) // when replaced with mockkStatic(SecurityContextHolder::class.qualifiedName!!), everything works
        every { SecurityContextHolder.clearContext() } just runs  // without this line, everything works

        // Unmock
        clearAllMocks()

        // Try to get SecurityContext -> throws NPE, but should not
        SecurityContextHolder.getContext()
    }
}
@Raibaz Raibaz added the bug label Apr 5, 2022
@mauquoic
Copy link

mauquoic commented Apr 5, 2022

Any update on this? I am having the same issue, and it breaks all other SpringBootTests that are run afterwards.

A workaround at the moment is to define the maven-surefire-plugin like so:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<configuration>
		<reuseForks>false</reuseForks>
		<forkCount>1</forkCount>
	</configuration>
</plugin>

Unfortunately, this makes the build a lot slower.

@mcarleio
Copy link
Author

A workaround at the moment is to define the maven-surefire-plugin

You can also do it like I comment in the example above:
mockkStatic(SecurityContextHolder::class.qualifiedName!!)

This is working flawlessly as a workaround in my scenario.

@stale
Copy link

stale bot commented Jul 10, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as stale just ask to put an important label.

@stale stale bot added the stale label Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants