Home > Mobile >  Does Juint reset global variables for each test?
Does Juint reset global variables for each test?

Time:08-17

I have an integration test class like this (but more complex)

@ExtendWith(SpringExtension::class)
@WebMvcTest(Controller::class)
class ScheduleControllerTest(@Autowired val mockMvc: MockMvc) {
   val obj = object {
       var b = 2
   }

   @Test
   fun a(){
       obj.b = 1
       assertEquals(obj.b, 1)
   }

   @Test
   fun b(){
       assertEquals(obj.b, 2)
       
   }
}

and all tests in this class pass as if after each test obj is reset to its initial value. No @Before functions are used here, so what is happening?

CodePudding user response:

By default JUnit creates a new test class instance for each test run (fields are initialized anew each time). You can modify this behavior by using the @TestInstance annotation.

  • Related