Flyout91
Flyout91

Reputation: 860

Cucumber - Share context with the type registry configuration file

What I want: I want to use a spring @Autowired annotation in the file conventionally named "TypeRegistryConfiguration". It works perfectly well for steps file, but for some reason the dependency injection does not work in this file (there is no error/warn message even in debug level). Spring scans "com.funky.steps", which contains the steps, the context and the type registry configuration file, see example below.

Context:

package com.funky.steps.context;

@Component
public class CommonContext {

...

Type registry configuration:

package com.funky.steps.typeregistry;

public class TypeRegistryConfiguration implements TypeRegistryConfigurer {

    @Autowired 
    private CommonContext context; // NOT INJECTED !

    @Override
    public Locale locale() {
        return Locale.ENGLISH;
    }

    @Override
    public void configureTypeRegistry(TypeRegistry typeRegistry) {
        registerStuff(typeRegistry)
    }

    ...

Steps:

package com.funky.steps;

public class WebServiceSteps {

    @Autowired
    private CommonContext context; // Correctly injected

    ...

Why I want it: I have steps that save variables in the context for later use. When I build an object using type registry, I want to be able to access these variables. Example:

Given I call the web service 1
And the response field "id" will be used as "$id" # id is saved in the context
When I call the web service 2: # call type registry configuration to build the request using $id (which I can not access because it is in the context and @Autowired is not working)
| id | $id |
Then ...

Upvotes: 2

Views: 1967

Answers (3)

user861594
user861594

Reputation: 5908

What ever you are looking for can be achieve by cucumber 5, using qaf-cucumber and cucumber 5 using qaf-cucumber by using . below is example:

Given I call the web service 1
And the response field "id" will be used as "id"
When I call the web service 2:
| id | ${id} |
Then ...

Step implementation may look like store method implementation:

@And("the response field {sting} will be used as {string}")
public void theResponseFiFeldWillBeUsedAs(String field, String key) {
    //do the need full
    String value = read(field);
    getBundle().setProperty(key, value);
}

@@QAFTestStep("I call the web service 2:")
public void iCallWS2:(Map<String, Object> map) {
    //map should be with key id and resolved value
}

Furthermore you can get benefit from web-service support library as well. It offers request call repository concept with parameter which makes web service testing easy and maintainable.

Upvotes: 1

M.P. Korstanje
M.P. Korstanje

Reputation: 12039

This is not possible in Cucumber 4.x but you are able to register parameter and data table types as part of the glue in Cucumber 5.0.0-RC1.

Instead of registering a parameter type with registry.registerParameterType you'd use @ParameterType instead. The same works for the data table types.

private final Catalog catalog; 
private final Basket basket;

@ParameterType("[a-z ]+")
public Catalog catalog(String name) {
  return catalogs.findCatalogByName(name);
}

@ParameterType("[a-z ]+")
public Product product(String name) {
  return catalog.findProductByName(name);
}

@Given("the {catalog} catalog")
public void the_catalog(Catalog catalog){
  this.catalog = catalog
}

@When("a user places the {product} in his basket")
public void a_user_place_the_product_in_his_basket(Product product){
  basket.add(product);
}

Note: The method name is used as the parameter name. A parameter name can also be provided via the name property of @ParameterType.

See: https://cucumber.io/blog/announcing-cucumber-jvm-v5-0-0-rc1/

Upvotes: 2

Flyout91
Flyout91

Reputation: 860

What I want is impossible for now. See :

https://github.com/cucumber/cucumber-jvm/issues/1516

Upvotes: 0

Related Questions