|
| 1 | +--- |
| 2 | +description: Java Acceptance criteria guidelines |
| 3 | +globs: **/*.feature |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | +# Java Acceptance criteria guidelines |
| 7 | + |
| 8 | +# Gherkin for Java Acceptance Criteria |
| 9 | + |
| 10 | +This document provides guidelines for writing Gherkin feature files to define acceptance criteria for Java applications. It focuses on capturing both functional and non-functional requirements in a clear, concise, and testable manner. |
| 11 | + |
| 12 | +## Implementing These Principles |
| 13 | + |
| 14 | +These guidelines are built upon the following core principles: |
| 15 | + |
| 16 | +- Clarity: Scenarios should be easily understandable by all stakeholders (developers, testers, business analysts). |
| 17 | +- Testability: Scenarios should be written in a way that allows for straightforward automation. |
| 18 | +- Completeness: Cover both functional behavior and relevant non-functional characteristics. |
| 19 | +- Collaboration: Gherkin files serve as a shared understanding of requirements. |
| 20 | + |
| 21 | +## Table of contents |
| 22 | + |
| 23 | +- Rule 1: Standard Gherkin Structure |
| 24 | +- Rule 2: Writing Effective Scenarios for Functional Requirements |
| 25 | +- Rule 3: Incorporating Non-Functional Requirements (NFRs) |
| 26 | +- Rule 4: Using Background for Preconditions |
| 27 | +- Rule 5: Scenario Outlines for Data-Driven Tests |
| 28 | +- Rule 6: Tags for Organization and Filtering |
| 29 | + |
| 30 | +## Rule 1: Standard Gherkin Structure |
| 31 | + |
| 32 | +Title: Adhere to Standard Gherkin Keywords and Structure |
| 33 | +Description: Gherkin files should follow the standard `Feature`, `Scenario` (or `Example`), `Given`, `When`, `Then`, `And`, `But` keywords. This ensures consistency and readability. |
| 34 | + |
| 35 | +**Good example:** |
| 36 | + |
| 37 | +```gherkin |
| 38 | +Feature: User Authentication |
| 39 | + |
| 40 | + Scenario: Successful login |
| 41 | + Given the user is on the login page |
| 42 | + When the user enters valid credentials |
| 43 | + And clicks the login button |
| 44 | + Then the user should be redirected to the dashboard |
| 45 | +``` |
| 46 | + |
| 47 | +**Bad Example:** |
| 48 | + |
| 49 | +```gherkin |
| 50 | +# Missing Feature or Scenario keywords, or using non-standard terms. |
| 51 | +# For instance, just writing a paragraph of text without structure. |
| 52 | + |
| 53 | +User tries to log in. |
| 54 | +If they provide correct username and password, they see the main page. |
| 55 | +``` |
| 56 | + |
| 57 | +## Rule 2: Writing Effective Scenarios for Functional Requirements |
| 58 | + |
| 59 | +Title: Clearly Define Actions and Expected Outcomes for Functional Behavior |
| 60 | +Description: Scenarios should describe a specific user interaction or system behavior and its observable outcome. Focus on "what" the system does, not "how" it does it. |
| 61 | + |
| 62 | +**Good example:** |
| 63 | + |
| 64 | +```gherkin |
| 65 | +Feature: Product Search |
| 66 | + |
| 67 | + Scenario: Search for an existing product |
| 68 | + Given the user is on the product search page |
| 69 | + When the user enters "Laptop" in the search bar |
| 70 | + And clicks the search button |
| 71 | + Then a list of laptops should be displayed |
| 72 | + And each product in the list should show its name and price |
| 73 | +``` |
| 74 | + |
| 75 | +**Bad Example:** |
| 76 | + |
| 77 | +```gherkin |
| 78 | +Feature: Product Search |
| 79 | + |
| 80 | + Scenario: Test product search |
| 81 | + Given the database connection is established |
| 82 | + When the search algorithm processes the query "Laptop" |
| 83 | + Then the results are fetched from the Product table |
| 84 | + And the UI renders the data |
| 85 | +# This is too focused on implementation details, not user-observable behavior. |
| 86 | +``` |
| 87 | + |
| 88 | +## Rule 3: Incorporating Non-Functional Requirements (NFRs) |
| 89 | + |
| 90 | +Title: Specify Non-Functional Requirements Clearly within Scenarios or via Tags |
| 91 | +Description: NFRs like performance, security, and usability can be incorporated into Gherkin. This can be done by adding specific steps in scenarios or by using tags to categorize NFR-related tests. |
| 92 | + |
| 93 | +**Good example (Performance in Scenario):** |
| 94 | + |
| 95 | +```gherkin |
| 96 | +Feature: Product Search Performance |
| 97 | + |
| 98 | + Scenario: Search response time |
| 99 | + Given the user is on the product search page |
| 100 | + When the user searches for "Electronics" |
| 101 | + Then the search results should be displayed within 2 seconds |
| 102 | +``` |
| 103 | + |
| 104 | +**Good example (Security via Tag and Scenario):** |
| 105 | + |
| 106 | +```gherkin |
| 107 | +Feature: Secure Data Access |
| 108 | + |
| 109 | + @security @authorization |
| 110 | + Scenario: Unauthorized access to admin panel |
| 111 | + Given a user with "viewer" role is logged in |
| 112 | + When the user attempts to access the admin panel |
| 113 | + Then the user should be denied access |
| 114 | + And an "Access Denied" message should be displayed |
| 115 | +``` |
| 116 | + |
| 117 | +**Good example (Usability - Accessibility):** |
| 118 | + |
| 119 | +```gherkin |
| 120 | +Feature: Website Accessibility |
| 121 | + |
| 122 | + Scenario: Keyboard navigation for login form |
| 123 | + Given the user is on the login page |
| 124 | + When the user navigates the login form using only the keyboard |
| 125 | + Then all interactive elements (username, password, login button) should be focusable |
| 126 | + And the user should be able to submit the form using the Enter key |
| 127 | +``` |
| 128 | + |
| 129 | +**Bad Example (Vague NFR):** |
| 130 | + |
| 131 | +```gherkin |
| 132 | +Feature: System Performance |
| 133 | + |
| 134 | + Scenario: System should be fast |
| 135 | + Given any user action |
| 136 | + When the action is performed |
| 137 | + Then the system should respond quickly |
| 138 | +# "Fast" and "quickly" are subjective and not testable without specific metrics. |
| 139 | +``` |
| 140 | + |
| 141 | +## Rule 4: Using Background for Preconditions |
| 142 | + |
| 143 | +Title: Use `Background` for Steps Common to All Scenarios in a Feature |
| 144 | +Description: The `Background` keyword allows you to define steps that are executed before each scenario in a feature file. This reduces repetition. |
| 145 | + |
| 146 | +**Good example:** |
| 147 | + |
| 148 | +```gherkin |
| 149 | +Feature: Shopping Cart Management |
| 150 | + |
| 151 | + Background: |
| 152 | + Given the user has an active account |
| 153 | + And the user is logged in |
| 154 | + And the user has an empty shopping cart |
| 155 | + |
| 156 | + Scenario: Add item to cart |
| 157 | + When the user adds "Product A" to the cart |
| 158 | + Then the shopping cart should contain 1 item |
| 159 | + And the item should be "Product A" |
| 160 | + |
| 161 | + Scenario: Add multiple items to cart |
| 162 | + When the user adds "Product B" to the cart |
| 163 | + And the user adds "Product C" to the cart |
| 164 | + Then the shopping cart should contain 2 items |
| 165 | +``` |
| 166 | + |
| 167 | +## Rule 5: Scenario Outlines for Data-Driven Tests |
| 168 | + |
| 169 | +Title: Employ `Scenario Outline` and `Examples` for Data-Driven Testing |
| 170 | +Description: Use `Scenario Outline` (or `Scenario Template`) with an `Examples` table to run the same scenario with different sets of data. This is useful for testing various inputs and conditions without duplicating the scenario logic. |
| 171 | + |
| 172 | +**Good example:** |
| 173 | + |
| 174 | +```gherkin |
| 175 | +Feature: User Login Validation |
| 176 | + |
| 177 | + Scenario Outline: Login with different credentials |
| 178 | + Given the user is on the login page |
| 179 | + When the user enters "<username>" and "<password>" |
| 180 | + And clicks the login button |
| 181 | + Then the user should see "<message>" |
| 182 | + |
| 183 | + Examples: |
| 184 | + | username | password | message | |
| 185 | + | "valid_user" | "valid_pass"| "Welcome to your dashboard" | |
| 186 | + | "invalid_user"| "wrong_pass"| "Invalid credentials" | |
| 187 | + | "" | "some_pass" | "Username cannot be empty" | |
| 188 | + | "some_user" | "" | "Password cannot be empty" | |
| 189 | +``` |
| 190 | + |
| 191 | +## Rule 6: Tags for Organization and Filtering |
| 192 | + |
| 193 | +Title: Utilize Tags for Categorizing and Filtering Scenarios |
| 194 | +Description: Tags (e.g., `@functional`, `@smoke`, `@regression`, `@sprint-5`, `@NFR-Performance`) help organize scenarios and allow for selective execution of tests. |
| 195 | + |
| 196 | +**Good example:** |
| 197 | + |
| 198 | +```gherkin |
| 199 | +Feature: Order Processing |
| 200 | + |
| 201 | + @smoke @functional |
| 202 | + Scenario: Successful order placement |
| 203 | + Given a logged-in user with items in the cart |
| 204 | + When the user proceeds to checkout |
| 205 | + And provides valid shipping and payment information |
| 206 | + Then the order should be placed successfully |
| 207 | + And the user should receive an order confirmation |
| 208 | + |
| 209 | + @regression @functional @edge-case |
| 210 | + Scenario: Order placement with insufficient stock |
| 211 | + Given a logged-in user |
| 212 | + And "Product X" has only 1 item in stock |
| 213 | + When the user tries to add 2 units of "Product X" to the cart |
| 214 | + And proceeds to checkout |
| 215 | + Then the order placement should fail |
| 216 | + And an "Insufficient stock" message should be displayed |
| 217 | +``` |
| 218 | +--- |
| 219 | +*(This template provides a comprehensive starting point. Adapt and extend these rules based on your project's specific needs and conventions.)* |
| 220 | + |
| 221 | + |
0 commit comments