Skip to content

singhalmynk/rest-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

End-to-End Testing of a Spring Boot Java Project with Cucumber

How I set up Cucumber and wrote a simple test scenario and step definition for the greeting service:

Step 1: Added Cucumber Dependencies For Maven, added the following dependencies to your pom.xml: io.cucumber cucumber-java 6.10.4 test io.cucumber cucumber-junit 6.10.4 test

Step 2: Wrote a Cucumber Test Scenario Writing test scenarios in Gherkin language involves creating feature files that describe the expected behavior of your application. Each feature file contains one or more scenarios, and each scenario is a list of steps. The steps are written in a natural language format that describes an action and an expected outcome. Here's a basic structure of a Gherkin feature file:

Feature: Title of your feature As a user type I want to perform an action So that I can achieve a result

Scenario: Title of your scenario Given some initial context When an event occurs Then ensure some outcomes

• Feature describes a feature of your application. • Scenario describes a specific situation or use case of the feature. • Given describes the initial context of the scenario. • When describes an event or action. • Then describes the expected outcome or result. • And and But can be used to add additional conditions or outcomes.

Create a new file greeting.feature in the src/test/resources directory: Feature: Greeting service Scenario: Get greeting Given the greeting service is running When I request a greeting Then I receive a greeting response

Here's feature file for a login feature: Feature: Login As a user I want to log in to the application So that I can access my account

Scenario: Successful login Given I am on the login page When I enter valid credentials And I click the login button Then I should be redirected to my account page

Scenario: Unsuccessful login with invalid password Given I am on the login page When I enter an invalid password And I click the login button Then I should see an error message

Each Given, When, Then, And, and But step in the scenario corresponds to a method in your step definition classes. The methods contain the code to execute each step, such as setting up the initial context, performing actions, and checking outcomes.

Step 3: Implemented Step Definitions Created a new class GreetingStepDefinitions in the src/test/java directory: import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; import static org.junit.Assert.*;

public class GreetingStepDefinitions {

private String greeting;

@Given("the greeting service is running")
public void theGreetingServiceIsRunning() {
    // This would usually involve starting the service or setting up a mock service
}

@When("I request a greeting")
public void iRequestAGreeting() {
    // This would usually involve making a HTTP request to the service
    // For simplicity, we'll just set a static greeting
    this.greeting = "Hello, World!";
}

@Then("I receive a greeting response")
public void iReceiveAGreetingResponse() {
    assertEquals("Hello, World!", this.greeting);
}

}

public class LoginStepDefinitions {

@Given("I am on the login page")
public void iAmOnTheLoginPage() {
    // code to navigate to the login page
}

@When("I enter valid credentials")
public void iEnterValidCredentials() {
    // code to enter valid credentials
}

@And("I click the login button")
public void iClickTheLoginButton() {
    // code to click the login button
}

@Then("I should be redirected to my account page")
public void iShouldBeRedirectedToMyAccountPage() {
    // code to check that the user is redirected to their account page
}

}

Step 4: Configured Cucumber Options Created a new class CucumberTest in the src/test/java directory:

import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; import org.junit.runner.RunWith;

@RunWith(Cucumber.class) @CucumberOptions( features = "src/test/resources/features", glue = "com.example.stepdefinitions", plugin = {"pretty", "html:target/cucumber-reports.html", "json:target/cucumber-reports.json"}) public class CucumberTest { }

Step 5: Executed Tests and Generated Reports Ran the CucumberTest class to execute the Cucumber tests. This doesn't involve actual HTTP requests or starting the service. In a real-world scenario, you would use a library like RestAssured or Spring's TestRestTemplate to make HTTP requests to the service, and you might use Spring's testing support to start the service or set up a mock service.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published