获取 java.lang.IllegalStateException:无法加载 CacheAwareContextLoaderDelegate 异常,而 运行 Cucumber 使用 Springs 进行测试

Getting java.lang.IllegalStateException: Could not load CacheAwareContextLoaderDelegate exception while running Cucumber tests with Springs

我正在尝试将 cucumebr 测试与 Spring 集成。我在包 actions 下创建了名为 MyClass 的 class,带有 Spring 注释 @Component

package actions;
import org.springframework.stereotype.Component;

@Component
public class MyClass{
public void printSomething(){
    System.out.println("Print this);
}

已创建 AppConfig class,注释 Spring @ComponentScan。并将上面创建的包 (actions) 传递给 @ComponentScan

import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = {"actions"})
public class AppConfig {

}

在我的 gluecode 中,我使用 @Autowired

初始化了 MyClass
@ContextConfiguration(classes=AppConfig.class)
public StepDefinitions implements En{

    @Autowired private MyClass myClass;

    public StepDefinitions(){   
        Before(() -> {
            myClass.printSomething();           
        });

下面是我的黄瓜测试运行enr class

import org.springframework.test.context.ContextConfiguration;

@RunWith(Cucumber.class)
@CucumberOptions(
              features = "myfeature.feature"
              ,glue={"stepDefinitions"}
              )
@ContextConfiguration(classes=AppConfig.class)
public class TestRunner{

}

当我运行将TestRunner作为junit测试时,junit抛出以下错误。

java.lang.IllegalStateException: Could not load CacheAwareContextLoaderDelegate [class org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
    at org.springframework.test.context.BootstrapUtils.createCacheAwareContextLoaderDelegate(BootstrapUtils.java:103)
    at org.springframework.test.context.BootstrapUtils.createBootstrapContext(BootstrapUtils.java:72)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:124)
    at cucumber.runtime.java.spring.CucumberTestContextManager.<init>(SpringFactory.java:206)
    at cucumber.runtime.java.spring.SpringFactory.start(SpringFactory.java:102)
    at cucumber.runtime.java.JavaBackend.buildWorld(JavaBackend.java:123)
    at cucumber.runtime.Runtime.buildBackendWorlds(Runtime.java:141)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:38)
    at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:102)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=16=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:95)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:38)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=16=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.api.junit.Cucumber.run(Cucumber.java:100)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)
Caused by: java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;
    at org.springframework.test.context.BootstrapUtils.createCacheAwareContextLoaderDelegate(BootstrapUtils.java:100)
    ... 32 more

并且 Eclipse 控制台在消息下方打印

[DEBUG] Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]

我正在使用 gradle 作为构建工具。以下是我添加的依赖项列表

compile group: 'org.springframework', name: 'spring', version: '2.5.6'
    compile group: 'org.springframework', name: 'spring-aop', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-aspects', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-beans', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-context', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-context-support', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-core', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-expression', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-instrument', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-instrument-tomcat', version: '4.3.18.RELEASE'
    compile group: 'org.springframework', name: 'spring-jdbc', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-jms', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-messaging', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-orm', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-oxm', version: '5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-test', version:'5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-tx', version:'5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-web', version:'5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version:'5.0.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc-portlet', version: '4.3.18.RELEASE'

谁能告诉我导致异常的问题。是否有任何 dependencies 我需要包含或者是否有任何 特定的 运行ner 我应该用来调用 cucumber-junit Cucumber.class

以外的弹簧

从 gradle 构建中删除不需要的依赖项为我解决了这个问题。代码没有错。

刚刚添加了以下依赖项并删除了所有依赖项。

compile group: 'org.springframework', name: 'spring-context', version: '5.0.7.RELEASE'
compile group: 'org.springframework', name: 'spring-core', version: '5.0.7.RELEASE'
compile group: 'org.springframework', name: 'spring-test', version:'5.0.7.RELEASE'
compile group: 'org.springframework', name: 'spring-tx', version:'5.0.7.RELEASE'