IntelliJ Error when running unit test: Could not find or load main class ${surefireArgLine}
Asked Answered
N

6

91

I get the following error when running Unit tests in IntelliJ: Error: Could not find or load main class ${surefireArgLine}. I am using maven and in pom.xml I have:

<properties>
    ...
    <surefire.argLine />
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>1.1.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/generated-sources/java</outputDirectory>
                        <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
             <!--Sets the VM argument line used when unit tests are run.-->
            <argLine>${surefire.argLine}</argLine>
        </configuration>
    </plugin>
  <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.1.201405082137</version>
            <executions>
                <!--
                    Prepares the property pointing to the JaCoCo runtime agent which
                    is passed as VM argument when Maven the Surefire plugin is executed.
                -->
                <execution>
                    <id>pre-unit-test</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <!--Sets the path to the file which contains the execution data.-->
                        <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                        <!--
                            Sets the name of the property containing the settings
                            for JaCoCo runtime agent.
                        -->
                        <propertyName>surefireArgLine</propertyName>
                    </configuration>
                </execution>
   ...

Did anyone have similiar problem? How to set value for surefireArgLine?

Nicotinism answered 9/6, 2014 at 6:57 Comment(0)
R
324

I had the same problem and i think i found the solution on the vertx-issue tracker.

In short you have to configure your IntelliJ Maven (surefire plugin) integration to behave differently.

This works for me in IntelliJ 14.1.6 with mvn 3.3.9 Preferences -> Build,Execution,Deployment -> Build Tools -> Maven -> Running Tests

For IntelliJ 2019 and above Settings-> Build,Execution,Deployment -> Build Tools -> Maven -> Running Tests

Uncheck argLine

Rouse answered 13/2, 2016 at 10:11 Comment(11)
Thanks its the same problem here in start of 2016 ;)Entellus
In Intellij 2016: Settings -> Build, Execution, Deployment -> Build Tools -> Maven -> Running TestsStylite
Same problem, but sans surefire. Getting error "Could not find or load main class ${argLine}". This works.Chlamys
Thank you...what are the chances of finding such a post when trying to make changes to a build and making sure it does not have side effects for the developers...god bless stackoverflow.Bornstein
Hi from 2018 :) This is a precious piece of solution. Thank you very much.Whitefly
In Intellij 2018: File > Settings > Build, Execution, Deployment > Build Tools > Maven > Running TestsAnglim
Similar problem for me with error message containing @{argLine}. Resolution works.Cochabamba
problem still happening, unchecking argLine fixed for meBecalmed
That feeling when you realize it's not the first time you're reading an answer. I was going to upvote it but then I saw it was already upvoted by me :-)Lexicology
Seems we can upvote for issue youtrack.jetbrains.com/issue/IDEA-145991 .Temptation
April 2021, still a valid answer.Malia
A
14

I was able to fix this error in Netbeans by changing the surefire-plugin version to 2.10 and removing

<argLine>-Xmx1024m -XX:MaxPermSize=256m ${argLine}</argLine>

from the maven-surefire-plugin configuration. Instead i have created a property argLine that is picked automatically by surefire.

<properties>
    <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
  </properties>

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.10</version>
      </plugin>

Now, i can run and debug single files and test methods. And Code Coverage is working as expected.

Acus answered 4/12, 2015 at 11:5 Comment(1)
This works for IntelliJ too. This option is mentioned in the JaCoCo documentation.Hookah
C
4

Update of pom.xml solved my problem.

<argLine>${surefire.argLine}</argLine>

Complete plugin info in pom.xml

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>    
            <version>2.18.1</version>                 
            <configuration>
                <parallel>classes</parallel>
                <threadCount>10</threadCount>
                <workingDirectory>${project.build.directory}</workingDirectory>   
                <jvm>${env.JDK1_8_HOME}\bin\java</jvm>   
                <argLine>${surefire.argLine}</argLine>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit4</artifactId>
                    <version>2.18.1</version>
                </dependency>
            </dependencies>
     </plugin> --> 
Checkerbloom answered 8/5, 2018 at 14:16 Comment(0)
D
1

Was looking for this and found this project "fix" it in this thread

Basically define your jacocoArgLine var name as empty project property. Then in surefire configuration use @{jacocoArgLine} instead of dollar prefix.

Decalogue answered 2/2, 2018 at 18:26 Comment(0)
N
0

I found out that I have to run my test case from maven with mvn -Dtest=TestCircle test not directly from IDE.

Nicotinism answered 9/6, 2014 at 7:16 Comment(4)
Did you ever found another sollution for this? I need to run it from my IDE to be able to use debug mode.Stantonstanway
Unfortunatelly I haven't found another solution for this.Nicotinism
@Stantonstanway you can debug tests without running the tests in an IDE. Just start the tests with mvnDebug on the command line. Then use your favorite IDE to connect to the command line process. Google for "remote debug" in your favorite IDE.Rouse
@Nicotinism solution proposed by "jah" (has 200+ ups) is much better, could you please mark accordingly?Messy
S
0

For a more permanent fix for every new project add the following to your IntelliJ IDEA Custom VM Options:

  1. Help > Edit Custom VM options
  2. Add: -Didea.maven.surefire.disable.argLine=true

This will deactivate surefire for all new projects you open.

Sugar answered 2/6, 2022 at 8:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.