Tests pass when run individually but not when the whole test class run

后端 未结 5 1334
感动是毒
感动是毒 2021-01-02 04:38

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,

5条回答
  •  無奈伤痛
    2021-01-02 04:45

    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:

    • create @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);
       }
    
    • Interactions on the mock are getting accumulated from different test executions. Therefore, to be sure that you get a clean instance of the mock on each test method execution, reset the mock whether in @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.

提交回复
热议问题