Cucumber4 Spring Boot 2自动连线实例为空

时间:2019-06-10 10:18:08

标签: java spring-boot cucumber autowired cucumber-serenity

当我尝试运行黄瓜测试时,该测试正在运行,但步骤定义类中的自动装配字段为空

我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
........
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>4.2.0</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>4.2.0</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-spring -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>4.2.0</version>
            <scope>test</scope>
        </dependency>
......
<dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-core</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-junit</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-screenplay</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-screenplay-webdriver</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>net.serenity-bdd</groupId> -->
<!--            <artifactId>serenity-cucumber</artifactId> -->
<!--            <version>${serenity.cucumber.version}</version> -->
<!--            <scope>test</scope> -->
<!--        </dependency> -->
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-cucumber4</artifactId>
            <version>1.0.4</version>
            <scope>test</scope>
        </dependency>
.........
</project>

我的黄瓜班级

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(glue = "fr.mycompany.cucumber.scenarios.subscriber", features = { "src/test/resources/cucumber/scenarios/subscriber" })
public class CucumberStepDefinitions {

}

我的黄瓜步骤定义类

public class SubscriberSteps extends IntegrationTest {

    @Autowired
    private SubscriberBean subscriberBean;

    @Autowired
    private SubscriberService subscriberService;

    @When("^The user make a susbscription$")
    public void The_user_make_a_susbscription() {
        System.out.println("Run");
        SubscriptionData data = SubscriptionDataBuilder.build();
        Subscriber subscriber = subscriberService.subscription(data);
        assertThat(subscriber).overridingErrorMessage("Subscription 
           failed").isNotNull();
        subscriberBean.setSubscriber(subscriber);

    }
........
}

IntegrationTest类

@SpringBootTest(classes = SpringBootCucumberConfig.class)
public class IntegrationTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private GenericService genericService;

    @Test
    public void test() {

    }
........
}

我的Spring Boot应用程序类

@SpringBootApplication(scanBasePackages = { "fr.mycompany.cucumber" })
public class SpringBootCucumberConfig {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootCucumberConfig.class, args);
    }

}

服务类别

@Service
public class SubscriberService {
    @Autowired
    private SubscriberRepository subscriberRepository;
.......
}

当我运行场景时,测试正在运行,但是在调试时在订阅步骤“用户怀疑”中,我看到自动连接的字段SubscriberBean和SubscriberService为null,但是当我运行junit测试时, 不是这两个字段,而是所有自动连接的字段都为空

0 个答案:

没有答案