We have lots of selenium tests running through junit, and they all have a few steps which need to run before the actual tests. For this we have a parent TestCase class which has some @Before and @After methods in it.
Now, due to a new feature of our site I would like to parameterise part of this set up, I’d like to create a new annotation to put on some tests to indicate to the setup() method that the setup is slightly different, whilst allowing the others to use the default. So, is it possible to reflectively access the test method about to be run in an @Before method?
eg.
class SomeTest extends MyTestCase {
@Test
@Flavour(Red.class)
public void testRedFlavouredHomepage() {
testHomepage();
}
@Test
public void testBlueFlavouredHomepage() { // assuming Blue is my default flavour
testHomepage();
}
public void testHomepage() {
// test some stuff
}
}
You can do this using @Rule (with the later versions of JUnit >= 4.9). If you have a class which implements TestRule, specifically apply(), you can do extra things before your test gets run. The Description is passed to the apply method, which contains the annotations on your method:
Using
@Deprecatedas an example:This produces as output: