bkoodaa
bkoodaa

Reputation: 5322

Unable to run JUnit tests inside a Maven project in IntelliJ IDEA

I have a 3-year old Maven project that was working fine in IntelliJ IDEA 3 years ago. I'm trying to return to the project now, but I can't get JUnit tests to run anymore. There's nothing fancy with the tests themselves, it's really basic @Test and assertTrue stuff. I suspect the issue lies with my pom.xml file.

If I try to run tests with the original pom.xml file from 3 years ago, I get an error "No tests found". I tried to update my dependencies, and after updating I get a different error: java.lang.NoSuchMethodError: 'java.util.Optional org.junit.jupiter.api.extension.ExtensionContext.getTestInstanceLifecycle()'. It appears to be some kind of internal error from JUnit. I've spent 1 hour googling, and I've randomly tried different ways of setting up the dependencies, but nothing so far works.

How should a pom.xml file look in order to allow basic JUnit tests to be run in a Maven project in IntelliJ IDEA 2019.3.2 Community Edition?

Here is the pom.xml file from 3 years ago.

Upvotes: 0

Views: 1967

Answers (1)

Mark Bramnik
Mark Bramnik

Reputation: 42431

All-in-all you should create a dependency on junit-jupiter-engine and not on the api (the engine will bring the api) and upgrade a version of surefire plugin that runs the tests:

    <dependencies>

        <!-- junit 5 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    ...
     <build>
        <plugins>

            <!-- Requires at least 2.22.0 otherwise junit 5 doesn't work-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
            </plugin>
          ...
         </plugins>
   </build>

You can find the working example of minimal setup (that is pretty close to you fairly basic pom.xml) in this tutorial

Upvotes: 1

Related Questions