I have solved a topCoder problem for which all the tests pass when I run them on their own. Nonetheless when I run the whole test class some of them fail. Could you, please,
I had same issue. I needed to mock a logger, which was a static field. So eventually class loader creates only a single instance of the static field during the first invocation of a class under the test and disregards all further mocking and stubbing. When run separately, test was green, because the logger was initialized and loaded as expected, but when run all together, with other test methods, it got initialized as a concrete object, not a mock. Workaround:
@BeforeClass
method to ensure that the right instance of static field will be created in the first place: @BeforeClass
public static void setupBeforeClass() {
PowerMockito.mockStatic(LoggerFactory.class);
loggerMock = mock(Logger.class);
when(LoggerFactory.getLogger(any(Class.class))).thenReturn(loggerMock);
}
@Before
or @After
method: @Before
public void setup() {
// Reset interactions on the mocked logger
Mockito.reset(loggerMock);
}
Note, that in my example I used PowerMock so you need a corresponding runner @RunWith(PowerMockRunner.class)
and @PrepareForTest({LoggerFactory.class, MyClass.class)}
statements.