Springboot/Cucumber 集成-如何在步骤定义中使用@Autowired(或其他SB 标签)class?
Springboot/Cucumber Integration-How to use @Autowired(or other SB tags) inside step definition class?
我正在尝试创建一个 Springboot、cucumber 和 Junit4 自动化框架。我使用的版本如下:
- Spring开机:2.1.3
- 黄瓜:io.cucumber 4.2.3
- Junit4: 4.12
- 操作系统:Win7 Pro。
我创建了一个道具 class,它试图从 属性 文件 (.yml)
中获取属性
道具Class:
@Data
@Component
public class PropsConfig {
@Value("${spring.skyewss}")
public String url;
}
步骤定义:
public class SkyeWssLoginStepDef implements En {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
private WebDriver driver;
private SkyeWssLoginPage loginPage;
private SkyeWssUtil skyeWssUtil;
@Autowired
private PropsConfig propsConfig;
public SkyeWssLoginStepDef() {
Given("^I open Skye WSS web page$", () -> {
driver = CukeHook.driver;
loginPage = new SkyeWssLoginPage(driver);
driver.get(propsConfig.getUrl());
skyeWssUtil = new SkyeWssUtil();
LOGGER.info("Current page is " + driver.getTitle());
});
}
......
}
黄瓜赛跑者class:
@RunWith(Cucumber.class)
@CucumberOptions(
features = {"src/test/resources/features"},
plugin = {"pretty", "html:target/cucumber-html-report"},
tags = {"@SkyeWss"}
)
@SpringBootTest
public class WssRegApplicationTests {
}
我尝试在 stepdef classes 上添加标签,但没有成功。
当我给 stepdef classes 标签如@Component 或@SrpingBootTest 时,我会得到错误。
cucumber.runtime.CucumberException: Glue class class
com.flexicards.wss_reg.skye.step.SkyeWssLoginStepDef and class
com.flexicards.wss_reg.skye.step.SkyeWssDashboardValStepDef both
attempt to configure the spring context. Please ensure only one glue
class configures the spring context
cucumber.runtime.CucumberException: Glue class com.flexicards.wss_reg.skye.step.SkyeWssDashboardValStepDef was
annotated with @Component; marking it as a candidate for
auto-detection by Spring. Glue classes are detected and registered by
Cucumber. Auto-detection of glue classes by spring may lead to
duplicate bean definitions. Please remove the @Component annotation
我是 Spring 和 Springboot 的新手,我很确定我在某处没有正确配置。大多数 springboot 和 cucumber 的例子都已经过时了。我已经试过了。就像创建一个由所有 stepdefs classes 扩展的抽象 classes 。这将给我与@SpringBootTest one.
相同的错误
谁能帮我解决这个问题?欢迎任何意见。非常感谢。
看起来你几乎做对了所有事情。唯一不合适的是上下文配置的位置。它必须位于具有步骤或挂钩定义的文件中。否则 Cucumber 不会检测到它。这应该可以解决问题:
@SpringBootTest
@AutoConfigureMockMvc
public class CucumberContextConfiguration {
@Before
public void setup_cucumber_spring_context(){
// Dummy method so cucumber will recognize this class as glue
// and use its context configuration.
}
}
您可以在 cucumber github repository 中找到 cucumber-spring
的工作示例。
也许还值得记住的是,Cucumber 将步骤定义实现为 spring bean,而不是像您预期的那样 post 处理单元测试 类。这意味着 @MockBean
、@SpyBean
和朋友将无法工作。
编辑:
使用 Cucumber v6.0.0,您可以省略虚拟方法,而是使用 @CucumberContextConfiguration
注释。
@SpringBootTest
@CucumberContextConfiguration
public class CucumberContextConfiguration {
}
我正在尝试创建一个 Springboot、cucumber 和 Junit4 自动化框架。我使用的版本如下:
- Spring开机:2.1.3
- 黄瓜:io.cucumber 4.2.3
- Junit4: 4.12
- 操作系统:Win7 Pro。
我创建了一个道具 class,它试图从 属性 文件 (.yml)
中获取属性道具Class:
@Data
@Component
public class PropsConfig {
@Value("${spring.skyewss}")
public String url;
}
步骤定义:
public class SkyeWssLoginStepDef implements En {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
private WebDriver driver;
private SkyeWssLoginPage loginPage;
private SkyeWssUtil skyeWssUtil;
@Autowired
private PropsConfig propsConfig;
public SkyeWssLoginStepDef() {
Given("^I open Skye WSS web page$", () -> {
driver = CukeHook.driver;
loginPage = new SkyeWssLoginPage(driver);
driver.get(propsConfig.getUrl());
skyeWssUtil = new SkyeWssUtil();
LOGGER.info("Current page is " + driver.getTitle());
});
}
......
}
黄瓜赛跑者class:
@RunWith(Cucumber.class)
@CucumberOptions(
features = {"src/test/resources/features"},
plugin = {"pretty", "html:target/cucumber-html-report"},
tags = {"@SkyeWss"}
)
@SpringBootTest
public class WssRegApplicationTests {
}
我尝试在 stepdef classes 上添加标签,但没有成功。 当我给 stepdef classes 标签如@Component 或@SrpingBootTest 时,我会得到错误。
cucumber.runtime.CucumberException: Glue class class com.flexicards.wss_reg.skye.step.SkyeWssLoginStepDef and class com.flexicards.wss_reg.skye.step.SkyeWssDashboardValStepDef both attempt to configure the spring context. Please ensure only one glue class configures the spring context
cucumber.runtime.CucumberException: Glue class com.flexicards.wss_reg.skye.step.SkyeWssDashboardValStepDef was annotated with @Component; marking it as a candidate for auto-detection by Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by spring may lead to duplicate bean definitions. Please remove the @Component annotation
我是 Spring 和 Springboot 的新手,我很确定我在某处没有正确配置。大多数 springboot 和 cucumber 的例子都已经过时了。我已经试过了。就像创建一个由所有 stepdefs classes 扩展的抽象 classes 。这将给我与@SpringBootTest one.
相同的错误谁能帮我解决这个问题?欢迎任何意见。非常感谢。
看起来你几乎做对了所有事情。唯一不合适的是上下文配置的位置。它必须位于具有步骤或挂钩定义的文件中。否则 Cucumber 不会检测到它。这应该可以解决问题:
@SpringBootTest
@AutoConfigureMockMvc
public class CucumberContextConfiguration {
@Before
public void setup_cucumber_spring_context(){
// Dummy method so cucumber will recognize this class as glue
// and use its context configuration.
}
}
您可以在 cucumber github repository 中找到 cucumber-spring
的工作示例。
也许还值得记住的是,Cucumber 将步骤定义实现为 spring bean,而不是像您预期的那样 post 处理单元测试 类。这意味着 @MockBean
、@SpyBean
和朋友将无法工作。
编辑:
使用 Cucumber v6.0.0,您可以省略虚拟方法,而是使用 @CucumberContextConfiguration
注释。
@SpringBootTest
@CucumberContextConfiguration
public class CucumberContextConfiguration {
}