8

I am using Spring 3.2 with Java based configuration and have some problems with my unit tests (JUnit 4.8.1). So this is a test runner:

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(classes={TestConfig.class})
 public class ManualTest
 {
     @Autowired
     ...

Howeever, I am receiving this error:

Caused by: java.lang.IllegalStateException: CGLIB is required to process @Configuration classes. Either add CGLIB to the classpath or remove the following @Configuration bean definitions: [testConfig]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:327)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:222)

As the Spring blog states, Spring 3.2 is inlining CGLIB 3. So why do I receive this error?

I am using Gradle 1.3 as build management tool and STS as IDE. When calling gradle eclipse gradle pulls in the dependencies twice: one time as plain jar and one time as library:

First as plain jar: plain jar

and than as library:

library

In the plain jar section I had still Spring 3.1 configured, while in the library section there was Spring 3.2. So I removed the plain jars and everything was working.

This is my project build.gradle

configurations
{
    driver
}

dependencies
{
    driver 'com.oracle:ojdbc6:11.2.0'

    compile "org.springframework:spring-jdbc:$springVersion"

    testCompile 'com.oracle:ojdbc6:11.2.0'
    testCompile "org.springframework:spring-test:$springVersion"
    testCompile "commons-dbcp:commons-dbcp:$dbcpVersion"
    testCompile "junit:junit:$junitVersion"
    testCompile "org.slf4j:slf4j-log4j12:$slf4jVersion"
}

sourceSets 
{
    main
    {
        java
        {
            srcDirs 'src/main/java', "$buildDir/generated-sources/"
        }
    }
}

And the build.gradle from the master project

configure(allprojects)
{
    ext.dbcpVersion = '1.4'
    ext.springVersion  = '3.2.0.RELEASE'
    ext.junitVersion  = '4.8.1'
    ext.slf4jVersion = '1.7.2' 
}

subprojects
{
    // Artifact settings
    group = 'xxx'
    version = '1.0-SNAPSHOT'

    // Standard plugins
    apply plugin: 'java'
    apply plugin: 'eclipse'

    // Repositories
    repositories
    {
        mavenLocal()
        maven
        {
            url "http://repo.springsource.org/release"
        }
        mavenCentral()
    }

    // Standard dependencies
    dependencies
    {
    }
}
ChrLipp
  • 15,526
  • 10
  • 75
  • 107
  • Can you post your classpath content? or pom.xml if using maven? – ElderMael Dec 17 '12 at 16:22
  • Does it fail in both Eclipse and on the gradle command line? – artbristol Dec 18 '12 at 10:52
  • Can you post your gradle build file? – Hiery Nomus Dec 18 '12 at 12:43
  • What happens when you run `gradle cleanEclipse eclipse`? Does the problem then still occur? Else run a `gradle dependencies` to determine where the wrong dependency comes from. – Hiery Nomus Dec 19 '12 at 10:04
  • When running `gradle cleanEclipse eclipse`, the subproject lost property 'Gradle project', so I lost all the Gradle and Groovy dependencies. It seems to me that the eclipse support in Gradle is just not that stable. What I don't know is how to bring back Gradle support. It is not a nature. – ChrLipp Dec 20 '12 at 08:05

2 Answers2

2

I deleted all Eclipse projects and settings and all Gradle temporary files. Then I tried to import the project in Eclipse (Import Gradle project..). This failed with an exception. Then I deleted the Gradle settings within the Eclipse project and after that the import worked.

So I will not use gradle eclipse with version 1.3.

Also the additional source set path did not make its way into the Eclipse project as source path.

ChrLipp
  • 15,526
  • 10
  • 75
  • 107
1

I had the same issue. Just add this dependency to your pom.xml file:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

And your unit tests and runtime code should work properly without cglib errors.

Javawerks
  • 51
  • 1
  • I migrated a project from Maven to Gradle and ,surprisingly, the used version of the spring-context dependency was a different one: 3.1.x instead of the expected 4.x. This explained the exception because Spring inlines CGLib since 3.2 but not before. After adding the abovementioned dependency the error disappeared. That's a pity that gradle does not support Maven's import scope yet. Otherwise one could use BOM (bill of materials) as well. – rwitzel Mar 28 '15 at 20:56