Junit before class ( non static )

后端 未结 8 594
旧巷少年郎
旧巷少年郎 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:30

    The article discuss 2 very nice solutions for this problem:

    1. "clean" junit with custom Runner (using interface but you could extend it with a custom annotation e.g. @BeforeInstance)
    2. Spring execution listeners as mentioned by Espen before.
    0 讨论(0)
  • 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;
    
            }
        }
    
     ...
    
    0 讨论(0)
提交回复
热议问题