本文介绍了spring boot整合Cucumber(BDD)的方法,分享给大家,具体如下:
1、新建一个springboot工程工程结构如下:
2、添加pom依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://.chhliu.service.UserInfoServiceI; import cucumber.api.java.zh_cn.假如; import cucumber.api.java.zh_cn.当; import cucumber.api.java.zh_cn.那么; public class Cucumber集成spring { @Resource(name="userInfoService") private UserInfoServiceI service; private String username; private String password; private String confirmPassword; @假如("^:我以\"([^\"]*)\"登录$") public void 我以_登录(String arg1) throws Throwable { this.username = arg1; } @假如("^:输入的密码为\"([^\"]*)\"$") public void 输入的密码为(String arg1) throws Throwable { this.password = arg1; } @当("^:确认密码也为\"([^\"]*)\"时$") public void 确认密码也为_时(String arg1) throws Throwable { this.confirmPassword = arg1; } @那么("^:显示银行卡余额为\"([^\"]*)\"$") public void 显示银行卡余额为(String arg1) throws Throwable { boolean isLogin = service.login(username, password, confirmPassword); if(isLogin){ System.out.println("登录成功!查询余额如下:"+arg1); Assert.assertEquals("500000", arg1); } } }
6、在测试步骤上添加注解支持
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration // 不加此注解,bean会注入不进去 @SpringBootTest // 不加此注解会找不到bean public class Cucumber集成spring{ }
7、测试结果
2 Scenarios (2 passed)
11 Steps (11 passed)
0m0.091s
8、整合注意点
spring boot与Cucumber整合的时候,有个地方需要注意,因为spring boot提倡去xml化,所以传统方式下,Cucumber会读取classpath下的cucumber.xml配置文件来初始化bean的方式,和spring整合后,就不能用这种方式了,需要使用@ContextConfiguration注解来实现类的加载,如果是需要加载配置文件的方式的话,可以如下使用:
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
如果使用注解的方式来整合的话,使用如下:
@ContextConfiguration(classes=SpringBootCucumberApplication.class)
或者直接
@ContextConfiguration
特别注意:@ContextConfiguration注解必加,否则会出现bean注入失败
下面,我们从源码来看下为什么会造成这种情况。
该部分涉及的代码都在cucumber-spring包下的SpringFactory类中,重点我们看下下面这个类:
public void start() {// cucumber测试启动方法 if (stepClassWithSpringContext != null) {// 如果使用了@ContextConfiguration注解的话,此处不为null testContextManager = new CucumberTestContextManager(stepClassWithSpringContext); } else {// 否则stepClassWithSpringContext就为null,会进入下面这个分支 if (beanFactory == null) { beanFactory = createFallbackContext();// 这个方法是我们要跟的重点 } } notifyContextManagerAboutTestClassStarted(); if (beanFactory == null || isNewContextCreated()) { beanFactory = testContextManager.getBeanFactory(); for (Class<?> stepClass : stepClasses) { registerStepClassBeanDefinition(beanFactory, stepClass); } } GlueCodeContext.INSTANCE.start(); }
我们在来跟下createFallbackContext方法:
private ConfigurableListableBeanFactory createFallbackContext() { ConfigurableApplicationContext applicationContext; if (getClass().getClassLoader().getResource("cucumber.xml") != null) {// 会先根据classpath下的cucumber.xml来初始化<span style="font-family:Arial, Helvetica, sans-serif;">ConfigurableApplicationContext</span> applicationContext = new ClassPathXmlApplicationContext("cucumber.xml"); } else {// 如果没有配置cucumber.xml的话,会new GenericApplicationContext applicationContext = new GenericApplicationContext(); } applicationContext.registerShutdownHook(); ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory(); beanFactory.registerScope(GlueCodeScope.NAME, new GlueCodeScope()); for (Class<?> stepClass : stepClasses) { registerStepClassBeanDefinition(beanFactory, stepClass); } return beanFactory; }
最后,来说下GenericApplicationContext这个类,该类会根据Bean的Type类型,然后newInstance实例,但是由于这个类中又注入了其他的类,而注入的类是无法通过new实例的方式来初始化的,所以最后就会注入失败,报空指针了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。