How to run kotlin test files with mvn test?

268 views Asked by At

I want to run multiple kotlin test files in multiple folders within src/test/kotlin. The problem is when I run mvn test then some tests are running but not all of them. I tried different solutions like adding kotlin-maven-plugin and adding source directory to pom.xml. I also added regExp to recognize all tests. How can I run mvn test so all kotlin tests will be recognized?

Also I use Junit jupiter api therefore this dependency.

import org.junit.jupiter.api.Test  

to all test files and added annotation @Test

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

This was my solution. I don't have Java in the project.

<build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <artifactId>kotlin-maven-plugin</artifactId>
                <groupId>org.jetbrains.kotlin</groupId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>

                    <execution>
                        <id>test-compile</id>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
                            </sourceDirs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <includes>
                        <include>Test*</include>
                        <include>*Test</include>
                        <include>*TestCase</include>
                    </includes>
                </configuration>
            </plugin>
...
</build>

These were the results , sneak peak:

[INFO] Running nl.ebpi.domain.SentListTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in nl.ebpi.domain.SentListTest
[INFO] Running nl.ebpi.domain.StatusTest
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in nl.ebpi.domain.StatusTest
[INFO] Running nl.ebpi.domain.DocumentListTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in nl.ebpi.domain.DocumentListTest
[INFO] Running nl.ebpi.domain.TypeTest
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in nl.ebpi.domain.TypeTest
...
[INFO] Results:
[INFO] 
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  26.411 s
[INFO] Finished at: 2023-01-13T17:37:28+01:00
[INFO] ------------------------------------------------------------------------

I tried to run mvn test, but only few passed. Even the files that recognize few tests, have more tests.

1

There are 1 answers

1
AndrewL On

You seem to be doing everything correctly. Does your IDE run the tests at the method/class/package level? Yes, this is a different execution method, but it might help you highlight where the problem comes from.

I assume you have no @Disabled kind of annotations.

My Maven config for a small project follows - note how I don't have surefire nor any of the filters you have, and I too am using the org.junit.jupiter.api.Test annotation on the tests.

    <build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.6</version>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>