【问题标题】:Autowire failed in cucumber Step Def testAutowire 在黄瓜 Step Def 测试中失败
【发布时间】:2019-04-29 23:47:26
【问题描述】:

我有一系列黄瓜功能文件和当前项目中相关步骤定义测试的列表

在 step def 测试包中,我有这个 Hook 定义

@ContextConfiguration(classes = Application.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Hooks {
    ....
}

和 RunCukesTest

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features", glue = { "com.myapp.test.jersey.rest.v1" })
@ContextConfiguration(classes = Application.class)
public class RunCukesTest {
    ....
}

上面的类路径是正确的。 并且有一个step def test

package com.myapp.test.jersey.rest.v1;

....
@ContextConfiguration(classes = Application.class)
public class OrderCreateServiceTest {
    ....
    @Autowired
    private OrderRepository repository;
}

但是我得到了 Spring Boot 的跟随错误

Exception in thread "main" java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?

那么如果我从 step def 类中取出@ContextConfiguration,像这样

//@ContextConfiguration(classes = Application.class)
public class CashpointCreateServiceTest {

显然,存储库对象的自动装配将通过抛出 NullPointerException 失败

如果有人能分享,不胜感激 (1) 使用HookCukeTest 配置,如何在step def 类中自动装配bean? (2) @ContextConfigurationHookCukeTest 类都可以吗?

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    您不需要 RunCukesTest 类上的 ContextConfiguration。对于运行黄瓜测试,我使用以下设置。

    Junit 测试类启动黄瓜测试:

    package mypackage.test;
    
    import org.junit.runner.RunWith;
    
    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;
    
    @RunWith(Cucumber.class)
    @CucumberOptions(features = "src/test-integration/resources/features")
    public class RunFeatures {
    
    }
    

    所有步骤定义类扩展的基类。这样所有的 step 类都具有相同的 spring 注释。

    package mypackage.test.steps;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
    import org.springframework.test.context.ContextConfiguration;
    
    import mypackage.Application;
    
    @ContextConfiguration(classes = { Application.class })
    @SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT)
    public class BaseSteps {
    
    }
    

    然后是测试步骤

    package mypackage.test.steps;
    
    import mypackage.repo.SampleRepo;
    import org.springframework.beans.factory.annotation.Autowired;
    import cucumber.api.java.en.When;
    
    public class SampleSteps extends BaseSteps {
    
        @Autowired
        private SampleRepo sampleRepo;
    
        @When("^Sample repo is called$")
        public void no_words_are_saved() {
             sampleRepo.findAll("something");
        }
    

    我不明白你的 Hooks 课程是干什么用的。我只在 cucumber-jvm 中使用了带注释的钩子(例如 @Before 或 @After)。如果你需要一些其他的钩子,你能解释一下你想要做什么吗?

    【讨论】:

      猜你喜欢
      • 2016-06-25
      • 2021-03-20
      • 2017-09-23
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多