JUnit: using constructor instead of @Before

前端 未结 8 824
旧巷少年郎
旧巷少年郎 2020-12-04 14:10

I\'m using JUnit 4. I can\'t see the difference between initializing in the constructor or using a dedicated init function annotated by @Before. Does this mean

8条回答
  •  臣服心动
    2020-12-04 14:38

    There is no difference except that the constructor is the only method ehere can initialize the @Rule objects:

    public class TestClass {
    
        @Rule
        public SomeRule rule;
    
        public TestClass() {
            // code to initialize the rule field
            conf = new RuleConf()
            rule = new SomeRule(conf)
        }
    }
    

提交回复
热议问题