|
| 1 | +Cucumber-JVM v5.1.0-v5.7.0 |
| 2 | +========================== |
| 3 | + |
| 4 | +In the last three months we picked up the pace and managed to do a new |
| 5 | +release nearly every week. With v6-RC1 released now would be a good time to |
| 6 | +look back and see what is still relatively new in v5. |
| 7 | + |
| 8 | +As always the [full changelog] can be in its usual place. |
| 9 | + |
| 10 | +[full changelog]: ../CHANGELOG.md |
| 11 | + |
| 12 | + |
| 13 | +v5.2.0: Redefine build in data table types |
| 14 | +------------------------------------------ |
| 15 | + |
| 16 | +In v5.0.0 empty cells in a data table would be transformed into null rather than |
| 17 | +the empty string. By using `replaceWithEmptyString = "[blank]"` on a |
| 18 | +datatable type an empty string can be explicitly inserted. |
| 19 | + |
| 20 | +However, this did not work for when converting to build in types such as `String` |
| 21 | +and `Object`. It is now possible to redefine build in types. |
| 22 | + |
| 23 | +This enables the following: |
| 24 | + |
| 25 | +```gherkin |
| 26 | +Feature: Whitespace |
| 27 | + Scenario: Whitespace in a table |
| 28 | + Given a blank value |
| 29 | + | key | value | |
| 30 | + | a | [blank] | |
| 31 | +``` |
| 32 | + |
| 33 | +```java |
| 34 | +@given("A blank value") |
| 35 | +public void givenABlankValue(Map<String, String> map){ |
| 36 | + // map contains { "key":"a", "value": ""} |
| 37 | +} |
| 38 | + |
| 39 | +@DataTableType(replaceWithEmptyString = "[blank]") |
| 40 | +public String listOfStringListsType(String cell) { |
| 41 | + return cell; |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Note that this only applies to `String` and `Object`. It is not possible to |
| 46 | +redefine other build in types. Though this could be considered if there is a |
| 47 | +clear use case. |
| 48 | + |
| 49 | +v5.3.0: Sensible CLI defaults |
| 50 | +----------------------------- |
| 51 | + |
| 52 | +The Cucumber CLI is rather complex to use. To work correctly it needs both |
| 53 | +a `--glue` parameter in the form of a package name, and a location of a feature |
| 54 | +file in form of a classpath uri or path. As a result people often configure |
| 55 | +Cucumber incorrectly and are left wondering why their features or glue cannot |
| 56 | +be found. |
| 57 | + |
| 58 | +This can be simplified by using sensible default for both. Unless explicitly |
| 59 | +told otherwise Cucumber will assume that glue and feature files reside in the |
| 60 | +class path root. So with the following project layout the CLI can discover and |
| 61 | +execute all features without needing additional arguments. |
| 62 | + |
| 63 | +``` |
| 64 | +├── pom.xml |
| 65 | +├── src |
| 66 | +│ ├── main |
| 67 | +│ │ └── java |
| 68 | +│ │ └── com/example/Application.java |
| 69 | +│ └── test |
| 70 | +│ ├── java |
| 71 | +│ │ └── com/example/StepDefinitions.java |
| 72 | +│ └── resources |
| 73 | +│ └── com/example/example.feature |
| 74 | +``` |
| 75 | + |
| 76 | +This can be done with a single maven command: |
| 77 | + |
| 78 | +```shell script |
| 79 | +mvn exec:java \ |
| 80 | + -Dexec.classpathScope=test \ |
| 81 | + -Dexec.mainClass=io.cucumber.core.cli.Main |
| 82 | +``` |
| 83 | + |
| 84 | +v5.3.0: Skip Scenarios with the JUnit Platform |
| 85 | +---------------------------------------------- |
| 86 | + |
| 87 | +The JUnit Platform supports skipping tests. For example in JUnit Jupiter a |
| 88 | +test can be annotated with `@Disabled`. This test will be marked as skipped. |
| 89 | + |
| 90 | +Cucumber scenarios do not have annotations nor is there any support to disable |
| 91 | +specific scenarios. A typical work around from JUnit 4 was to set |
| 92 | +`tags="not @Disabled"` in `@CucumberOptions` and tag scenarios with |
| 93 | +`@Disabled`. |
| 94 | + |
| 95 | +To do the same with the Cucumber JUnit Platform Engine the |
| 96 | +`cucumber.filter.tags` property can be used. For example by adding |
| 97 | +`cucumber.filter.tags=not @Disabled` to `junit-platform.properties`. |
| 98 | + |
| 99 | +Note: unlike JUnit 4, skipped scenarios are not remove from in the test |
| 100 | +hierarchy. |
| 101 | + |
| 102 | +v5.5.0: @ParameterType(useRegexpMatchAsStrongTypeHint) |
| 103 | +---------------------------------------- |
| 104 | + |
| 105 | +When using regular expressions, Cucumber will use the pattern in a capture |
| 106 | +group as hint to determine which parameter type should be used. |
| 107 | + |
| 108 | +When the type hint provided by the regex, and the type hint provided by the |
| 109 | +method (i.e. the types of its arguments) disagree about the type |
| 110 | +`cucumber-expressions` prefers the hint provided by the regex. |
| 111 | + |
| 112 | +When declaring parameters types with a very simple regular expressions this may |
| 113 | +cause a problem. For example: |
| 114 | + |
| 115 | +```java |
| 116 | +import io.cucumber.java.ParameterType; |
| 117 | +import io.cucumber.java.en.Given; |
| 118 | + |
| 119 | +public class Main { |
| 120 | + |
| 121 | + private static class TypeA {} |
| 122 | + |
| 123 | + @ParameterType("\\w+") |
| 124 | + public TypeA typeA(String a) { |
| 125 | + return new TypeA(); |
| 126 | + } |
| 127 | + |
| 128 | + @given("a cucumber expression of {typeA}") |
| 129 | + public void an_object_of_type_a(TypeA a) { |
| 130 | + // works fine! |
| 131 | + } |
| 132 | + |
| 133 | + @given("^a regular expression of (\\w+)$") |
| 134 | + public void an_object_of_type_b(String b) { |
| 135 | + // broken: will attempt to use the transformer for TypeA |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +By using `@ParameterType(pattern="\\w+", useRegexpMatchAsStrongTypeHint=false)` |
| 141 | +this behaviour can be changed to instead prefer the type hint from the method. |
| 142 | + |
| 143 | +Note: `useRegexpMatchAsStrongTypeHint` will default to `false` in v6. |
| 144 | + |
| 145 | +v5.5.0: A better way to configure the application context in Cucumber Spring |
| 146 | +---------------------------------------------------------------------------- |
| 147 | + |
| 148 | +Cucumber Spring has a complicated way to configure the application context. |
| 149 | + |
| 150 | +The configuration can be provided by either: |
| 151 | + * A context configuration annotation on a class which also happens to have |
| 152 | + step definitions |
| 153 | + * A magic file named `cucumber.xml` |
| 154 | + * An empty application context if the previous options could not be discovered |
| 155 | + |
| 156 | +This makes it hard to explain how to use Cucumber Spring and the fallback |
| 157 | +strategy to the empty application context hides errors. |
| 158 | + |
| 159 | +From now on the preferred way to use `cucumber-spring` is to annotate a class |
| 160 | +with both `@CucumberContextConfiguration` and a Spring context configuration |
| 161 | +annotation such as `@ContextConfiguration`, `@SpringBootTest`, ect. |
| 162 | + |
| 163 | +```java |
| 164 | +import com.example.app; |
| 165 | + |
| 166 | +import org.springframework.boot.test.context.SpringBootTest; |
| 167 | + |
| 168 | +import io.cucumber.spring.CucumberContextConfiguration; |
| 169 | + |
| 170 | +@CucumberContextConfiguration |
| 171 | +@SpringBootTest(classes = TestConfig.class) |
| 172 | +public class CucumberSpringConfiguration { |
| 173 | + |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +The alternatives, `cucumber.xml` and annotating step definitions with a |
| 178 | +`@ContextConfiguration` have been deprecated and will be removed in v6. |
| 179 | + |
| 180 | +v5.7.0: Exclusive resources with Junit 5 |
| 181 | +---------------------------------------- |
| 182 | + |
| 183 | +The JUnit Platform supports parallel execution. To avoid flakey tests when |
| 184 | +multiple scenarios manipulate the same resource tests can be |
| 185 | +[synchronized][junit5-user-guide-synchronization] on that resource. |
| 186 | + |
| 187 | +[junit5-user-guide-synchronization]: https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution-synchronization |
| 188 | + |
| 189 | +To synchronize a scenario on a specific resource the scenario must be tagged |
| 190 | +and this tag mapped to a lock for a specific resource. A resource is identified |
| 191 | +by a string and can be either locked with a read-write-lock, or a read-lock. |
| 192 | + |
| 193 | +For example: |
| 194 | + |
| 195 | +```gherkin |
| 196 | +Feature: Exclusive resources |
| 197 | +
|
| 198 | + @reads-and-writes-system-properties |
| 199 | + Scenario: first example |
| 200 | + Given this reads and writes system properties |
| 201 | + When it is executed |
| 202 | + Then it will not be executed concurrently with the second example |
| 203 | +
|
| 204 | + @reads-system-properties |
| 205 | + Scenario: second example |
| 206 | + Given this reads system properties |
| 207 | + When it is executed |
| 208 | + Then it will not be executed concurrently with the first example |
| 209 | +
|
| 210 | +``` |
| 211 | + |
| 212 | +With this configuration: |
| 213 | + |
| 214 | +``` |
| 215 | +cucumber.execution.exclusive-resources.reads-and-writes-system-properties.read-write=SYSTEM_PROPERTIES |
| 216 | +cucumber.execution.exclusive-resources.reads-system-properties.read=SYSTEM_PROPERTIES |
| 217 | +``` |
| 218 | + |
| 219 | +The first scenario tagged with `@reads-and-writes-system-properties` will lock |
| 220 | +the `SYSTEM_PROPERTIES` with a read-write lock and will not be concurrently |
| 221 | +executed with the second scenario that uses a read lock. |
0 commit comments