0

In most of my unit-tests on a component with JUnit, I encounter the same issue: I want to run a first test on the component with no setup to check that it initializes correctly, then I want all my other tests to be done on the component setup in a given way.

I usually end-up with a method that I call at the beginning of all my tests but one, which is ugly and error prone. I could also create two different classes, with one containing only one test, but I don't think that is the most convenient solution.

I tried searching around and found this answer: Exclude individual test from 'before' method in JUnit, but as far as I could find, the base.evaluate() call will always contain the @Before statement.

Is there a better way to do this first unSetup test?

Community
  • 1
  • 1
Fred Tingaud
  • 740
  • 1
  • 6
  • 11
  • Did you try the solution? Did you try the solution with Enclosed? I don't think there are other options. – Tom Jonckheere Aug 27 '14 at 14:24
  • I currently use the Enclosed solution, but I like the test code to be the simpler and more readable as possible, and even though it is not terrible, I would like to move the boilerplate out, like a clean annotation could do. The linked solution I hadn't understood before Alexander Zak's answer. – Fred Tingaud Aug 27 '14 at 14:37
  • Why not make the test that's checking initialization act on a different object reference than the other tests, and allow an @Before to construct the object for the other tests? As long as they are constructed the same way, I don't see any problems with this approach. – Mark W Aug 27 '14 at 14:51
  • That is indeed the most straight forward solution. Thanks! – Fred Tingaud Aug 27 '14 at 15:12

1 Answers1

0

What the answer you linked to meant is that you do not use a @Before annotation at all. Instead you put whatever you wanted to do in the @Before method instead of the

// run the before method here

comment.

alexander zak
  • 929
  • 6
  • 13