3

I am trying to get Cucumber working with Spring. In our code, we are already using java based Spring configuration. I am having trouble getting it to work in the following scenario. Can someone please help?

Today , in our integration test classes we use @ContextConfiguration for each class and provide the config class that is declared with in that integration test class for loading the beans. Config class is annotated with @Configuration. Same bean could be instantiated differently in 2 different classes Config classes used in 2 different integration test classes.

So when I use Cucumber, since the Contextconfiguration differs on different classes, it looks for 'Cucumber.xml' . In the xml file, I am using component-scan to scan the cucumber step definition classes by giving the package name that these classes use (both classes have same package name) . Since all beans gets loaded in same context, Cucumber is failing to load the beans when it finds the same bean defined in these different config classes .

How do I get over this problem of creating same bean but in different ways and use them in different classes?
Please note that I am not looking for a solution that creates lot of churn from our existing coding practices, so having per-test-xml file is not an option for me.

Here is how our code looks:

Class NameAndAddressProviderIntegrationTestSteps :-

@ContextConfiguration(locations="classpath:cucumber.xml")
public class NameAndAddressProviderIntegrationTestSteps {
@Configuration
@Import({
    xyz.class,
    abc.class,
    NameAndAddressProvider.class
})
@ImportResource({
        "file:configuration/spring-configuration/abc.xml",
        "file:configuration/spring-configuration/xyz.xml"
})
public static class Config {

    @Bean
    AccountHolderDataMap dataMap() {
        AccountHolderDataMap data = new AccountHolderDataMap();
        data.put(ID,
                new AccountHolderData(customerID));
       data.get(customerID).setCustomerplaceID(testCustomerplaceID);
        return data;
    }

 }
@Inject
private NameAndAddressProvider provider;

@When("^I call nameandAddress provider with a 'customerId'$")
public void i_call_nameandAddress_provider_with_a_customerId() throws DependencyException {
    System.out.println("Entering when method");
    names = provider.getNames(customerID);
    System.out.println(provider.toString());       
}
......
}

Class AddressProviderIntegrationTestSteps:-

 @ContextConfiguration(locations="classpath:cucumber.xml")
 public class AddressProviderIntegrationTestSteps {
 @Configuration
 @Import({
        abc.class,
        xyz.class,
        AddressesProvider.class
})
@ImportResource({
        "file:configuration/spring-configuration/test-environment.xml",
        "file:configuration/spring-configuration/test-logging-config.xml"
})
public static class Config {
    @Bean
    @DependsOn("Environment")
    AccountHolderDataMap data() {
        AccountHolderDataMap data = new AccountHolderDataMap();
       data.put(testCustomerID,
                new AccountHolderData(testCustomerID, testCustomerplaceID,businessType));
        return data;
    }
}

private static final String testCustomerID = "1234";
private static final String testMarketplaceID = "abc";

@Inject
private AddressesProvider provider;

@When("^I call AddressesProvider provider with a 'CustomerID'$")
public void i_call_AddressesProvider_provider_with_a_CustomerID() throws Throwable {
    List<Address> addresses = provider.getAddresses(testCustomerID);
    Log.info(addresses.get(0).toString());
    assertTrue(addresses.size()==1);
  }    

}

And here is the nested exception I am getting:- "nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [....AccountHolderDataMap] is defined: expected single matching bean but found 2: dataMap,data"

Appreciate your help!

1

1 Answer 1

1

I've managed multiple sources for bean-definitions. You can use this at a starting point (or others in the internet as your question is quite old) I am using spring4, see my other cucumer post for the pom

At the stepdefs use a config.class

@ContextConfiguration(classes = { CucumberConfiguration.class })
public class StepdefsTest123 { 

    @Autowired bean; // from cucumberBeanContext.xml


    @When("^A$")
    public void a() throws Throwable {
        System.out.println(bean.getFoo());
    }

}

in the config-class add aditional beandefinitions

@Configuration
@ComponentScan(basePackages   = "package.here.cucumber")
@ImportResource("classpath:cucumberBeanContext.xml")
public class CucumberConfiguration {

    // nothing to do here

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.