Skip to content
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

[Java] Allow parameter types access to test context #1677

Merged
merged 30 commits into from
Jul 28, 2019

Conversation

mpkorstanje
Copy link
Contributor

@mpkorstanje mpkorstanje commented Jun 30, 2019

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

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.

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
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:

# language: fr
Fonctionnalité: Concombres fractionnaires

  Scénario: dans la ventre
    Étant donné j'ai 5,5 concombres fractionnaires
@Étantdonné("j'ai {bigdecimal} concombres fractionnaires")
public void jAiConcombresFractionnaires(BigDecimal arg0) {
    assertThat(arg0, is(new BigDecimal("5.5")));
}

Motivation & Context

Fixes #851.
Fixes #1458.

Types of changes

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).

Checklist:

  • I've added tests for my code.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@mpkorstanje mpkorstanje changed the base branch from master to develop-v5 June 30, 2019 11:57
@mpkorstanje mpkorstanje added this to the 5.0.0 milestone Jun 30, 2019
@mpkorstanje mpkorstanje force-pushed the allow-parameter-types-access-to-test-context branch 2 times, most recently from cc1c541 to 6b3b763 Compare July 12, 2019 21:03
@mpkorstanje mpkorstanje force-pushed the allow-parameter-types-access-to-test-context branch 3 times, most recently from 99cadd0 to 9475dc9 Compare July 21, 2019 17:47
@mpkorstanje mpkorstanje force-pushed the allow-parameter-types-access-to-test-context branch from 9475dc9 to bfef3a6 Compare July 21, 2019 17:48
@mpkorstanje mpkorstanje force-pushed the allow-parameter-types-access-to-test-context branch from c144e7e to ec3563c Compare July 22, 2019 07:16
@mpkorstanje mpkorstanje changed the title WIP Allow parameter types access to test context [Java] Allow parameter types access to test context Jul 28, 2019
@mpkorstanje mpkorstanje merged commit ce0a6b8 into develop-v5 Jul 28, 2019
@mpkorstanje mpkorstanje deleted the allow-parameter-types-access-to-test-context branch July 28, 2019 10:40
mpkorstanje pushed a commit that referenced this pull request Oct 9, 2019
mpkorstanje pushed a commit that referenced this pull request Oct 18, 2019
With the implementation of `@ParameterType` and friends in Java #1764 and
Java8 #1677 there is no reason to maintain the `TypeRegistryConfigurer`.
Deprecating and eventually removing it will simplify the implementation of
Cucumber Core as it will no longer have to load any glue code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants