I was trying to find a way to properly implement set up and tear down methods of a test class (for use with JUnit 4).
After trying lots of things and after some reseach I came across this.
Difference between setUp() and setUpBeforeClass()
So... if that's true... I find this design very inflexible.
I mean... In my case I want to run a @BeforeClass method once for the whole test class i.e. once before all test case methods, but after the initialization of my test class instance. And I need this method to be an instance method.
Seems I cannot do it. It is asking me to define the @BeforeClass method as static.
And if I try to use a @Before instance method, I can see that it is called many times... before each test case method. But I need it called just once.
Is there any decent way to do what I want? I am amazed...
Here is the interesting part: for @Before OK... I can do some workaround, I can define some boolean flag and just detect that the current call is the very first @Before method call. And only then do something useful/actual it in.
But for @After I want to detect the very last call of the @After method (so that I actually do something in it only if that's the very last call). But I cannot detect this last call with any flag.
I wonder how the designers of this JUnit thing didn't think of all that.
Or... am I missing something?