My goal is to run different versions of Junit Framework work. Surefire
supports three different generations of JUnit: JUnit 3.8.x, JUnit 4.x
(serial provider) and JUnit 4.7
http://
http://maven.apache.org/plugins/maven-surefire-plugin/exampl[..]
My pom file has the following provider:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.12</version>
</dependency>
</dependencies>
</plugin>
And dependencies:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
I have no problems compiling and running TestNG. However the same compiled
Junit test will sometimes run and sometimes it wont. I have tried with
different types of hello world simple tests. I have tried with version 3.8.1
and version 4.8.1. Maven seems to have an executable jar file for both
versions in the repository. All my Test classes start with Test and they are
in the requiered directory.
What I usually do is change the pom, save it, call mvn test and see if the
test is run. I remove the ones that dont work and add different ones in hope
that they do. Typical compilation Compilation error occurs when using the
import statement:
Incompatible type
Cant find Symbol
I am completely baffled that it has run at times and others not. And yes my
point is to make a comparison of different testing frameworks as Junit 4.x
is basically a new framework.
Please upload your complete example project to github or similar, so
someone can have a look at it.
The combination in your pom is illegal. The 4.7 provider requires
JUnit 4.7 or higher.
Kristian
In my 15 hour quest to stubornly solve this I have not thanked everyone who
has come with their useful remarks. This is a very impressive forum.
I use now is
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>[4.8.1]</version>
<scope>test</scope>
</dependency>
</dependencies>
I have removed all other source and test files.
The brackets force it to use this version. TestNG needs Junit 3.8.1 but I
removed both to just see if I can compile this simple test placed under
C:\Users\Gogirl\_MavenProjects\mavenHelloWorld\src\test\java\com\mycompany\app
created by the quickstart archetype. Test file:
package com.mycompany.app;
import org.junit.*;
// by FYICenter.com
public class TestJunit4.8{
@Test
public void testHello() {
String message = "Hello World!";
Assert.assertEquals(12, message.length());
System.out.println("I am running Junit 4.8.2");
}
}
mvn test won't compile now and claims that it expect a {
I have downloaded my own junit 4.8.2 and its in my classpath and i could
compile and run it fine with the cmd.exe.
I have a clean machine with the latest downloaded versions.
here is the complete pom:
<project xmlns=" http://maven.apache.org/POM/4.0.0"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"> >
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>mavenHelloWorld</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mavenHelloWorld</name>
<url> http://maven.apache.org</url> >
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>[4.8.1]</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
It's really a bare bones project now.Although these articles were
particularly helpful
http://stackoverflow.com/questions/2021771/surefire-is-not-p[..]
http://stackoverflow.com/questions/5845990/maven-3-and-junit[..]
http://stackoverflow.com/questions/739076/why-cant-java-find[..]
It has not explained the erratic behaviour i get. I haven't tried github but
will look into it thx.
Some answers inline:
The square brackets are redundant here. You could just use:
<version>4.8.1</version>
for the same effect.
This is not a legal classname. I'm surprised that it compiles anywhereat all.
The version that compiles does have a legal classname. I was trying to get a
more meaningful compile error message than "missing {" and forgot to change
the name back when i posted. It's an intriguing point none-the-less about
the usefulness of the error messages.
I ran 3 more tests. Conclusion any attempt to use junit fails regardless of
version and testng works.
Changed pom back to version to 3.8.1
Used new legal java file:
package com.mycompany.app;
import junit.framework.*;
public class TestMe extends TestCase {
public void testAdd() {
int num1 = 3;
int num2 = 2;
int total = 5;
int sum = 0;
sum = Math.add(num1, num2);
assertEquals(sum, total);
}
}
MAVEN ERROR: Cannot find symbol
**********************************************
Changed the pom back to a testng dependency with just this test file:
package com.mycompany.app;
import java.util.*;
import org.testng.Assert;
import org.testng.annotations.*;
public class TestNGCollection {
private Collection collection;
@BeforeClass
public void oneTimeSetUp() {
// one-time initialization code
System.out.println("@BeforeClass - oneTimeSetUp");
}
@AfterClass
public void oneTimeTearDown() {
// one-time cleanup code
System.out.println("@AfterClass - oneTimeTearDown");
}
@BeforeMethod
public void setUp() {
collection = new ArrayList();
System.out.println("@BeforeMethod - setUp");
}
@AfterMethod
public void tearDown() {
collection.clear();
System.out.println("@AfterMethod - tearDown");
}
@Test
public void testEmptyCollection() {
Assert.assertEquals(collection.isEmpty(),true);
System.out.println("@Test - testEmptyCollection");
}
@Test
public void testOneItemCollection() {
collection.add("itemA");
Assert.assertEquals(collection.size(),1);
System.out.println("@Test - testOneItemCollection");
}
}
Compiles and runs happily
A small point of correction, this is not a Maven error but rather a
compiler error that is being reported by Maven.
Can you please copy and paste the actual error text (not the whole log,
just a couple lines please) that show the cannot find symbol error plus a
couple lines before and after for context?
It may also be instructive to you if you use mvn -X so you see additional
debugging text and you can try to run the compilation for yourself using
the same javac... command that Maven is attempting to use.
Something more is going on here as many people use JUnit very successfully.
Wayne
I am sure that the answer to my problem is here somewhere but I can't find
it:
http://maven.apache.org/plugins/maven-surefire-plugin/exampl[..]
"You can also manually force a specific provider by adding it as a
dependency to Surefire itself..
When using this technique there is no check that the proper test-frameworks
are present on your project's classpath. Failing to add the proper
test-frameworks will result in a build failure."
To simplify further, I removed the provider but I get the same error
messages.
Could this be a classpath problem? If so, which of the many junit jars
downloaded by Maven need to be added?
Hi Wayne:
I am not sure how many are running parallel versions of junit. As mentioned,
JUnit has two important distinct versions: JUnit 3, the traditional library
and JUnit 4, a complete redesign. I saw Maven run some JUnit 3 examples and
not JUnit 4, before it refused to compile and run any. I had misunderstood
the part that a provider is needed in the pom. It does not seem so but as I
wrote removing it did not do much.
[INFO]
------------------------------------------------------------------------
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:2.
3.2:testCompile (default-testCompile) on project mavenHelloWorld:
Compilation fa
ilure
[ERROR] \Users\_MavenProjects\mavenHelloWorld\src\test\java\com\mycompan
y\app\TestMe.java:[12,18] error: cannot find symbol
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute
goal o
rg.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile
(default-testCom
pile) on project mavenHelloWorld: Compilation failure
\Users\_MavenProjects\mavenHelloWorld\src\test\java\com\mycompany\app\Te
stMe.java:[12,18] error: cannot find symbol
at
org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
.java:213)
File used:
package com.mycompany.app;
import junit.framework.*;
public class TestMe extends TestCase {
public void testAdd() {
int num1 = 3;
int num2 = 2;
int total = 5;
int sum = 0;
sum = Math.add(num1, num2);
assertEquals(sum, total);
}
}