Injecting spring beans.xml for junit test case execution


My Junit test classes unable to sense beans.xml placed in the resource folder.

We are able to compile the Junit test sources properly, but while executing ‘test’ task its failed due to java.lang.NoClassDefFoundError even though both source and test class files are generated.

The test cases are failed which are dependent on beans.xml, others are passed

Below is the project structure :

myproject/src/main/java/ - source files
myproject/src/main/resources/ - included beans.xml
myproject/src/test/java/ - test source files
myproject/src/test/resources/ - included beans.xml

Can anyone help me on this.

Thanks in advance.

Take a closer look at the NoClassDefFoundError and I think you’ll find you’re missing a dependency from your runtime classpath.

Hi Lance,

Even though i have set runtime classpath for ‘test’ task in the dependencies, i am facing the same error (NoClassDefFoundError), while executing ‘test’ task.

Below are the code:

testCompile "junit:junit:4.12"
testRuntime files(‘src/main/resources/beans.xml’)
testRuntime fileTree(dir: ‘build/classes/main’, include: '**/.class’)*
testRuntime fileTree(dir: ‘build/classes/test’, include: '**/.class’)*

Can anyone guide me on this?

Thank in advance


Look at the exception message, it will tell you what class can’t be found. Once you know that you’ll know what’s missing from your classpath.

i am able to find the particular class in the classes directory, but its still shows NoClassDefFoundError.

Are you saying that the NoClassDefFoundError is for a class derived from src/main/java?

If you use the standard conventions of src/main/java, src/main/resources, src/test/java and src/test/resources there shouldn’t be any extra config required.

The following should correctly configure your test classpath.

apply plugin: 'java' 
test {
   compile "foo:bar:1.23"
   testCompile "junit:junit:4.12"

Please mention the root cause of the NoClassDefFoundError and also the location of the source of the missing class (relative to the project)