{"id":381,"date":"2023-06-06T16:33:22","date_gmt":"2023-06-06T09:33:22","guid":{"rendered":"https:\/\/thoaicoder.com\/?p=381"},"modified":"2024-03-07T01:29:07","modified_gmt":"2024-03-06T18:29:07","slug":"bdd-testing-with-cucumber","status":"publish","type":"post","link":"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/","title":{"rendered":"BDD Testing with Cucumber"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">1. BDD introduction<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">BDD test stands for <strong>behavior-driven development<\/strong> test. It is a testing methodology used in agile software development and is an extension of Test-Driven Development (TDD). BDD test emphasizes developing features based on a user story and writing code that provides a behavior that matches the desired outcome of the feature written in a natural language.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"432\" src=\"https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/07\/image-4.png\" alt=\"\" class=\"wp-image-507\" srcset=\"https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/07\/image-4.png 593w, https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/07\/image-4-300x219.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The result is a set of functional specifications that outline<strong> executable scenarios<\/strong> for the software. BDD tests are designed to close the gap between business people and technical people by focusing on the behaviors that matter most to the business requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A business requirement translated into a language such as&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Cucumber_(software)#Gherkin_language\" target=\"_blank\" rel=\"noreferrer noopener\">Gherkin<\/a>&nbsp;can be defined as:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Given&nbsp;<em>Precondition<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When&nbsp;<em>Action<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then&nbsp;<em>Result<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These features\/behaviors are mapped to test code that can execute and validate the behavior. In development, the practice is to define the scenario BEFORE writing any code. This ensures we\u2019re building the right function that the business needs. There are many tools to develop BDD for example Cucumber, Parasoft, Recorder, Selenic, and SOAtest.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">2. Cucumber overview<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Cucumber is a tool that supports BDD with Gherkin language. With Cucumber, software behavior can be defined in plain, understandable language, and test cases can be written to match the desired behavior. Essentially, Cucumber provides a way to write tests that are easy to understand, and it serves as a supporting tool for automated testing in a BDD workflow. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Cucumber is a nontechnical language that\u2019s easy to read and uses Given\/When\/Then statements to structure the behaviors of your software. Below is a sample Cucumber feature file about a login feature that is straightforward.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"669\" height=\"374\" src=\"https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/07\/image-5.png\" alt=\"\" class=\"wp-image-512\" srcset=\"https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/07\/image-5.png 669w, https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/07\/image-5-300x168.png 300w\" sizes=\"auto, (max-width: 669px) 100vw, 669px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/cucumber.io\/tools\/cucumberstudio\/\">CucumberStudio <\/a>is a collaborative testing platform in the cloud that allows the software delivery team (Product Owner, BA, QA, Developer, or whoever) to co-design Cucumber tests.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">3. Spring Boot Cucumber Tests Example<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">We will create an Email Service application by Spring Boot which contains some REST APIs to perform some operations as below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Send emails to contacts (or customers)<\/li>\n\n\n\n<li>Track email open event<\/li>\n\n\n\n<li>Get email send statistics (how many contacts were sent email successfully to, how many contacts opened email)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Then we will use Cucumber to create some BDD test cases to demonstrate how BDD helps us to develop software features that match with business requirements. You can check out this project <a href=\"https:\/\/github.com\/ThoaiNguyenHuu842\/bdd-cucumber-with-spring-boot\">over on Github<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.1.&nbsp;Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Docker<\/li>\n\n\n\n<li>Java 11<\/li>\n\n\n\n<li>Gradle<\/li>\n\n\n\n<li>Spring boot<\/li>\n\n\n\n<li>Flyway<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3.2.&nbsp;Database design<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The database of this project is simple:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>contact <\/strong>table to store the customer info, the <strong>unsubscribed <\/strong>column is a boolean that lets us know if a contact has already unsubscribed, so when sending an email to this contact, an ERROR status should be captured.<\/li>\n\n\n\n<li><strong>email <\/strong>table to store sent emails<\/li>\n\n\n\n<li><strong>email_contact <\/strong>is an association between email and contact to store which contacts an email was sent to which <strong>status <\/strong>column presents send email status which can be <em>ERROR<\/em>, <em>SENT,<\/em> or <em>OPENED<\/em><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"606\" height=\"279\" src=\"https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/08\/image.png\" alt=\"\" class=\"wp-image-519\" srcset=\"https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/08\/image.png 606w, https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/08\/image-300x138.png 300w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3.3. Integrate docker-compose to BDD tests<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To build up the entire application including the MySQL database via Docker compose we use <strong><a href=\"https:\/\/github.com\/avast\/gradle-docker-compose-plugin\">gradle-docker-compose-plugin<\/a><\/strong> which supports <strong>composeUp <\/strong>and <strong>composeDown <\/strong>gradle tasks. Then we can easily add the <strong>composeUp <\/strong>task before running tests and the <strong>composeDown <\/strong>after running tests using <strong>dependsOn <\/strong>and <strong>finalizedBy<\/strong>. The approach is useful and enables us to handle the tedious setup especially when running BDD tests on a CI environment such as Jenkin.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>build.gradle<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-tangaroa-background-color has-text-color has-background\" style=\"padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--40);padding-left:var(--wp--preset--spacing--40)\"><code>plugins {\n\tid 'org.springframework.boot' version '2.3.0.RELEASE'\n\tid 'io.spring.dependency-management' version '1.0.9.RELEASE'\n\tid 'java'\n\tid 'io.freefair.lombok' version '5.0.0'\n\tid 'com.avast.gradle.docker-compose' version '0.16.12'\n}\n\ngroup = 'nht.demo-cucumber'\nversion = '0.0.1-SNAPSHOT'\n\njava {\n\tsourceCompatibility = \"11\"\n\ttargetCompatibility = \"11\"\n}\n\nrepositories {\n\tmavenCentral()\n}\n\next {\n\tset('springCloudVersion', 'Hoxton.SR5')\n}\n\ndependencies {\n\timplementation 'org.flywaydb:flyway-core'\n\timplementation 'org.springframework.boot:spring-boot-starter-data-jpa'\n\timplementation 'mysql:mysql-connector-java'\n\timplementation 'org.springframework.boot:spring-boot-starter-web'\n\n\ttestImplementation 'io.cucumber:cucumber-java:7.13.0'\n\ttestImplementation 'io.cucumber:cucumber-spring:7.13.0'\n\ttestImplementation 'io.cucumber:cucumber-junit:7.13.0'\n\ttestImplementation 'io.cucumber:cucumber-core:7.13.0'\n\ttestImplementation 'org.springframework.boot:spring-boot-starter-test'\n}\n\ndockerCompose {\n\tuseComposeFiles = &#91;'docker\/docker-compose.yml']\n}\n\ntest {\n\tuseJUnitPlatform()\n\tdependsOn composeUp\n\tfinalizedBy composeDown\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3.3.&nbsp;Implement RESTful APIs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We will create some REST APIs to perform some operations as mentioned before:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Send email to contacts: POST \/emails\/send<\/li>\n\n\n\n<li>Track email opens: POST \/emails\/{emailId}\/contacts\/{contactId}\/open<\/li>\n\n\n\n<li>Get email send statistics: GET \/emails\/{emailId}\/report<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em>EmailController.java<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-tangaroa-background-color has-text-color has-background\" style=\"padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--40);padding-left:var(--wp--preset--spacing--40)\"><code>@RestController\n@RequiredArgsConstructor\n@Slf4j\npublic class EmailController extends BaseController {\n\n  private final EmailService emailService;\n\n\n  @PostMapping(\"\/emails\/send\")\n  public ResponseEntity sendEmail(@RequestBody SendEmailDto sendEmailDto) {\n    try {\n      emailService.sendEmail(sendEmailDto);\n      return ResponseEntity.ok().build();\n    }\n    catch (Exception ex) {\n      return handleInternalServerError(ex);\n    }\n  }\n\n  @PostMapping(\"\/emails\/{emailId}\/contacts\/{contactId}\/open\")\n  public ResponseEntity openEmail(@PathVariable Long emailId, @PathVariable  Long contactId) {\n    try {\n      emailService.openEmail(emailId,contactId);\n      return ResponseEntity.ok().build();\n    }\n    catch (Exception ex) {\n      return handleInternalServerError(ex);\n    }\n  }\n\n  @GetMapping(\"\/emails\/{emailId}\/report\")\n  public ResponseEntity getEmailReport(@PathVariable Long emailId) {\n    try {\n      return ResponseEntity.ok(emailService.getEmailReport(emailId));\n    }\n    catch (Exception ex) {\n      return handleInternalServerError(ex);\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3.4.&nbsp;Cucumber configuration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We can use @RunWith and @CucumberOptions to enable cucumber on our project and specify the directory containing future files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>CucumberIntegrationTest.java<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-tangaroa-background-color has-text-color has-background\" style=\"padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--40);padding-left:var(--wp--preset--spacing--40)\"><code>@RunWith(Cucumber.class)\n@CucumberOptions(features = \"src\/test\/resources\")\npublic class CucumberIntegrationTest {\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We also need to make Cucumber be able to load Spring Boot context and start our Spring Boot application via <strong>@CucumberContextConfiguration<\/strong> and <strong>@SpringBootTest<\/strong>. Finally, the most important thing is defining cucumber steps using Gherkin language which maps with a Java method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>SpringIntegrationTest.java<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-tangaroa-background-color has-text-color has-background\" style=\"padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--40);padding-left:var(--wp--preset--spacing--40)\"><code>@CucumberContextConfiguration\n@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)\npublic class SpringIntegrationTest {\n\n  @Autowired\n  private ContactRepository contactRepository;\n  @Autowired\n  private EmailRepository emailRepository;\n  @Autowired\n  private EmailContactRepository emailContactRepository;\n\n  @Autowired\n  private RestTemplate restTemplate;\n\n  private final String baseUrl = \"http:\/\/localhost:8080\";\n\n  @Before\n  public void initialization() {\n    emailContactRepository.deleteAll();\n    emailRepository.deleteAll();\n    contactRepository.deleteAll();\n  }\n\n  @Given(\"^the below contacts exists\")\n  public void createContact(DataTable table) {\n    List&lt;List&lt;String&gt;&gt; rows = table.asLists(String.class);\n    List&lt;Contact&gt; contacts = new ArrayList&lt;&gt;();\n\n    for (int i = 1; i &lt; rows.size(); i++) {\n      List&lt;String&gt; columns = rows.get(i);\n      contacts.add(Contact.builder()\n        .name(columns.get(0))\n        .email(columns.get(1))\n        .unsubscribed(Boolean.parseBoolean(columns.get(2)))\n        .build());\n    }\n    contactRepository.saveAll(contacts);\n  }\n\n  @When(\"^the email with title \\\"(&#91;^\\\"]*)\\\" and content \\\"(&#91;^\\\"]*)\\\" is sent to below emails\")\n  public void sendEmail(String title, String content, DataTable table) {\n    Set&lt;String&gt; emails = table.asLists(String.class).stream().map(columns -&gt; columns.get(0)).collect(\n      Collectors.toSet());\n    SendEmailDto sendEmailDto = SendEmailDto.builder()\n      .content(content)\n      .title(title)\n      .emails(emails)\n      .build();\n\n    HttpHeaders headers = new HttpHeaders();\n    headers.setContentType(MediaType.APPLICATION_JSON);\n    HttpEntity&lt;SendEmailDto&gt; request = new HttpEntity&lt;&gt;(sendEmailDto, headers);\n    restTemplate.postForObject(baseUrl + \"\/emails\/send\", request, Void.class);\n  }\n\n  @When(\"^contact \\\"(&#91;^\\\"]*)\\\" clicks an email with title \\\"(&#91;^\\\"]*)\\\"\")\n  public void openEmail(String email, String emailTitle) {\n    Long emailId = emailRepository.findByTitleIn(Set.of(emailTitle)).get(0).getId();\n    Long contactId = contactRepository.findByEmailIn(Set.of(email)).iterator().next().getId();\n    URI uri = UriComponentsBuilder.fromHttpUrl(baseUrl).path(\"\/emails\/{emailId}\/contacts\/{contactId}\/open\").buildAndExpand(emailId, contactId).toUri();\n    restTemplate.postForObject(uri, null, Void.class);\n  }\n\n  @Then(\"^the below report of email with title \\\"(&#91;^\\\"]*)\\\" exists\")\n  public void getReport(String emailTitle, DataTable table) {\n    Long emailId = emailRepository.findByTitleIn(Set.of(emailTitle)).get(0).getId();\n    URI uri = UriComponentsBuilder.fromHttpUrl(baseUrl).path(\"\/emails\/{emailId}\/report\").buildAndExpand(emailId).toUri();\n    EmailReportDto actualData = restTemplate.getForObject(uri, EmailReportDto.class);\n\n    EmailReportDto expectedData = new EmailReportDto();\n    List&lt;String&gt; columns = table.asLists(String.class).get(1);\n    expectedData.setTitle(columns.get(0));\n    expectedData.setContent(columns.get(1));\n    expectedData.setTotalContacts(Integer.parseInt(columns.get(2)));\n    expectedData.setTotalSent(Integer.parseInt(columns.get(3)));\n    expectedData.setTotalError(Integer.parseInt(columns.get(4)));\n    expectedData.setTotalOpened(Integer.parseInt(columns.get(5)));\n\n    Assertions.assertTrue(expectedData.compare(actualData));\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s dive deeper into each method inside the above class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>initialization<\/strong>: clear all data before running any test to make sure all of our test scenario is isolated from each other using Junit @Before<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>createContact<\/strong>: this method maps with Cucumber step @Given(&#8220;^the below contacts exists&#8221;) in order to set up contact data, note that the Datatable parameter is mapped with the table defined in Cucumber scenarios as below which is an amazing feature of Cucumber.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-tangaroa-background-color has-text-color has-background has-small-font-size\" style=\"padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--40);padding-left:var(--wp--preset--spacing--40)\"><code>Given the below contacts exists\n      | Name  | Email           |\n      | Thoai | thoai@gmail.com |\n      | Ti    | ti@gmail.com    |<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>sendEmail, openEmail, getReport<\/strong>: these methods map with our RESTful APIs defined above, we use regular expressions to map each API&#8217;s parameter such as email title, and email contact. Below is an example step which maps with <strong>sendEmail(String title, String content, DataTable table)<\/strong> method.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-tangaroa-background-color has-text-color has-background has-small-font-size\" style=\"padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--40);padding-left:var(--wp--preset--spacing--40)\"><code>When the email with title \"hello\" and content \"how are you\" is sent to below emails\n      | thoai@gmail.com |\n      | ti@gmail.com    |<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>getReport<\/strong> method maps with a &#8220;then&#8221; step which is the expected result of a test scenario, so we use Junit <strong>Assertions <\/strong>to verify the email report must be displayed as our expectation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.5.&nbsp;Write BDD tests<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we&#8217;ve enough configurations to integrate Cucumber into our application and it&#8217;s time to write BDD tests. Imagine the business requirement of our application is really simple, we only need to implement the sending email feature, tracking open email event feature, and email report feature. So we will create two test scenarios to cover this requirement:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Send an email to contacts:\n<ul class=\"wp-block-list\">\n<li><strong>Given<\/strong>: setup contact data<\/li>\n\n\n\n<li><strong>When<\/strong>: Send an email to two contacts with email is &#8220;thoai@gmail.com&#8221; or &#8220;ti@gmail.com&#8221;<\/li>\n\n\n\n<li><strong>Then<\/strong>: the expected result is that the email report must contain one row with &#8220;Total Contacts&#8221; = 2 and &#8220;Total Sent&#8221; = 2<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Send an email to contacts, then two contacts open an email\n<ul class=\"wp-block-list\">\n<li><strong>Given<\/strong>: Setup contact data<\/li>\n\n\n\n<li><strong>When<\/strong>: Send an email to two contacts with email is &#8220;thoai@gmail.com&#8221; or &#8220;ti@gmail.com&#8221;<\/li>\n\n\n\n<li><strong>When<\/strong>: Two contacts above click this email<\/li>\n\n\n\n<li><strong>Then<\/strong>: the expected result is that the email report must contain one row with &#8220;Total Contacts&#8221; = 2 and &#8220;Total Opened&#8221; = 2<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><em>send-email.feature<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-tangaroa-background-color has-text-color has-background has-small-font-size\" style=\"padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--40);padding-left:var(--wp--preset--spacing--40)\"><code>Feature: send email\n\n  Background:\n    Given the below contacts exists\n      | Name  | Email           |\n      | Thoai | thoai@gmail.com |\n      | Ti    | ti@gmail.com    |\n\n  Scenario: send an email to contacts\n    When the email with title \"hello\" and content \"how are you\" is sent to below emails\n      | thoai@gmail.com |\n      | ti@gmail.com    |\n    Then the below report of email with title \"hello\" exists\n      | Title | Content     | Total Contacts | Total Sent | Total Error | Total Opened |\n      | hello | how are you | 2              | 2          | 0           | 0            |\n\n  Scenario: send an email to contacts, then two contacts open email\n    When the email with title \"hello\" and content \"how are you\" is sent to below emails\n      | thoai@gmail.com |\n      | ti@gmail.com    |\n    And contact \"ti@gmail.com\" clicks an email with title \"hello\"\n    And contact \"thoai@gmail.com\" clicks an email with title \"hello\"\n    Then the below report of email with title \"hello\" exists\n      | Title | Content     | Total Contacts | Total Sent | Total Error | Total Opened |\n      | hello | how are you | 2              | 0          | 0           | 2            |<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On the feature above, we use &#8220;<strong>background<\/strong>&#8221; to define the setup contact step which is common to all the tests in the feature file. It allows us to add some context to the scenarios for a feature where it is defined.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine, that after MVP1, the business adds a new requirement: allow contacts to unsubscribe from email, and if we send an email to an unsubscribed contact, we will mark it as &#8220;Error&#8221; on the email report. A product owner guy (or BA, whoever works closely with the business side) will look at this feature file and update it to a new version as below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>send-email.feature<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-tangaroa-background-color has-text-color has-background has-small-font-size\" style=\"padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--40);padding-left:var(--wp--preset--spacing--40)\"><code>Feature: send email\n\n  Background:\n    Given the below contacts exists\n      | Name  | Email           | Unsubscribed |\n      | Thoai | thoai@gmail.com | false        |\n      | Ti    | ti@gmail.com    | false        |\n      | Teo   | teo@gmail.com   | true         |\n\n  Scenario: send an email to contacts\n    When the email with title \"hello\" and content \"how are you\" is sent to below emails\n      | thoai@gmail.com |\n      | ti@gmail.com    |\n    Then the below report of email with title \"hello\" exists\n      | Title | Content     | Total Contacts | Total Sent | Total Error | Total Opened |\n      | hello | how are you | 2              | 2          | 0           | 0            |\n\n  Scenario: send an email to contacts, then two contacts open email\n    When the email with title \"hello\" and content \"how are you\" is sent to below emails\n      | thoai@gmail.com |\n      | ti@gmail.com    |\n    And contact \"ti@gmail.com\" clicks an email with title \"hello\"\n    And contact \"thoai@gmail.com\" clicks an email with title \"hello\"\n    Then the below report of email with title \"hello\" exists\n      | Title | Content     | Total Contacts | Total Sent | Total Error | Total Opened |\n      | hello | how are you | 2              | 0          | 0           | 2            |\n\n  Scenario: send an email to unsubscribed contact\n    When the email with title \"hello\" and content \"how are you\" is sent to below emails\n      | thoai@gmail.com |\n      | teo@gmail.com   |\n    Then the below report of email with title \"hello\" exists\n      | Title | Content     | Total Contacts | Total Sent | Total Error | Total Opened |\n      | hello | how are you | 2              | 1          | 1           | 0            |<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The new version of the feature file adds a new Unsubscribed column into the contact table and a new scenario about sending email to an unsubscribed contact. This feature test file can&#8217;t be passed unless we add new code to implement the new requirement. This is how BDD works to ensure we\u2019re building the right function the business needs.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">4. Conclusion<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Within this article, we\u2019ve looked at the BDD concept as well as how BDD is applied to software development process. We&#8217;ve also dug into Cucumber and created a Spring Boot application to demonstrate how BDD is applied to reduce the gap between the technical side and the business side. This demo application can be found on&nbsp;<a href=\"https:\/\/github.com\/ThoaiNguyenHuu842\/bdd-cucumber-with-spring-boot\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. BDD introduction BDD test stands for behavior-driven development test. It is a testing methodology [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":560,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,15],"tags":[26,27,30,31,28,29],"class_list":["post-381","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-software-development-practices","tag-bdd","tag-cucumber","tag-docker","tag-gradle","tag-java","tag-wiremock"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.14 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>BDD Testing with Cucumber - I \u2764 Code<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"BDD Testing with Cucumber - I \u2764 Code\" \/>\n<meta property=\"og:description\" content=\"1. BDD introduction BDD test stands for behavior-driven development test. It is a testing methodology [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/\" \/>\n<meta property=\"og:site_name\" content=\"I \u2764 Code\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-06T09:33:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-06T18:29:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/06\/TDD-and-BDD.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"731\" \/>\n\t<meta property=\"og:image:height\" content=\"594\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Thoai\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Thoai\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/\"},\"author\":{\"name\":\"Thoai\",\"@id\":\"https:\/\/thoaicoder.com\/#\/schema\/person\/a3304c209434f63c501b05622d7d2720\"},\"headline\":\"BDD Testing with Cucumber\",\"datePublished\":\"2023-06-06T09:33:22+00:00\",\"dateModified\":\"2024-03-06T18:29:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/\"},\"wordCount\":1217,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/thoaicoder.com\/#\/schema\/person\/a3304c209434f63c501b05622d7d2720\"},\"keywords\":[\"BDD\",\"Cucumber\",\"Docker\",\"Gradle\",\"Java\",\"Wiremock\"],\"articleSection\":[\"Java\",\"Software Development Practices\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/\",\"url\":\"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/\",\"name\":\"BDD Testing with Cucumber - I \u2764 Code\",\"isPartOf\":{\"@id\":\"https:\/\/thoaicoder.com\/#website\"},\"datePublished\":\"2023-06-06T09:33:22+00:00\",\"dateModified\":\"2024-03-06T18:29:07+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/thoaicoder.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"BDD Testing with Cucumber\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/thoaicoder.com\/#website\",\"url\":\"https:\/\/thoaicoder.com\/\",\"name\":\"I \u2764 Code\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/thoaicoder.com\/#\/schema\/person\/a3304c209434f63c501b05622d7d2720\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/thoaicoder.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/thoaicoder.com\/#\/schema\/person\/a3304c209434f63c501b05622d7d2720\",\"name\":\"Thoai\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/thoaicoder.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/03\/photo_2023-01-05_22-53-32.jpg\",\"contentUrl\":\"https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/03\/photo_2023-01-05_22-53-32.jpg\",\"width\":640,\"height\":640,\"caption\":\"Thoai\"},\"logo\":{\"@id\":\"https:\/\/thoaicoder.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/thoaicoder.com\",\"https:\/\/www.linkedin.com\/in\/huu-thoai-nguyen\/\"],\"url\":\"https:\/\/thoaicoder.com\/index.php\/author\/admin_cvyxq4b8\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"BDD Testing with Cucumber - I \u2764 Code","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/","og_locale":"en_US","og_type":"article","og_title":"BDD Testing with Cucumber - I \u2764 Code","og_description":"1. BDD introduction BDD test stands for behavior-driven development test. It is a testing methodology [&hellip;]","og_url":"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/","og_site_name":"I \u2764 Code","article_published_time":"2023-06-06T09:33:22+00:00","article_modified_time":"2024-03-06T18:29:07+00:00","og_image":[{"width":731,"height":594,"url":"https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/06\/TDD-and-BDD.webp","type":"image\/webp"}],"author":"Thoai","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Thoai","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/#article","isPartOf":{"@id":"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/"},"author":{"name":"Thoai","@id":"https:\/\/thoaicoder.com\/#\/schema\/person\/a3304c209434f63c501b05622d7d2720"},"headline":"BDD Testing with Cucumber","datePublished":"2023-06-06T09:33:22+00:00","dateModified":"2024-03-06T18:29:07+00:00","mainEntityOfPage":{"@id":"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/"},"wordCount":1217,"commentCount":0,"publisher":{"@id":"https:\/\/thoaicoder.com\/#\/schema\/person\/a3304c209434f63c501b05622d7d2720"},"keywords":["BDD","Cucumber","Docker","Gradle","Java","Wiremock"],"articleSection":["Java","Software Development Practices"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/","url":"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/","name":"BDD Testing with Cucumber - I \u2764 Code","isPartOf":{"@id":"https:\/\/thoaicoder.com\/#website"},"datePublished":"2023-06-06T09:33:22+00:00","dateModified":"2024-03-06T18:29:07+00:00","breadcrumb":{"@id":"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/thoaicoder.com\/index.php\/2023\/06\/06\/bdd-testing-with-cucumber\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thoaicoder.com\/"},{"@type":"ListItem","position":2,"name":"BDD Testing with Cucumber"}]},{"@type":"WebSite","@id":"https:\/\/thoaicoder.com\/#website","url":"https:\/\/thoaicoder.com\/","name":"I \u2764 Code","description":"","publisher":{"@id":"https:\/\/thoaicoder.com\/#\/schema\/person\/a3304c209434f63c501b05622d7d2720"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thoaicoder.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/thoaicoder.com\/#\/schema\/person\/a3304c209434f63c501b05622d7d2720","name":"Thoai","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/thoaicoder.com\/#\/schema\/person\/image\/","url":"https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/03\/photo_2023-01-05_22-53-32.jpg","contentUrl":"https:\/\/thoaicoder.com\/wp-content\/uploads\/2023\/03\/photo_2023-01-05_22-53-32.jpg","width":640,"height":640,"caption":"Thoai"},"logo":{"@id":"https:\/\/thoaicoder.com\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/thoaicoder.com","https:\/\/www.linkedin.com\/in\/huu-thoai-nguyen\/"],"url":"https:\/\/thoaicoder.com\/index.php\/author\/admin_cvyxq4b8\/"}]}},"_links":{"self":[{"href":"https:\/\/thoaicoder.com\/index.php\/wp-json\/wp\/v2\/posts\/381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thoaicoder.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thoaicoder.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thoaicoder.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thoaicoder.com\/index.php\/wp-json\/wp\/v2\/comments?post=381"}],"version-history":[{"count":32,"href":"https:\/\/thoaicoder.com\/index.php\/wp-json\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":843,"href":"https:\/\/thoaicoder.com\/index.php\/wp-json\/wp\/v2\/posts\/381\/revisions\/843"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thoaicoder.com\/index.php\/wp-json\/wp\/v2\/media\/560"}],"wp:attachment":[{"href":"https:\/\/thoaicoder.com\/index.php\/wp-json\/wp\/v2\/media?parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thoaicoder.com\/index.php\/wp-json\/wp\/v2\/categories?post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thoaicoder.com\/index.php\/wp-json\/wp\/v2\/tags?post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}