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.

2

u/v_krishna Sep 10 '17

So in junit 5 you could declare a @BeforeEach once and it would act like you did @Before each test? I could see that being handy...

9

u/[deleted] Sep 10 '17

I really don't see what it changes, apart from the fact that the names changed. But there are no new behaviours...

-2

u/v_krishna Sep 10 '17

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.

11

u/GleemanGiveth Sep 11 '17

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() {}
}

Output:

Test executed
Test executed
Test executed

This is just a name change, not behavior.

2

u/v_krishna Sep 11 '17

Ah I stand corrected. Thanks for the clarification.