如何在黄瓜中共享从class1步骤到class2步骤创建的数据?

时间:2018-09-16 19:34:18

标签: java cucumber

我正在尝试与在Class2中创建的step方法共享从Class1中的step方法创建的对象。让我们向您展示我的具体示例:

我的MainStep具有Main.feature:

 public class MainStep extends SpringTest {

    private ResponseEntity<String> responseEntity;

    @When("^the client calls (.*)$")
    public void theClientCalls(String path) {
        responseEntity = restTemplate.getForEntity(getServer() + path, String.class);
        CucumberCache.INSTANCE.putObject("resp", responseEntity);
    }

    @Then("^the client receives status code (\\d+)$")
    public void theClientReceivesStatusCode(int statusCode) {
        assertTrue(responseEntity.getStatusCode().value() == statusCode);
    }

    @Then("^the client receives body (.*)$")
    public void theClientReceives(String body) {
        assertTrue(responseEntity.getBody().equals(body));
    }
}


Feature: the version can be retrieved

  Scenario: client gets 200 and hello world
    When the client calls /main
    Then the client receives status code 200
    Then the client receives body hello world

  Scenario: client gets 200 and bye world
    When the client calls /second
    Then the client receives status code 200
    Then the client receives body bye world

现在让我们向您展示DbStep和Db.feature:

public class DbStep extends SpringTest {
    @Then("^the client verifies that the response is not null$")
    public void theClientVerifiesThatTheResponseIsNotNull() {
        CucumberCache.INSTANCE.getObject("resp").ifPresent(resp -> {
            assertNotNull(resp);
        });
    }
}

Feature: the version can be retrieved

  Scenario: client gets 200 and hello world
    When the client calls /db
    Then the client verifies that the response is not null
    Then the client receives status code 201
    Then the client receives body db status ok

所以这是我的问题:基本上,Db.feature方案执行与Main.feature方案几乎相同的事情,除了theClientVerifyThatTheResponseIsNotNull步骤。

因此,总而言之,我如何与DbSteps#theClientVerifyThatTheResponseIsNotNull共享在MainSteps#theClientCalls中创建的响应对象? (目前,就像您看到的那样,我正在使用单例映射,在其中写入并获取响应)。

亲切的问候,

0 个答案:

没有答案