51

I have a large Maven project which has several modules in it. When I want to run a JUnit test from one module I run 'mvn -Dtest=NameOfTest test' in the directory that contains all the modules. When I run this command Maven goes through each module and tries to compile it (though it's already compiled), which involves copying a bunch of files and adds to the total time of the test. It seems that the 'test' command for the Maven surefire plugin executes all the steps up to the test. I was wondering if there is a way to execute only the test step and not bother with all the attempted compilation and copying of files.

Here is some output from before the test starts:

[INFO] 
[INFO] --- build-helper-maven-plugin:1.5:add-test-source (add-test-source) @ module1 ---
[INFO] Test Source directory: <directory in module1 with some generated sources> added.
[INFO] 
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ module1 ---
[debug] execute contextualize
[INFO] Copying 108 resources
[INFO] Copying 1113 resources
[INFO] Copying 1 resource
[INFO]

It repeats this for each of the other modules. All told it takes a minute or two before it actually starts the test. Does anyone know a way to get the test to run without bothering with all the compilation beforehand? Please let me know if there's any more information I should provide.

Ian
  • 599
  • 2
  • 5
  • 13

2 Answers2

91

If what you would like to do is to run just the test phase of the lifecycle without running all of the previous phases you could also call the goal that is bound to the test phase:

mvn surefire:test

or if you want to run just one test

mvn -Dtest=NameOfTest surefire:test
Akro
  • 1,916
  • 14
  • 11
  • 1
    In my case, `mvn test -DfailIfNoTests=false -Dtest=package.name.TestClass#testMethod -pl subModuleName -am` works rebuilding module and module dependencies. But `mvn surefilre:test -DfailIfNoTests=false -Dtest=package.name.TestClass#testMethod -pl subModuleName -am` fails to resolve module dependencies – user1767316 Jan 24 '20 at 14:30
1

What is wrong with simply running the test from within the module the test resides in? That way, Maven will not attempt to build the other modules which you are not interested in.

JamesB
  • 7,774
  • 2
  • 22
  • 21
  • @johntrepreneur suggested that as well. It doesn't work in my case for the specific code base I'm working with. If you know that it works in general I can spend some time trying to get it work. – Ian Feb 14 '13 at 19:23
  • @Ian I don't see anyone else suggesting what I have. Can you confirm where you are running this command from? You implied it was from the top level project containing all the modules. I am saying run the command from within the desired module itself. – JamesB Feb 14 '13 at 19:27
  • Hmm. Maybe he deleted the comment. I had actually tried what you're suggesting before I posted here, running the test from inside the module instead of the top-level directory, but something in the tests that I need to run threw an exception because it couldn't find some resource it needed, I think. It's a good suggestion though, and I think it would work for other people. – Ian Feb 14 '13 at 19:33
  • The problem then is that your test are not actually unit tests and should be fixed.. – Manfred Moser Feb 14 '13 at 22:45