r/java • • Sep 10 '17

JUnit 5 released!

http://junit.org/junit5
213 Upvotes

33 comments sorted by

28

u/cypher0six Sep 10 '17 edited Sep 10 '17

3

u/mreichman Sep 11 '17

Does @BeforeAll still require a static method?

2

u/sormuras Sep 12 '17

It depends: @BeforeAll method must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).

See http://junit.org/junit5/docs/current/user-guide/#writing-tests-test-instance-lifecycle for details.

1

u/Feroc Sep 11 '17

Yes.

1

u/emberko Sep 12 '17

Good. It means there are no any reasons to migrate from TestNG.

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?

8

u/cdombroski Sep 10 '17

You appear to be correct, according to the notes. Before/After become BeforeEach/AfterEach and BeforeClass/AfterClass become BeforeAll/AfterAll.

-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.

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.

3

u/Butterstick1108 Sep 10 '17

2

u/cypher0six Sep 10 '17

Yeah, saw that. Was looking for something a bit more high level than the entire user guide. Thanks though.

20

u/omgdonerkebab Sep 10 '17

I'm super excited for nested tests.

https://blog.codefx.org/libraries/junit-5-basics/#Nesting-Tests

Though I prefer Java, for the last year I've been working at a Ruby on Rails shop. But there have been some (few) things that the Ruby ecosystem has gotten right, and one of those is nested tests in Rspec.

Now I can better group my tests up, for example, to test the behavior of a service class for different users or different contexts. And more importantly, I can set up that context with a @BeforeEach method that applies only to the tests in that group.

2

u/jonhanson Sep 11 '17 edited Mar 08 '25

chronophobia ephemeral lysergic metempsychosis peremptory quantifiable retributive zenith

2

u/sormuras Sep 12 '17

That's true -- but the Platform is the basis for other test engines, like http://jqwik.net -- Example Based Testing and Property Based Testing

1

u/cryptos6 Sep 13 '17

Another option providing property based testing would be KotlinTest. However you would write tests in Kotlin (what would be more concise and fun, but is not always possible).

1

u/Feroc Sep 11 '17

Migrated one of our smaller projects this morning. Finding and using the extension for Mockito was the "hardest" part. The rest was pretty straight forward.

1

u/diroussel Sep 11 '17

Got a link for that to save me the time?

1

u/Feroc Sep 11 '17

A link to what?

1

u/diroussel Sep 11 '17

You said finding the extension to mockito was the hard bit. I was hoping you could link to it.

1

u/cryptos6 Sep 13 '17

Is there no such thing as "before suite" and "after suite" to have global setup and teardown? I'm looking for something like TestNG's @BeforeSuite or KotlinTest's beforeAll

1

u/winder Sep 14 '17

That would be @BeforeAll and @AfterAll

1

u/cryptos6 Sep 14 '17

No, @BeforeAll is executed before all tests of the class. From the documentation:

@BeforeAll: Denotes that the annotated method should be executed before all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class;

0

u/[deleted] Sep 10 '17

[deleted]

2

u/secondsun Sep 10 '17

On my phone but I'll try. There's setup that isn't intuitive and it seems like a lot of breaks for no good reason at first. However it has a lot of configurability that didn't exist and parameterized tests are pretty awesome.

As an example you can look at this file and see halfway through I changed to csv parameterized tests. https://github.com/secondsun/sms4j/blob/master/src/test/java/net/saga/console/emulator/sms/sms4j/test/E03_Z80DecrementTest.java

As you can see I switched from each test being for a specific register and then went to passing in the codes as a csv set.

1

u/[deleted] Sep 11 '17

I've been using it. Rewrote some rules into extensions. Extensions give more control than rules/runners. Also need less boilerplate code to add them all to a bunch of tests.