I'm trying to write a specific JUnit Runner, and I did not find good tutorials / examples online.
The requirements are the following :
@BeforeClass
method that needs to be run if the test is launched alone@BeforeClass
method must only be launched once.@Before
and @After
methods.That being said, I looked at the different options available.
I started by creating Test Suites
which would represent my test categories, using the provided junit Suite Runner. (@RunWith(Suite.class)
)
You need to manually provide the Tests
in the suite, using @SuiteClasses()
which is annoying.
So I looked at the ClasspathSuite
Runner, which allows to put all classes from the classpath in every suite, and filter them after with @Category
It also provides a @BeforeSuite
annotation that is launched once when the suite is launched.
=> Everything is almost perfect.
@BeforeClass
from this test is launchedTest Suite
is launched, the correct Tests
are launched (thanks to the @Category
), the @BeforeSuite
is called only once, but the @BeforeClass
of every Test is also called, where it should not.=> All these things led me to think about implementing my own Runner. I tried extending the ClasspathSuite Runner, but it is not designed to do so. I tried extending the stock Suite Runner, but I did not succeed.
Could you help me understand how to implement these requirements in a JUnit Runner?
How about doing it without writing a runner? Instead, write a @BeforeClass
method that uses a singleton list that records each before-category method that has been run in the current test run, and that knows how to run all the before-category methods (each probably in its own class). The @BeforeClass
method determines its class's @Category
, checks the list, and
@Category
's before-category method has already run, or@Category
's method has been run.Not requiring a custom runner is better because it will work in environments that provide their own runners, such as IDEs and continuous integration servers.