Cucumber testing java example?

2020-06-28 16:15发布

I want to try a sample application with cucumber testing,cucumber testing can be done with only ruby or also can be done with java? please help me with a sample example.

thank you.

5条回答
一纸荒年 Trace。
2楼-- · 2020-06-28 16:39

This one is from Cucumber-jvm project https://github.com/cucumber/cucumber-jvm/tree/master/examples. Cucumber-JVM is a Java port of cucumber. Configuration info could be found on project page https://github.com/cucumber/cucumber-jvm.

查看更多
神经病院院长
3楼-- · 2020-06-28 16:49

I'm kind of late to this question but I think It worth a clearer answer after a long time this question was asked because there are lots of good resources to get start with cucumber right now. The best place to get start with cucumber in 10 minutes is the following link:

https://cucumber.io/docs/guides/10-minute-tutorial/

And as mentioned in the link you can create sample project with maven archetype:

mvn archetype:generate                      \
   "-DarchetypeGroupId=io.cucumber"           \
   "-DarchetypeArtifactId=cucumber-archetype" \
   "-DarchetypeVersion=5.6.0"               \
   "-DgroupId=hellocucumber"                  \
   "-DartifactId=hellocucumber"               \
   "-Dpackage=hellocucumber"                  \
   "-Dversion=1.0.0-SNAPSHOT"                 \
   "-DinteractiveMode=false"
查看更多
淡お忘
4楼-- · 2020-06-28 16:53

To get started, clone the java-skeleton project from the Cucumber team: https://github.com/cucumber/cucumber-java-skeleton

This is specifically intended as a getting started project, and should work out of the box. Once you have a working project, it is much easier to extend it to contain the things you need. You can build the project using Maven with:

mvn clean install

If you'd prefer to start from scratch, check the pom.xml for the dependencies you need (at least cucumber-java, probably also cucumber-junit and junit itself to run your tests).

Create a src/test/java folder where you will implement your step definitions Create a src/test/resources folder where you will create your .feature files

If you are using an IDE (like IntelliJ) there's probably a Cucumber plugin that you need.

查看更多
Melony?
5楼-- · 2020-06-28 16:58

You need ListItTest.java to test a particular integration test, then you need a feature file, defination file and a sample response json file.

ListItTest.java

@RunWith(Cucumber.class)
@CucumberOptions(
        glue = {"com.abcd.cucumber.steps"},
        features = {"classpath:features/member/member-document.feature"},
        plugin = {"pretty"}
)
public class ListItTest {

}

member-password.feature

# Change password of member
# Tags : Member's change password

Feature: Change password of member
  Scenario: Change password of a registered member
    When member enters oldPassword newPassword and confirmPassword
    Then member password will be changed

member-password.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "status": {
      "type": "boolean"
    },
    "sessionExpired": {
      "type": "boolean"
    },
    "messages": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "shortMessage": {
              "type": "string"
            },
            "longMessage": {
              "type": "string"
            }
          },
          "required": [
            "shortMessage",
            "longMessage"
          ]
        }
      ]
    }
  },
  "required": [
    "status",
    "sessionExpired",
    "messages"
  ]
}

MemberPasswordDef.java

public class MemberPasswordDef {
    private LoginModel loginModel = new LoginModel();
    private Logger logger = Logger.getLogger(MemberPasswordDef.class);
    PasswordChangeParams passwordChangeParams = new PasswordChangeParams();

@When("member enters oldPassword newPassword and confirmPassword")
public void member_enters_oldPassword_newPassword_and_confirmPassword() {
    passwordChangeParams.setOldPassword("1234");
    passwordChangeParams.setNewPassword("1212");
    passwordChangeParams.setConfirmPassword("1212");
    loginModel.setUsername("test");
    loginModel.setPassword("9999");
}

@Then("member password will be changed")
public void member_password_will_be_changed() {
    try {
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(passwordChangeParams);
        RestAssured.given()
                .header(new Header("content-type", "application/json"))
                .auth().preemptive().basic(loginModel.getUsername(), loginModel.getPassword())
                .when()
                .body(jsonString)
                .put(URL)
                .then()
                .assertThat()
                .body(matchesJsonSchemaInClasspath("schemas/members/member-password.json"));


    }
    catch(Exception e) {
        Assert.assertEquals(false, true);
        logger.error(e.getMessage(), e);
    }
   }
 }
查看更多
▲ chillily
6楼-- · 2020-06-28 17:04

You can check out a simple example we wrote at our place to demonstrate cucumber-jvm : https://github.com/gphilipp/mowitnow-tdd

查看更多
登录 后发表回答