【问题标题】:Running cucumber-groovy features against a spring boot api针对 spring boot api 运行 cucumber-groovy 功能
【发布时间】:2016-04-01 00:36:11
【问题描述】:

我一直在尝试让 cucumber-groovy 与 spring-boot 一起工作,但进展并不顺利。我收到错误org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8080/applicants": Connection refused; nested exception is java.net.ConnectException: Connection refused,这似乎表明它正在到达端点,但服务没有运行。

我读到我需要一个cucumber.xml 文件,但我的项目没有使用任何 xml 配置,都是注释,所以我得到了这个:

package support
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.base.package")
public class CucumberConfiguration {}

我已将其添加到World,但这似乎是错误的处理方式(即我不知道如何在 groovy step defs 上添加注释)。

package support

import com.thing.app.Application
import org.junit.runner.RunWith
import org.springframework.boot.test.SpringApplicationContextLoader
import org.springframework.boot.test.WebIntegrationTest
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.test.context.web.WebAppConfiguration  
import static cucumber.api.groovy.Hooks.*

//@RunWith(SpringJUnit4ClassRunner)
//@ContextConfiguration(classes = Application, loader = SpringApplicationContextLoader)
//@WebAppConfiguration
//@WebIntegrationTest
@ContextConfiguration(classes = CucumberConfiguration)
public class AbstractTest {

}

World() {
    new AbstractTest()
}

Before() {}

After() {}

我在其他注释中留下了一些说明我到目前为止所做的事情。它都没有奏效。

我也尝试过设置一个 AbstractDefs 类,如此处https://github.com/jakehschwartz/spring-boot-cucumber-example/tree/master/src/test/java/demo 所见,但这也没有奏效,主要是因为我没有使用 cucumber-java 风格的东西,而是使用 cucumber-groovy 风格,它不使用步骤定义类。

编辑:刚刚发现我做错了env.groovy,我已经习惯了红宝石黄瓜,所以我很难找到所有的小问题。不过仍然有同样的问题,我不知道如何在 Spring 上下文中执行。

【问题讨论】:

    标签: spring groovy spring-boot cucumber spring-annotations


    【解决方案1】:

    您可以使用 io.cucumber.spring.SpringFactory 实例化 Spring 测试上下文并在 World 中注册适配器以允许 groovy 脚本访问 Spring bean:

    env.groovy:

    @ContextConfiguration(classes = TestConfiguration, loader = SpringBootContextLoader)
    class CucumberContextConfiguration {
    }
    
    //adapter bypassing World properties to Spring context    
    class SpringFactoryWorldAdapter {
        private final SpringFactory factory;
    
        SpringFactoryWorldAdapter(SpringFactory factory)  {
            this.factory = factory;
        }
    
        @Override
        Object getProperty(String s) {
            return factory.testContextManager.getContext().getBean(s);
        }
    }
    
    def factory; //Keep state to prevent repeated context initialization
    World { args ->
       if (factory == null) {
           factory = new SpringFactory()
           factory.addClass(CucumberContextConfiguration)
           factory.start()
       }
       new SpringFactoryWorldAdapter(factory)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多