0

Possible Duplicate:
JUnit 4: Set up things in a test suite before tests are run (like a test's @BeforeClass method, just for a test suite)

I am looking for an entry point to be invoked just once before all the tests, not just in a class, but in all of the suites and classes within the same jar.

MbUnit (in .NET) provides the AssemblySetUp attribute, which designates such an entry point.

How can I do it in java using junit 4?

Community
  • 1
  • 1
mark
  • 59,016
  • 79
  • 296
  • 580
  • Like configuring the network emulation appliance, whatever. Believe, there are lots of use cases. – mark Mar 05 '12 at 12:28
  • I'm not questioning the need for it, I'm just asking for a concrete use case so we can get a feel for the problem. – skaffman Mar 05 '12 at 13:32
  • Look at [this answers](http://stackoverflow.com/questions/349842/junit-4-set-up-things-in-a-test-suite-before-tests-are-run-like-a-tests-befo) to a similar question. It gives an example of how to implement this behavior. – a.tereschenkov Mar 05 '12 at 13:08

2 Answers2

0

Looking through the documentation, I don't see any simple way to do this. There is the @BeforeClass annotation, which runs only once per class.

A better solution might be some refactoring; in your example use case, why do you have so many classes accessing the network? If you encapsulated that functionality, and mocked it out for all the other test cases, you could greatly reduce the number of tests that would need that setup.

edit: Nothing like coming back 2 years later to respond again. You could put your network setup in its own class, and have it track whether it's been called or not (something simple like private static final calledYet = false), then call that from all your test cases in a @BeforeClass method, or even have a superclass with its own @BeforeClass method that all your test cases extend.

Sam Jones
  • 1,400
  • 1
  • 13
  • 25
0

My solution for this problem(costly setup that was the same for some tests) was to have a Setup-Class with an init method:

private static boolean initialized = false;

public static void init()
{
  if (initialized)
  {
    return;
  }

  //... do all the init stuff
  initialized = true;
}

The downside is that you have to call this in every that that depends on the initialization.

You could also write your own TestRunner, which could take care of the initialization.

oers
  • 18,436
  • 13
  • 66
  • 75