繁体   English   中英

运行黄瓜测试时如何启动Spring @Autowired实例?

[英]How do i initiate Spring @Autowired instances when running a Cucumber test?

在调试StepDefinitions.java时可以启动Spring,但是从gradle运行测试会产生null 我需要其他胶水吗?

产生空值: gradle黄瓜

产生null:运行myFeature.features

产生myService(工作):运行Stepdefinitions.java

我尝试了以下代码:

@ContextConfiguration(
        classes = Application.class,
        loader = SpringBootContextLoader.class)
@WebAppConfiguration

当前步骤定义:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class StepDefinitions{
    private static String roll;

   @Autowired
   MyService myService;


    /** Reset all static values before running test cases    */
    @BeforeClass
    public static void resetValues() {
        roll = "";
    }

    //I use swedish, "Given"
    @Givet("Jag har rollen {string}")
    public void getRole(String roll) {
        assertNotNull(roll);
        this.roll = roll;
        myService.useRole(roll);
    }
}

gradle.build:

dependencies {
compile files('../libs/cucumber-spring-4.7.1.jar')
testCompile 'io.cucumber:cucumber-java:' + cucumberVersion
testCompile 'io.cucumber:cucumber-junit:' + cucumberVersion
...
}

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + 
sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'steps/rufs', 
'src/test/resources/features', '--tags','@rufs']
            }
    }
}

从Gradle运行时,您不会在任何地方参与JUnit。 JUnit使用@RunWith ,这反过来又促使Spring参与其中。 当Cucumber作为套件运行时,它会忽略那些注释,因为它无法理解它们。

您将需要使用JUnit作为您的套件(即,不要运行cucumber.api.cli.Main )。 然后,您就会遇到问题,因为您需要使用两个“运行器”:Cucumber和Spring。

解决这一问题的方法是JUnit为其中一位跑步者制定的“规则”。 春天有条规则,但据我所见,黄瓜没有。 在这种情况下,请使用黄瓜流道:

@RunWith(Cucumber.class)

结合Spring的规则,如下所述: 如何通过参数化运行JUnit SpringJUnit4ClassRunner?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM