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
I prefer to use constructors to initialize my test objects because it allows me to make all the members final
so that the IDE or compiler will tell me when the constructor forgot to initialize a member and prevent another method from setting them.
IMHO, @Before
violates one of the most important Java conventions, that of relying on the constructor to completely initalize objects!
JUnit 5 also has better support for constructor injection.