-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transformers are not eligible for dependency injection #851
Comments
As we're building towards cucumber expressions this might be best solved by making it possible to add configurations to the configuration as part of the glue code. A lambda example of this might look like this: final class MaConfigurationFrançaise extends Configuration {
MaConfigurationFrançaise(TrafficLightDatabase database){
ExpressionDeConcombre("vert", "fue (rouge|jaune|vert)",
groupValues -> database.createTrafficLight(groupValues);
}
} |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in a week if no further activity occurs. |
This issue has been automatically closed because of inactivity. You can support the Cucumber core team on opencollective. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This issue has been automatically closed because of inactivity. You can support the Cucumber core team on opencollective. |
# Summary By annotating methods parameter and data table types can be defined as part of the Glue. This enables them to access the test context and makes them eligible for dependency injection. Additionally the type registry is now created for each feature. This means the language of the feature will be used to convert numbers. ## Details ## Parameter and DataTable Type Introduces the `@ParameterType` and `@DataTableType` annotations. This allows parameter and datatable types to be mapped to objects which can only be created by services inside the test context. For example in this scenario ```gherkin Given the awesome catalog When a user places the awestruck eels in his basket Then you will be shocked at what happened next ``` We are now able to look up the "awestruck eels" in the "awesome" catalog. ```java private final Catalog catalog; @ParameterType(".*") public Product product(String name) { return catalog.findProductByName(name); } ``` ## Default Transformer It is now also possible to register default transformers using annotations. Default transformers allow you to specific a transformer that will be used when there is no transform defined. This can be combined with an object mapper like Jackson to quickly transform well known string representations to Java objects. * `@DefaultParameterTransformer` * `@DefaultDataTableEntryTransformer` * `@DefaultDataTableCellTransformer` ```java package com.example.app; import com.fasterxml.jackson.databind.ObjectMapper; import io.cucumber.java.DefaultDataTableCellTransformer; import io.cucumber.java.DefaultDataTableEntryTransformer; import io.cucumber.java.DefaultParameterTransformer; import java.lang.reflect.Type; public class DataTableSteps { private final ObjectMapper objectMapper = new ObjectMapper(); @DefaultParameterTransformer @DefaultDataTableEntryTransformer @DefaultDataTableCellTransformer public Object defaultTransformer(Object fromValue, Type toValueType) { return objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType)); } } ``` ## Localization Some languages uses comma's rather then points to separate decimals. Previously to parse these properly you'd have to use `TypeRegistryConfigurer.locale` to set this globally. When not explicitly provided Cucumber will now take the language from the feature file. This makes the following work without additional configuration: ```gherkin # language: fr Fonctionnalité: Concombres fractionnaires Scénario: dans la ventre Étant donné j'ai 5,5 concombres fractionnaires ``` ```java @Étantdonné("j'ai {bigdecimal} concombres fractionnaires") public void jAiConcombresFractionnaires(BigDecimal arg0) { assertThat(arg0, is(new BigDecimal("5.5"))); } ``` # Motivation & Context Fixes #851. Fixes #1458.
Looking at the Guice module, I can see that dependency injection doesn't seem to take place on the
Transformer
interface.In order to support this, the core/src/main/java/cucumber/runtime/ParameterInfo.java would need the
private static Transformer<?> getTransformer(Annotation annotation)
methods to be updated to call the object factory. From here, Guice would be able to inject the dependencies.The text was updated successfully, but these errors were encountered: