I write a singleTone named DemoSingleTon
, a main class named DemoMain
and a test class named DemoTest
. when testing all tests of DemoTest individually, tests run successfully.If all tests run together, the latter two use cases will always fail.It looks like the mockStatic behind doesn't take effect.
public final class DemoSingleTon {
private static final DemoSingleTon instance = new DemoSingleTon();
private DemoSingleTon() {
}
public static DemoSingleTon getInstance() {
return instance;
}
public String test(String input) {
return input == null ? "" : input;
}
}
public class DemoMain {
private static final DemoSingleTon instance = DemoSingleTon.getInstance();
public static String testInput() {
return TestUtil.test("");
}
public String testInputUseSingleTone() {
return instance.test("input1");
}
}
public class DemoTest {
@Test
public void test() {
try (MockedStatic<DemoSingleTon> mockedStatic = Mockito.mockStatic(DemoSingleTon.class)) {
DemoSingleTon testUtil1 = Mockito.mock(DemoSingleTon.class);
mockedStatic.when(DemoSingleTon::getInstance).thenReturn(testUtil1);
Mockito.when(testUtil1.test("input1")).thenReturn("nothing");
DemoMain demoMain = new DemoMain();
assertEquals("nothing", demoMain.testInputUseSingleTone());
}
}
@Test
public void test1() {
try (MockedStatic<DemoSingleTon> mockedStatic = Mockito.mockStatic(DemoSingleTon.class)) {
DemoSingleTon testUtil1 = Mockito.mock(DemoSingleTon.class);
mockedStatic.when(DemoSingleTon::getInstance).thenReturn(testUtil1);
Mockito.when(testUtil1.test("input1")).thenReturn("everything");
DemoMain demoMain = new DemoMain();
assertEquals("everything", demoMain.testInputUseSingleTone());
}
}
@Test
public void test2() {
DemoMain demoMain = new DemoMain();
assertEquals("input1", demoMain.testInputUseSingleTone());
}
}
build.gradle following:
testImplementation group: 'org.mockito', name: 'mockito-inline', version: '4.9.0'
testImplementation('org.junit.jupiter:junit-jupiter-api:5.6.2')
testImplementation('org.junit.jupiter:junit-jupiter-engine:5.6.2')
I think each call to mockitoStatic should be independent and not interact with each other.