Search code examples
jenkinsjunit5

Junit5 @BeforeEach doesn't get executed/seen in jenkins?


Running Junit5 tests (localhost) on Jenkins works but BeforeEach, AfterEach hooks do not fire. Surprisingly, when I run those in intelliJ IDE referencing the same path/directory which Jenkins consumes, the hooks execute.

Unit test class sample:

import org.junit.jupiter.api.*;
 @BeforeEach
    public void beforeEach() throws Exception {
        System.out.println("BeforeEach");
    }

pom.xml junit part:

  <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
  </dependency>

I don't get any errors, warnings when running on Jenkins, it just gets skipped. Inserting the @BeforeEach logic inside the actual @Test but obviously I don't want that.

Am using maven to execute the tests: mvn test -Dtest=unittest.partner.SampleTests


Solution

  • After trying various stuff this worked: inserting maven-surefire-plugininside inside pom.xml

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.1.2</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.9.1</version>
                </dependency>
            </dependencies>
        </plugin>