jQuery in Action, 3rd edition
The moose likes Ant, Maven and Other Build Tools and the fly likes How to run UT in Eclipse/Maven Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "How to run UT in Eclipse/Maven" Watch "How to run UT in Eclipse/Maven" New topic

How to run UT in Eclipse/Maven

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 397
I'm running Eclipse SDK 3.3.0 and Maven 2.0.7.
I have created a new empty project. I use maven to download junit4.0.jar
into the Maven2 Dependences.

I'm trying to follow the demo at http://m2eclipse.codehaus.org/Maven_2.0_Plugin_for_Eclipse.html except use junit4 and it is not working!

All I want to do is create a simple Unit test in eclipse and execute it maven. What am I doing wrong?
See below for more details.

I right click on the project -> new -> Junit Test and it creates a package
for me that is in the top level directory for that project? Should it not be
under main/test?

Well here is my test:

package m2Test;
import static org.junit.Assert.*;
import org.junit.Assert;
public class simple2 {
public void simple(){
Assert.assertNotNull(new String());

Well I right click on the project again and select Run As -> JUnit Test and
it says 'No tests found with test runner 'JUnit 4'. Why not?

So then I move the test into the main/test and I still get the same result.
So then I try a JUnit 3 test and that does not work either (scroll way down
to see my JUnit3 test). So then I try to get maven to execute the tests (I
right click on pom.xml, select run as -> maven 2 buld ) and it cannot find
any tests to run either. Maven does run a C++ project in the same workspace!

Is this an eclipse problem or a maven plugin problem? I think it is (at
least in part) an eclipse issue since it cannot find my tests with just the
'Run As -> JUnit Tests'.



[INFO] ----------------------------------------------------------------------------

[INFO] Building Unnamed - demo:Maven_Hibernate_Spring_Demo:jar:0.0.1

[INFO] task-segment: [test]

[INFO] ----------------------------------------------------------------------------

[INFO] resources:resources

[INFO] Using default encoding to copy filtered resources.

[INFO] compiler:compile

[INFO] No sources to compile

[INFO] resources:testResources

[INFO] Using default encoding to copy filtered resources.

[INFO] compiler:testCompile

[INFO] No sources to compile

[INFO] surefire:test

[INFO] No tests to run.

[INFO] ----------------------------------------------------------------------------


[INFO] ----------------------------------------------------------------------------

[INFO] Total time: 0 second

[INFO] Finished at: Mon Sep 17 23:01:40 MDT 2007

[INFO] Memory 2M/6M

[INFO] ----------------------------------------------------------------------------

// JUnit 3 test:

package m2Test;

import junit.framework.TestCase;

* @author Siegfried Heintze
public class SimpleJUnit3 extends TestCase {

* @param name
public SimpleJUnit3(String name) {

/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
protected void setUp() throws Exception {

/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
protected void tearDown() throws Exception {
public testSimple(){
SimpleJUnit3 s = new SimpleJUnit3("demo");

Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Here's what I did...

I created a new project in Eclipse:

Right Click in 'Project Explorer' -> New -> Project -> Maven Project.

I had it create src/main/java, src/main/resources, src/test/java, and src/test/resources.

I right clicked on the src/test/java source folder (if you click on the project it defaults to src/main/java, and you are correct, for maven to run them as tests in the build, they need to be in the src/test/ hierarchy) and created a new unit test:

Right Click 'src/main/Java' -> New -> Other -> Java -> JUnit -> JUnit Test Case

I chose JUnit 4 in the dialog. It warned me that JUnit 4 libraries were not included in the project. Don't add JUnit libraries to the project here... Eclipse will include it in the Eclipse project - NOT in the Maven project. Just create your test - Eclipse will show the red lines under the JUnit import, but we'll fix that. Add JUnit 4 to the Maven project:

Right Click 'pom.xml' -> Maven -> Add Dependency

type 'junit' in the search box, find the 4.0 version of the junit jar and add it as a dependency. Wait a bit, the project will re-build and the red mark will disappear in Eclipse because the it can now find the JUnit libraries.

You can now build your Maven project and have the test run:

Right click 'pom.xml' -> Run As... -> Maven test

You will see lots of output and (hopefully) a 'Build Successful' message.

You can also run the unit tests in Eclipse:

Right click 'src/test/java' -> Run As... -> Unit test

You'll see Eclipse's JUnit GUI, and (hopefully) a fully green bar showing a successful test run.

Notes: Eclipse 3.3.0, Maven 2.0.7, JDK 1.6.0_02, and the Development version of the Maven plugin from CodeHaus (version is

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link: http://aspose.com/file-tools
subject: How to run UT in Eclipse/Maven
It's not a secret anymore!