3

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.

Arry
  • 1,630
  • 9
  • 27
  • 46

4 Answers4

3

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.

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
matthaeus
  • 797
  • 2
  • 7
  • 17
  • Interestingly, this seems to be exactly the problem I had. Eclipse automatically adds JUnit to the class path, but e2m's autogenerated POM does not include JUnit as a dependency. Minor side note to future readers to update the version. As of post time, JUnit is now version 4.11. – Kat May 28 '14 at 07:35
3

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

Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101
0

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.

e-sushi
  • 13,786
  • 10
  • 38
  • 57
Parag
  • 79
  • 7
0

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.

killjoy
  • 940
  • 1
  • 11
  • 16