Tests failing in Maven install from command line but passing from Eclipse UI

12.2k Views Asked by At

I am using Maven in my Java project and the IDE is Eclipse. I have few test cases in my project, and when I am doing a Maven Install from eclipse then my test cases are passing, but when I am doing the Maven install from the command line, then all my test cases are failing. I have the following directory structure for my project: src/ main/ java/ resources/ test/ java/ resources/

Also, for my test cases, I have to use few configs from main/resources. I suspect that while running test cases from the command line (mvn clean install), it is not looking for the resources in main/resources and so is the error.

Can anyone please tell that how can I ask maven to look for the configs in main/resources also for my test cases? Also, if you suspect that the error is something else then please comment.

Thanks.

4

There are 4 best solutions below

0
On BEST ANSWER

If your tests are failing using maven command line then rest asure there is a problem with your test. You can't rely on eclipse, or rather m2Eclipse, for this because m2eclipse is not able to provide you with a correct classpath.

For instance, in eclipse you can refer from a class in src\main\java to a class in src\test\java, you won't get any compilation error. Of course, in maven (or an IDE with a decent maven integration like intelliJ) compilation will fail, as it should. Running tests in eclipse is fine (quicker/easier than maven command line) but the actual test you must perform (before committing to svn for instance) is to do a clean install with maven command line.

UPDATE
To address your question: src/main/resources is in the classpath when maven (surfire) runs the tests

0
On

I had a slightly different issue with same symptoms.

I had two files with same filename in src/main/resources and src/test/resources. The file from src/test/resources was incorrect, but was overwriting the newer file from src/main/resources.

The solution for me was to simply delete the file from src/test/resources.

1
On

I had the same problem, tests are all ok in Eclipse with JUnit, but fail in maven when I do mvn test in the console.

I solved this issue adding

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

to my pom.xml.

0
On

I solved this by disabling my firewall - Comodo - even though I told it not to sandbox surefire jar, damn thing was still blocking it. Disabled sandbox and everything was fine.