So right now I'm using JUnit 4 and in the @BeforeClass
methods I setup everything needed to reset the user schema or to prepare sample data.
Now, it's not that I don't like this approach but I found it quite frustrating for the following reason:
- I'm using the Parameterized annotation to run the very same tests with different input data. Parameterized doesn't work on
@BeforeClass
because@BeforeClass
works with a static method.
This means I have to replicate tests if I want to keep the @BeforeClass
logic. I can't use @After and @Before
because those will happen after every test and it would be an overhead.
I was thinking I could refactor this Unit Tests in the sense that I'll write an abstract class that handles the test and a subclass for every group parameters I want to try so that I can have the test code written only once.
I'm hoping you can suggest a cleaner option with the following starting point: the use of @Parameterized
, the need to run the "database" method only once per parameter group.
EDIT:
this is an example of my class without the BeforeClass
RunWith(LabelledParameterized.class)
public class TestCreateCampaign extends AbstractTestSubscriberCampaign {
public TestCreateCampaign(String label, String apiKey, String userKey,
int customerId) {
super(label, apiKey, userKey, customerId);
}
@Before
public void setUp() throws Exception {
super.setUp();
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Parameters
public static Collection<Object[]> generatedData() {
return DataProvider.generatedCorrectSubscriberData();
}
@Test
public void testCreateEmailCampaignBothTriggered() {
// TEST
}
@Test
public void testCreateTextCampaignTriggered() {
// TEST
}
@Test
public void testCreateTextCampaignTest() {
// TEST
}
// Other Tests
}