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

JunitIntellij IdeaMaven Surefire-Plugin

Junit Problem Overview


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?

Junit Solutions


Solution 1 - Junit

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

Solution 2 - Junit

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.

Solution 3 - Junit

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> --> 

Solution 4 - Junit

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.

Solution 5 - Junit

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlueLettuce16View Question on Stackoverflow
Solution 1 - JunitHendrik JanderView Answer on Stackoverflow
Solution 2 - Junittak3shiView Answer on Stackoverflow
Solution 3 - JunitnandeeshView Answer on Stackoverflow
Solution 4 - JunitJaime CaseroView Answer on Stackoverflow
Solution 5 - JunitBlueLettuce16View Answer on Stackoverflow