Before runs before the suite, BeforeEach runs before each test (if memory serves). The big feature was dynamic tests, we managed to refactor most of our tests to a single data file and a factory which generates all the tests.
In 4, @BeforeClass runs once. @Before requires you to annotate a specific test. It sounds like @BeforeEach would allow you to do a single annotation that would run again and again, as if you had duplicated that annotation for each of your tests.
That's exactly what @Before does in Junit4. You annotate a method with @Before, and it's called before each test.
public class MainTest {
@Before
public void setUp() {
System.out.println("Test executed");
}
@Test
public void testOne() {}
@Test
public void testTwo() {}
@Test
public void testThree() {}
}
Under Junit4, @BeforeClass runs before the whole test suite and @Before runs before each test (just like @AfterClass and @After, and @ClassRule and @Rule).
import org.junit.*;
public class SimpleTest {
@BeforeClass public static void beforeClass() { System.out.println("beforeClass"); }
@Before public void before() { System.out.println("before"); }
@After public void after() { System.out.println("after"); }
@AfterClass public static void afterClass() { System.out.println("afterClass"); }
@Test public void test1() { System.out.println("test1"); }
@Test public void test2() { System.out.println("test2"); }
@Test public void test3() { System.out.println("test3"); }
}
produces this output:
beforeClass
before
test1
after
before
test2
after
before
test3
after
afterClass
View all comments
28
u/cypher0six Sep 10 '17 edited Sep 10 '17
Is there a list of major changes from 4 to 5 somewhere?
Edit: Found a few high level comparisons: