2012-12-17 53 views
8

我正在使用基於Java的配置的Spring 3.2,並且在單元測試(JUnit 4.8.1)中遇到了一些問題。所以這是一個測試運行:使用基於Java的配置的Spring 3.2單元測試

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

Howeever,我收到此錯誤:

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) 

隨着Spring blog狀態,彈簧3.2內嵌CGLIB 3.那麼,爲什麼我收到此錯誤?

我使用Gradle 1.3作爲構建管理工具,STS作爲IDE。當調用gradle eclipse gradle這個在依賴拉兩次:一次爲普通罐子和一個時間庫:

首先以純的jar: plain jar

比爲庫:

library

在普通的jar部分中,我仍然配置了Spring 3.1,而在庫部分中有Spring 3.2。所以我刪除了普通的罐子,一切正常。

這是我的項目的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/" 
     } 
    } 
} 

而且從主項目

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 
    { 
    } 
} 
+0

你可以發佈你的classpath內容嗎?或如果使用maven pom.xml? – ElderMael

+0

它在Eclipse和Gradle命令行中都失敗了嗎? – artbristol

+0

僅適用於Eclipse(STS)。 – ChrLipp

回答

2

我刪除了所有的Eclipse項目設置和所有搖籃臨時文件的build.gradle。然後我嘗試在Eclipse中導入項目(導入Gradle項目..)。這失敗了一個例外。然後我刪除了Eclipse項目中的Gradle設置,然後導入工作。

所以我不會在版本1.3中使用gradle eclipse

此外,額外的源集路徑並未作爲源路徑進入Eclipse項目。

1

我有同樣的問題。只需添加這依賴於你的pom.xml文件:

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

和單元測試和運行時代碼應該沒有錯誤CGLIB正常工作。

+0

我將一個項目從Maven遷移到了Gradle,令人驚訝的是,Spring-context依賴的使用版本是一個不同的版本:3.1.x而不是預期的4.x.這解釋了這個異常,因爲Spring自3.2開始就使用CGLib,但之前沒有。添加上述依賴關係後,錯誤消失。遺憾的是,Gradle不支持Maven的導入範圍。否則,人們也可以使用BOM(物料清單)。 – rwitzel

相關問題