Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Jpa - scans for files in tests folder

I'm using Spring Data JPA and it's strange but it tries to scan (during deploy) test files, which causes an error:

java.lang.ClassNotFoundException: org.junit.runner.RunWith
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
...
WARN : org.springframework.core.type.classreading.AnnotationAttributesReadingVisitor - Failed to classload      type while reading annotation metadata. This is a non-fatal error, but certain annotation metadata may be     unavailable.

The classnotfoundexception is because test dependencies are in "test" scope in maven.

But my question is, why it is scanning test folder? (/src/main/test).

The context configuration is as follows:

 <jpa:repositories base-package="com.domain.repository" />

Can you tell me what am I doing wrong? My .classpath content is as follows:

<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
...

I really appreciate your help with this.

like image 255
Michal Borek Avatar asked Apr 29 '12 19:04

Michal Borek


1 Answers

But my question is, why it is scanning test folder? (/src/main/test).

The context configuration is as follows:

<jpa:repositories base-package="com.domain.repository" />

To be precise, I don't think Spring is scanning this folder - it is your source folder after all. It's only looking at the deployed package structure and my suspicion would be that the cause of the problem has something to do with that.

I encountered the same problem but it was caused by an erroneous deployment configuration in Eclipse rather than something specific to Spring. I had Eclipse configured to deploy my test classes in src/test/java to the target deployment package so all the test files (that of course had corresponding package structures) where present when I started the server. However, judging by the .classpath snippet you've posted, I see you are explicitly excluding them from being deployed.

Nevertheless, there are other ways that class files can creep into your deployment. Have you been able to verify that the test classes are in fact not present in the deployed package? Could they be being synched there by some other process that you've set up (FileSynch plugin maybe)?

Also, do you have any other Spring context files with configurations to scan other base packages?

You might add some more details about your Eclipse configuration and how you're deploying to your test server to help solve the problem.

like image 170
chrisjleu Avatar answered Sep 29 '22 10:09

chrisjleu