Pages

Friday, May 19, 2017

Cannot nest 'projectname/src/main/java' inside 'projectname/src'. To enable the nesting exclude 'main/' from 'projectname/src' in eclipse maven project

I noticed following error in my project in eclipse.
       
Cannot nest 'projectname/src/main/java' inside 'projectname/src'. To enable the nesting exclude 'main/' from 'projectname/src'

due to this error also my test of junit was not working. and problem was due to reason that i changed eclipse java project into maven project and in the pom file eclipse added
 <sourceDirectory>src</sourceDirectory>
 and due to this maven plugin was giving error after removing this line plugin error gone and also junit test were fine.


Wednesday, May 17, 2017

junit unable to find junit test annotation class gives compilation error

In Eclipse I noticed sometimes for maven projects  (mostly i assume if eclipse project converted in maven project nature)  if you notice compilation error "package org.junit does not exist" that means he is unable to find annotation class while doing compilation.
following could be the probable errors.

1. in your pom file you did not add the dependency of junit  add it as
       

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

 

2. although above dependency is there it works fine if you remove scope which is not the right solution as your test class then gets added to your final jar or war package.




sol1: one solution that i found,  probably it could be issue with eclipse. that in such a case I make sure that maven dependency is shown in the package explorer of eclipse. for this  I do following
on project from project explorer right click select menu "Add Libraries..." there you select "Maven managed Dependencies" click ok and you will see dependencies are added properly by eclipse and in dependencies you see junit library is refereed

now it will compile and run your test.

sol2: another solution i noticed that when eclipse java project converted in to maven project specially if you assume everything right still it gives error even after trying sol1 above, then please look into pom file and from your build remove "<sourceDirectory>src</sourceDirectory>" this as it causes problem to maven plugin to read test folder properly, after removed it worked properly

I also notice my bad but sometimes we forget that junit needs class name also should have Test key word prefix or post-fix otherwise it gives error test run 0

hope this helps someone.!!