【问题标题】:Getting java.lang.IllegalStateException: Could not load CacheAwareContextLoaderDelegate exception while running Cucumber tests with Springs获取 java.lang.IllegalStateException:使用 Springs 运行 Cucumber 测试时无法加载 CacheAwareContextLoaderDelegate 异常
【发布时间】:2018-07-06 11:29:48
【问题描述】:

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

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

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

使用 Spring Annotation @ComponentScan 创建了 AppConfig 类。并将上面创建的包(actions) 传递到@ComponentScan

import org.springframework.context.annotation.ComponentScan;

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

}

在我的胶水代码中,我使用@Autowired 初始化了MyClass

@ContextConfiguration(classes=AppConfig.class)
public StepDefinitions implements En{

    @Autowired private MyClass myClass;

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

下面是我的黄瓜测试runenr类

import org.springframework.test.context.ContextConfiguration;

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

}

当我将 TestRunner 作为 junit Test 运行时,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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.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'

谁能告诉我导致异常的问题。是否有任何依赖项我需要包含,或者是否有任何特定的跑步者我应该用来在Cucumber.class以外的弹簧中调用cucumber-junit

【问题讨论】:

  • 我尝试仅在 StepDefinitions.java 类和 TestRunenr.java 类中提供 @ContextConfiguration(classes=AppConfig.class) 并且还尝试在这两个类中提供。每次都得到相同的异常。

标签: spring gradle java-8 cucumber junit4


【解决方案1】:

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

刚刚添加了下面的依赖并删除了所有。

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'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    相关资源
    最近更新 更多