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

Static method mocks incompatible with MockitoExtension (NotAMockException) #1967

Closed
olada opened this issue Jul 12, 2020 · 2 comments
Closed

Comments

@olada
Copy link

olada commented Jul 12, 2020

Hello,

I am experimenting with the newly released functionality of mocking static methods.
It appears that static mocks are not compatible with the MockitoExtension designed for JUnit 5.
Following error message is produced when using the extension:

org.mockito.exceptions.misusing.NotAMockException: Argument passed to Mockito.mockingDetails() should be a mock, but is an instance of class java.lang.Class!

Full Stacktrace: static-mock-mockito-extension-stacktrace.txt

I have created following minimal example which is composed of a Utils class (containing the static method) and a UtilsUser class (calls the static Utils method). UtilsUserTest tests the functionality of the UtilsUser class.

public class Utils {
    public static long millis() {
        return System.currentTimeMillis();
    }
}
public class UtilsUser {
    public long use() {
        return Utils.millis();
    }
}
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(MockitoExtension.class)
class UtilsUserTest {
    @InjectMocks
    private UtilsUser utilsUser;

    @Test
    void use() {
        try (MockedStatic<Utils> mockedUtils = Mockito.mockStatic(Utils.class)) {
            mockedUtils.when(Utils::millis).thenReturn(17L);
            assertEquals(17L, utilsUser.use());
        }
    }
}

When converting this test to a test which does not use the extension (removing @ExtendWith, removing @InjectMocks, adding = new UtilsUser() after the field declaration), the test passes.

Can you tell me if there is something special I have to accomodate for when using the static mocks in JUnit Jupiter?
Or is this indeed a bug (which you can hopefully reproduce)?

I am using mockito-inline and mockito-junit-jupiter in version 3.4.0.

Thanks in advance and thanks for the new static mocks functionality!
David

@raphw
Copy link
Member

raphw commented Jul 12, 2020

Thanks for reporting. We already found the error, it slipped us. I think we can have a release ready fairly soon.

@raphw raphw closed this as completed in be220bb Jul 16, 2020
truman-show added a commit to truman-show/caregiver-salrong that referenced this issue Jun 12, 2021
- 3.4.0 -> 3.11.1 로 버전을 올렸습니다.
- 3.4.0 은 static method을 사용할 때  MockitoExcension 과 호환되지 않는 이슈가 있습니다.
- 발생 예외 : org.mockito.exceptions.misusing.NotAMockException: Argument passed to Mockito.mockingDetails() should be a mock, but is an instance of class java.lang.Class!
- 이슈 : mockito/mockito#1967
truman-show added a commit to TuesdayGangnam/caregiver-salon that referenced this issue Jun 13, 2021
- 3.4.0 -> 3.11.1 로 버전을 올렸습니다.
- 3.4.0 은 static method을 사용할 때  MockitoExcension 과 호환되지 않는 이슈가 있습니다.
- 발생 예외 : org.mockito.exceptions.misusing.NotAMockException: Argument passed to Mockito.mockingDetails() should be a mock, but is an instance of class java.lang.Class!
- 이슈 : mockito/mockito#1967
ksankeerth added a commit to ksankeerth/shardingsphere that referenced this issue Jan 30, 2022
In Mocktio 3.4.0, static mocks causes NotAMockException. They are fixed in 3.4.2.
[1] : mockito/mockito#1968
[2]: mockito/mockito#1967
terrymanu pushed a commit to apache/shardingsphere that referenced this issue Jan 30, 2022
* Fixed test cases of MySQLDataSourcePreparer (Git issue : 14246)

* Changed the mockito version to 3.4.2 (Git issue : 14246)
In Mocktio 3.4.0, static mocks causes NotAMockException. They are fixed in 3.4.2.
[1] : mockito/mockito#1968
[2]: mockito/mockito#1967

* Fixed test MySQLDataSourcePreparerTest (Git issue : 14246)
@kishan1301
Copy link

I used @ExtendWith(SpringExtension.class) instead of MockitoExtension.class and it worked.
Not sure if you're intended to test it with MockitoExtension itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants