使用Cucumber测试Spring启动应用程序

时间:2018-02-15 02:22:52

标签: java spring maven spring-boot cucumber

我的要求是使用BDD测试我的Spring启动应用程序。为此我选择了黄瓜。在寻找可以帮助我做到这一点的任何教程时,我遇到了很多,我最终会让自己感到困惑。以下是我的要求

  1. 我希望能够在命令行使用mvn test构建应用程序时测试我的应用程序

  2. 我也希望在构建服务器中执行上一步。

  3. 我的期望是,如果我只是运行mvn test,那么maven应该启动我的应用程序并逐步执行我的功能,但实际上它并没有启动我的应用程序,而是运行测试,我的应用程序正在运行,它试图使用这些服务。以下是我的黄瓜测试课程

    @RunWith(Cucumber.class)
    @CucumberOptions(features = "src/test/resources/features",
            glue="com.api.resources.steps",
            format = {
                    "pretty",
                    "html:target/cucumber-reports/cucumber-pretty",
                    "json:target/cucumber-reports/CucumberTestReport.json",
                    "rerun:target/cucumber-reports/rerun.txt"
            }
    )
    public class ApiTests {
    }
    

    我的FeatureStep类

    @TestConfigs
    public class FacebookAuthentication{
        public static final String HOST="http://localhost:8080";
        @Given("^User requests authentication by providing \"([^\"]*)\" and \"([^\"]*)\" obtained from facebook$")
        public void userRequestsAuthenticationByProvidingAndObtainedFromFacebook(String arg0, String arg1) throws Throwable {
            String token = "token";
            String id = "id";
            Map<String, String> map = new HashMap<>();
            map.put("token",token);
            map.put("id",id");
            String response = HttpUtilForTests.sendPostRequest(HOST+"/auth/facebook", map, "");
        }
    
        @When("^Facebook returns User details with \"([^\"]*)\" \"([^\"]*)\" and \"([^\"]*)\"$")
        public void facebookReturnsUserDetailsWithAnd(String arg0, String arg1, String arg2) throws Throwable {
    
        }
    
        @Then("^User should be authorized in pixyfi application and return User info with UserToken$")
        public void userShouldBeAuthorizedInPixyfiApplicationAndReturnUserInfoWithUserToken() throws Throwable {
            Assert.assertTrue(true);
        }
    }
    

    我的测试配置

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @ContextConfiguration
    @ActiveProfiles("test")
    @SpringBootTest(
            webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
            classes = GlobalTestonfig.class)
    public @interface TestConfigs {
    
    }
    
    @Configuration
    @Import(MyApplication.class)
    class GlobalTestonfig {
    
    }
    

    使用上述设置mvn test启动我的测试但在http://localhost:8080消耗服务失败,因为服务本身未启动。 你能不能帮我解决一下我错误的地方?

0 个答案:

没有答案
相关问题