简体   繁体   中英

Does Juint reset global variables for each test?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM