r/java • • Sep 10 '17

JUnit 5 released!

http://junit.org/junit5
213 Upvotes

33 comments sorted by

View all comments

Show parent comments

5

u/Trout_Tickler Sep 10 '17

There was one but the features I switched for were @BeforeEach and dynamic tests.

10

u/crummy Sep 10 '17

isn't BeforeEach just like Before?

-10

u/Trout_Tickler Sep 10 '17

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.

1

u/experts_never_lie Sep 10 '17 edited Sep 10 '17

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

-2

u/Trout_Tickler Sep 10 '17

Eh, as I said dynamic tests was the real big deal.