Search code examples
springgradlejava-8cucumberjunit4

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


I'm trying to integrate cucumebr tests with Springs. I have created class named MyClass under package actions with Spring annotation @Component

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

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

Created AppConfig class with Spring Annotation @ComponentScan. And passed package created above (actions) into@ComponentScan

import org.springframework.context.annotation.ComponentScan;

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

}

In my gluecode, i initialised MyClass Using @Autowired

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

    @Autowired private MyClass myClass;

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

Below is my cucumber test runenr class

import org.springframework.test.context.ContextConfiguration;

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

}

When i run the TestRunner as junit Test, junit throwing following error.

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

and eclipse console printing below message

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

I'm using gradle as build tool. Following are the list of dependencies i added

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'

Could anyone tell me the issue causing the exception. Are there any dependencies i need to include or is there any specific runner i should use to invoke cucumber-junit in springs other than Cucumber.class


Solution

  • Removing unwanted dependencies from gradle build resolved the issue for me. Nothing wrong in the code.

    Just added below dependencies and removed all.

    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'