Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info

The tutorial repository can be found here:

https://gitlab.eufus.eu/bpogodzinski/ach-tutorials/-/tree/TDD-java/TDD-java/Overview

Test class is in src/test/java/Junit5ExampleTestJUnit5OverviewTest.java 


A basic test class looks like this:

Code Block
import org.junit.jupiter.api.DisplayName*;
 
@DisplayName("JUnit 5 Overview Exampleclass")
class JUnit5ExampleTest JUnit5OverviewTest {

    @Test
    void exampleTest() {
        System.....out.println("example test method");
    }

}


 Setup and Teardown methods

...

Code Block
import org.junit.jupiter.api.*;
 
@DisplayName("JUnit 5 Example")
class JUnit5ExampleTest {
 
    @BeforeAll
    static void beforeAll() {
        System.out.println("Before all test methods");
    }
 
    @BeforeEach
    void beforeEach() {
        System.out.println("Before each test method");
    }
 
    @AfterEach
    void afterEach() {
        System.out.println("After each test method");
    }
 
    @AfterAll
    static void afterAll() {
        System.out.println("After all test methods");
    }
}


@DisplayName and @Disabled

Now let's move to new test-optional methods:

@DisplayName("Single test successful")
@Test
void testSingleSuccessTest() {
    log.info("Success");
}
 
@Test
@Disabled("Not implemented yet")
void testShowSomething() {
}


As we can see, we can change the display name or disable the method with a comment, using these annotations.


After we have added setup and teardown methods to our test class, we can finally write our first test methods. Let's find out how we can do it.

...