Maven, Intellij, Spring and Unit Tests problems. Configuration help needed.

I have a large multi-project application that uses Maven2 for building and Spring for configuration. When I run some of the Spring based unit tests they pass in Maven2 but fail when run from Intellij. Our Spring expert feels that the problem has its roots in the primary difference in the classpath between Maven2 and Intellij.

When the tests are run from Maven2, the tests see a module's source and test class directories and the jars of all its dependent modules.

When the tests are run from Intellij, the tests see not only the current module's source and test class directories but also the source and test class directories of all the dependent modules. This apparently causes problems with Spring because some dependent modules have Spring configuration files in their test class directories that interfere with the current module's configuration.

What I need is a way to exclude the test class directories of the dependent modules from the current module's classpath when running the tests. Is this possible?

Alternatively, I could use an option during the Maven 2 project import that says depend on the dependent modules jar files and not their classes directories. Apparently Eclipse has such an option.

Erik Husby

10 comments
Comment actions Permalink

There is an option in JUnit run configurations: Use classpath and jdk of module.
Does using the option with a particular module helps?

0
Comment actions Permalink

No the JUnit run configuration "use classpath and jdk of module" does not help. Because the classpath of the module includes the exported classpath elements of the dependent modules. Each module always exports both its compiler output directories. I need a way to exclude the test output path of the dependent modules.

0
Comment actions Permalink

By dependent modules you mean dependencies?

IDEA do not currently support production/test classpart separation.

There are two ways to solve the problem with config interference. First is to reorder the dependencies so that the desired one goes first. And, second, to create separate modules for production classes and for test classes. This way you may set test dependencies so that there will be no interference.

0
Comment actions Permalink

>> By dependent modules you mean dependencies?
Yes, my project has 34 modules in it.

I guess I will file an enhancement request to allow fine grain separation of production and test class path exporting.

0
Comment actions Permalink

Erik, I reckon we have misunderstanding.
Do you mean to say that when running a test from module A, classpath contains classes from module B that depends on module A?

0
Comment actions Permalink

OK, Assume two modules, A & B. A depends on B

In both modules, the compiler outputs are target/classes and target/test-classes.

When I run a test in Module A, the classpath contains B/target/classes and B/target/test-classes. I want to exclude B/target/test-classes. Because there are configuration files in B/target/test-classes that are specific to running a test in module B but these configuration files should not be seen when running the tests in Module A.

0
Comment actions Permalink

Ok, now I understand. Thanks for clarification 8)
As I said before, the workaround is to have separate modules for production and for test classes.

Please create a jira issue for the request.

Thanks,
Anton Makeev

0
Comment actions Permalink

Has this problem been reported in Jira already? I can't find it in open issues, and it's still not solved as for version 8.1
I have found another reported bug (http://www.jetbrains.net/jira/browse/IDEA-20553) which seems to be caused by the same problem.

If it's hasn't been reported, I'll do it.

This is a serious stopper for me, as TDD is the way I work and not being able to run all tests without maven, slows me down.
And makes me want to go back to eclipse, actually.

0
Comment actions Permalink

I dropped the ball on reporting this as a Jira issue because our Spring expert developed a work around for us in one project. It has now cropped up in another project, so I will try to file the report.

0
Comment actions Permalink

I created

IDEA-24680

Problems running unit tests in a large mutli module project with Spring and Maven


And issue describing this problem.

0

Please sign in to leave a comment.