Junit before class ( non static )

后端 未结 8 674
旧巷少年郎
旧巷少年郎 2020-12-02 12:12

Are there any best practices to get Junit execute a function once in a test file , and it should also not be static.

like @BeforeClass on non static fun

8条回答
  •  星月不相逢
    2020-12-02 12:39

    A simple if statement seems to work pretty well too:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:test-context.xml"})
    public class myTest {
    
        public static boolean dbInit = false;
    
        @Autowired
        DbUtils dbUtils;
    
        @Before
        public void setUp(){
    
            if(!dbInit){
    
                dbUtils.dropTables();
                dbUtils.createTables();
                dbInit = true;
    
            }
        }
    
     ...
    

提交回复
热议问题