asgreflexx / casp-modulith

1 stars 1 forks source link

Cucumber Tests #20

Open lacribeiro11 opened 4 days ago

lacribeiro11 commented 4 days ago

Create integration tests for and based:

Creating Cucumber tests with @SpringBootTest can help you write integration tests for your Spring Boot application. Here’s a basic example to get you started:

First, ensure you have the necessary dependencies in your pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-spring</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Next, create your feature file in src/test/resources/features. For example, example.feature:

Feature: Example feature

  Scenario: Example scenario
    Given the application is running
    When I perform an action
    Then I expect a result

Now, create a test runner class to execute the Cucumber tests. For example, CucumberTest.java in src/test/java/com/example:

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

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features", plugin = {"pretty"})
public class CucumberTest {
}

Create step definitions to map the Gherkin steps to Java methods. For example, StepDefinitions.java in src/test/java/com/example:

import org.springframework.boot.test.context.SpringBootTest;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import static org.junit.Assert.*;

@SpringBootTest
public class StepDefinitions {

    @Given("the application is running")
    public void the_application_is_running() {
        // Initialization or setup code
    }

    @When("I perform an action")
    public void i_perform_an_action() {
        // Action code
    }

    @Then("I expect a result")
    public void i_expect_a_result() {
        // Assertion code
        assertTrue(true);
    }
}

This is a basic structure to get you started. You can expand upon it by adding more detailed steps, scenarios, and integrating with your actual application logic.

lacribeiro11 commented 1 day ago

ScenarioScoped

In Cucumber, the @ScenarioScoped annotation is used to define objects whose lifespan is bound to the lifespan of a single scenario. Scenario scope ensures that each scenario runs in isolation with its own set of dependencies and shared states, which are reset at the beginning of each new scenario. This is especially useful for maintaining state between steps in a scenario and ensuring that one scenario's state does not interfere with another.