Best Practice: Initialize JUnit class fields in setUp() or at declaration?

前端 未结 9 1421
走了就别回头了
走了就别回头了 2020-12-07 09:20

Should I initialize class fields at declaration like this?

public class SomeTest extends TestCase
{
    private final List list = new ArrayList();

    publi         


        
9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 10:08

    In your case (creating a list) there is no difference in practice. But generally it is better to use setUp(), because that will help Junit to report Exceptions correctly. If an exception occurs in constructor/initializer of a Test, that is a test failure. However, if an exception occurs during setup, it is natural to think of it as some issue in setting up the test, and junit reports it appropriately.

提交回复
热议问题