Reputation: 351
I am not sure if it is possible to do, but I need to call different @Before methods depending on Tests. Is it possible, to make some resolver for it?
@Before
performBeforeOne();
@Before
performBeforeTwo();
@Test
callBeforeOneAndExecuteTestOne();
@Test
callBeforeTwoAndExecuteTestTwo();
Or should I just create several methods and call them manually from each test?
Upvotes: 1
Views: 1505
Reputation: 7894
I think the best way to achieve this (and the clearest) is to refactor your tests as such:
@Before
public void performBeforeForAll() {}
@Test
testOne() {
testOneBefore();
//.. test execution
}
@Test
testTwo() {
testTwoBefore();
//.. test execution
}
private void testOneBefore() {}
private void testTwoBefore() {}
That way, you can see exactly what each test is setting up before it runs. You will probably find that some tests share the same setup code, in which can you have a private method already there to prevent duplication.
Upvotes: 0
Reputation: 77206
No, you can only have one method with each lifecycle annotation. Create a composite method that calls the others if they're too large to combine.
Upvotes: 1