Stop Using JUnit BeforeEach! <https://youtu.be/cs2...
# test
d
Stop Using JUnit BeforeEach!

https://youtu.be/cs2Wu9Co-2s

c
I always thought that you have to use beforeEach in junit 5 because otherwise your dependency will also be created for disabled tests, but the afterEach will not be called.
I have not used junit 5 for years but i once had to refactor a test suite to beforeEach because it was leaking resources when tests were disabledd
d
I was trying to remember what corner case it was that you had found! It does seem strange that test instances are created for disabled tests. As long as you only allocate memory the test will be gc’d in this case, but yes, I think other resources could leak.
Maybe I'll make another video!
c
d
I sometimes despair with issues like this. Disabled is clearly not fit for purpose, but the solution appears to be to document its unfitness rather than solve the problem
c
you can always just comment out the test annotation to disable a test. but this behavior was one thing that made me write my own test runner
d
I suppose so yes, leading to the question of why bother to have Disabled at all. As you know I'm a fan of writing test frameworks, but the sad fact is that integration with build tools and IDEs is really hard if you don't base them on JUnit (and even then is really hard).
c
are you now sticking to junit for better IDE support?
d
I certainly default to JUnit, only bringing out Minutest when I have a hard job.
c
cool video! now I feel semi famous, I’m mentioned in a youtube video
x
Hey Duncan, I watch your videos, and I might have missed something (in which case, sorry for the noise), but it feels like there's nothing inherently wrong with using
@BeforeEach
, except that we can do the same thing in a cleaner way using the test instance constructor/`init{}` block, right? The fixtures are then `val`instead of
lateinit var
, but appart from that there's nothing that will break your tests if you use
@BeforeEach
?