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

staticMock() and singletons #2153

Open
ThexXTURBOXx opened this issue Dec 22, 2020 · 2 comments
Open

staticMock() and singletons #2153

ThexXTURBOXx opened this issue Dec 22, 2020 · 2 comments

Comments

@ThexXTURBOXx
Copy link

ThexXTURBOXx commented Dec 22, 2020

Environment

Windows 10 64-bit, Java 14.0.2 x64, mockito-inline 3.6.28, JUnit 5.7.0

Short, Self Contained, Correct (Compilable), Example

http://femtopedia.de/SSCCE.zip

Issue

When we run the given tests, one of them will fail, which shouldn't. I am not entirely sure, what the actual issue is here, but I assume, it has to do with the use of singletons.

When we change the order of the tests or don't use mocking on the objects/classes, the tests pass without issues.

Also, if we "initialize" the singleton before all tests run, the tests also pass.

@ThexXTURBOXx ThexXTURBOXx changed the title staticMock and singletons staticMock() and singletons Dec 22, 2020
@ThexXTURBOXx
Copy link
Author

Edit: Updated the SSCCE by adding yet another workaround to fix this issue (could help investigating what's wrong here)

@ppanda2021
Copy link

I meet the same problem as you when using mockito-inline:4.9.0. My test code as folowing

public class DemoTest {
    @Test
    public void test() {
        try (MockedStatic<DemoSingleTons> mockedStatic = Mockito.mockStatic(DemoSingleTons.class)) {
            DemoSingleTons testUtil1 = Mockito.mock(DemoSingleTons.class);
            mockedStatic.when(DemoSingleTons::getInstance).thenReturn(testUtil1);
            Mockito.when(testUtil1.test(Mockito.anyString())).thenReturn("nothing");
            assertEquals("nothing", DemoSpock.testInput1());
        }
    }

    @Test
    public void test1() {
        try (MockedStatic<DemoSingleTons> mockedStatic = Mockito.mockStatic(DemoSingleTons.class)) {
            DemoSingleTons testUtil1 = Mockito.mock(DemoSingleTons.class);
            mockedStatic.when(DemoSingleTons::getInstance).thenReturn(testUtil1);
            Mockito.when(testUtil1.test(Mockito.anyString())).thenReturn("everything");
            assertEquals("everything", DemoSpock.testInput1());
        }
    }

    @Test
    public void test3() {
        assertEquals("input1", DemoSpock.testInput1());

    }
}

When I executed each use case individually, it's successful. If all tests run together, the latter two use cases will always fail
image
It looks like the mockStatic behind doesn't take effect.
Looking forward to a reply @mockitoguy @davidkarlsen

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

2 participants