randomuser1
randomuser1

Reputation: 2803

tests are not run during maven test procedure from IDE (using surefire)

In my Spring app I have tests named properly:

src
 -main
 -test
    -java
       -com.mypackage.sth
           -utils
              SomethingTest.java

I can run it straight from the IDE intellij, but when I chose it from maven window:

enter image description here

I'm getting info:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

in my pom.xml file I have this section:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.4.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

I also have a dependency added above:

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

The test still don't run though. How can I fix it? What's the main problem here?

----- EDIT I did exactly what Naor Tedgi suggested in the first answer, however right after adding:

<sourceDirectory>src/main/..</sourceDirectory>
<testSourceDirectory>src/test/..</testSourceDirectory>

to the <build> tag, my project structure went nuts, after doing so it looks like this:

src
 -main
 -test.java.com.mypackage.sth
      -utils
           SomethingTest.java

and all the symbols in test classes are not resolvable. Now when I try to run tests, they run! so that's a good thing, but there are lots and lots of errors, e.g.

package org.junit does not exist

etc.

--- EDIT v2 okay, I edited my pom.xml so that now it contains these two lines in <build>:

<sourceDirectory>src/main/java/</sourceDirectory>
<testSourceDirectory>src/test/java/</testSourceDirectory>

errors disappeared and the project is compilable, however it still didn't help - the tests did not run

Here's the output from mvn test:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myProject 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ projectName ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ projectName ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ projectName ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\path\to\myProject\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ projectName ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ projectName ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.181 s
[INFO] Finished at: 2019-02-27T17:16:29+01:00
[INFO] Final Memory: 22M/224M
[INFO] ------------------------------------------------------------------------

Upvotes: 1

Views: 322

Answers (1)

Naor Tedgi
Naor Tedgi

Reputation: 5707

first, extract Junit outside of build tag into dependencies

then add this lines to build

   <sourceDirectory>src/main/{PATH TO SRC}</sourceDirectory>
   <testSourceDirectory>src/test/{PATH TO TESTS}</testSourceDirectory>

should look something like this

<project>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>

   <sourceDirectory>src/main/{PATH TO SRC}</sourceDirectory>
   <testSourceDirectory>src/test/{PATH TO TESTS}</testSourceDirectory>

        <plugins>
            <!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
        </plugins>
    </build>


</project>

Upvotes: 1

Related Questions