Search code examples
javamavenjunitmaven-surefire-pluginjunit5

Maven does not run @BeforeEach Methods while running


i have a test Class lets call it TestSomething, and a Test Object lets call this one SomeObject.

Now i need this Object in every Single Test new this means that i have in my Code a @BeforeEach that loads this Object in a Field:

import me.test.SomeObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class TestSomething {
    private SomeObject someObject;

    @BeforeEach
    public void load() {
        someObject = new SomeObject();
    }

    @Test
    public void test1() {
        boolean result = someObject.checkForSomething();
        Assertions.assertEquals(true, result);
    }

    @Test
    public void test2() {
        boolean result = someObject.checkForSomethingElse();
        Assertions.assertEquals(false, result);
    }

pom.xml from the Test Module:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <parent>
    <artifactId>test</artifactId>
    <groupId>me.test</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>


  <properties>
    <projectVersion>1.0.0</projectVersion>
    <maven.deploy.skip>false</maven.deploy.skip>
  </properties>


  <artifactId>Tests</artifactId>
  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.0.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>me.test</groupId>
      <artifactId>project</artifactId>
      <version>1.0-SNAPSHOT</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

not sure if it is relevant, but the Object SomeObject is in a separate Module, and the Test Module has a Dependency on that Module with Scope test. (i also tried provided and compile)

So now if i Run this Tests in InteliJ they Work just Fine. but now if i try to Build my Project the Tests Fail, with NullPointerExceptions because someObject is null.

Now the Test work of i call the Method load() in every Test, but that is not exactly what i want.


Solution

  • By default Maven will not run the test with the Jupiter engine as

    In order to have Maven Surefire run any tests at all, a TestEngine implementation must be added to the runtime classpath.

    And this is not present by default.
    So to enable it you have to configure the maven-surefire-plugin that runs the unit tests as documented in the Jupiter documentation :

    UPDATE (28.10.2020):

    Since version 2.22.0, you only have to specify a test dependency on the desired junit engine. Failing to do so, will also result in the behavior described in the question.

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

    Keeping the original answer as a reference, before version 2.22.0 the solution was:

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.21.0</version>
                <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.2.0</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.2.0</version>
                </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    

    Whatever the issue is not necessarily easy to spot because Maven uses a runner that is able to run the Jupiter tests but didn't manage to execute the hook methods...

    As a hint : to know whether the JUnit 5 runner is launched you can execute the tests with the verbose flag such as : mvn test -X.
    If the Jupiter runner is used, you should find lines that look like :

    [DEBUG] Surefire report directory: ...\target\surefire-reports

    [DEBUG] Using configured provider org.junit.platform.surefire.provider.JUnitPlatformProvider