Skip to content
Advertisement

JUnit: @Before only for some test methods?

I have some common set up code that I’ve factored out to a method marked with @Before. However, it is not necessary for all this code to run for every single test. Is there a way to mark it so the @Before method only runs before certain tests?

Advertisement

Answer

Just move out the tests that don’t need the setup code into a separate test class. If you have some other code common to the tests that would be helpful to keep, move that out into a helper class.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement