Qumber Abbas
Qumber Abbas

Reputation: 760

Mockito 3.4.0 Static Mocking Exception

I am getting the following exception when trying to mock the static method.

For SettingsUtility, static mocking is already registered in the current thread To create a new mock, the existing static mock registration must be deregistered

@Before
fun setUp() {
    mockStatic(SettingsUtility::class.java) {
        `when` { SettingsUtility.method(app) }.thenReturn { "" }}
}

Upvotes: 64

Views: 84256

Answers (7)

Rodrigo
Rodrigo

Reputation: 11

@sudha-chinnappa answer is the one. Use:

@AfterAll
public static void close() {
  mockedSettings.close();

}

To close the static mocking

Upvotes: 0

Manish Sinha
Manish Sinha

Reputation: 231

Always use MockedStatic in function context inside try() that auto-closes it rather than using it as private static class variable. Using it at a class level variable will result into these sort of error even if you close them in AfterAll or AfterEach.

Example -

try ( MockedStatic mockedYourClassStatic = Mockito.mockStatic(YourClass.class) ) { ... }

Also you can refer to this -

https://github.com/mockito/mockito/blob/4767db122f95e5302ad6a60a6efaa69f9d7e6cb1/subprojects/inline/src/test/java/org/mockitoinline/StaticMockTest.java

Upvotes: 1

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4799

Mockito Inline works differently than the Mockito hence the failure. So what needs to be done is initialise and close the mocks explicitly.

You need to initialise the mockedStatic for your class

private MockedStatic<YourClassWithStaticMethod> mockedStatic;

Then add below code in BeforeEach and AfterEach

@BeforeEach
  public void init() {
    mockedStatic = mockStatic(YourClassWithStaticMethod.class);
  }

@AfterEach
  public void cleanup() {
    mockedStatic.close();
  }

And now you can set the mocked expectations.

mockedStatic.when(YourClassWithStaticMethod::staticMethodToMock).thenReturn(yourReturnedMockedObject);

Upvotes: 13

Null Pointer
Null Pointer

Reputation: 275

I tried below and worked for me.

MockedStatic mockedStatic = mockStatic(SettingsUtility.class)
mocked.when(SettingsUtility::method).thenReturn("any thing");
...........
//do all stuf

and at last close the mock

mockedStatic.close();

Upvotes: 2

Sudha Chinnappa
Sudha Chinnappa

Reputation: 1223

The returned object's MockedStatic.close() method must be called upon completing the test or the mock will remain active on the current thread.

I am not sure if it is the same as how its done in Java. Hope this Java code snippet helps

private static MockedStatic<SettingsUtility> mockedSettings;

@BeforeAll
public static void init() {
    mockedSettings = mockStatic(SettingsUtility.class);
}

@AfterAll
public static void close() {
    mockedSettings.close();
}

Upvotes: 103

a fair player
a fair player

Reputation: 11786

Building on @Sudha 's answer. in Java you can use @BeforeClass and @AfterClass

private static MockedStatic<SettingsUtility> mockedSettings;

@BeforeClass
public static void init() {
    mockedSettings = mockStatic(mockedSettings.class);
}

@AfterClass
public static void close() {
    mockedSettings.close();
}

Upvotes: 19

Rajesh Kumar
Rajesh Kumar

Reputation: 417

Try doing this way, you will not get this error. It worked for me.

try(MockedStatic mocked = mockStatic(SettingsUtility.class)) {
        mocked.when(SettingsUtility::method).thenReturn("whatever you want");
    }

Upvotes: 33

Related Questions