I have several test cases, even if the logic is different, the output should be the same on all of them. So I thought about how to generalize them and put the Assert method only once.
Is there a way to do this better than this:
static public class Tests() {
private static String expected = null;
private String actual = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
expected = new String("My Desired Output");
}
@Before
public void setUp() {
actual = new String();
}
@Test
public void test1() throws Exception {
actual = ...
}
@Test
public void test2() throws Exception {
actual = ...
}
@After
public void tearDown() throws Exception {
assertThat(actual, is(equalTo(expected)));
}
@AfterClass
public static void tearDownAfterClass() {
}
}
Launch method:
@Test
public void runTests() {
Result result = JUnitCore.runClasses(Tests.class);
assertThat(result.getRunCount(), is(2));
assertThat(result.getFailureCount(), is(0));
}
source
share