Using different java spring context configurations with cucumber

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

Today in our test integration classes, we use @ContextConfiguration for each class and provide a configuration class declared in this integration test class to load beans. The Config class is annotated with @Configuration. The same bean can be instantiated differently in two different classes of configuration classes used in 2 different classes of integration tests.

So when I use Cucumber, since Contextconfiguration is different from different classes, it looks for "Cucumber.xml". In the xml file, I use component scanning to scan the perch pitch definition classes by specifying the package name that these classes use (both classes have the same package name). Since all beans are loaded in the same context, Cucumber does not load beans when it finds the same bean defined in these different config classes.

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

This is what our code looks like:

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());       
}
......
}

      

AddressProviderIntegrationTestSteps class: -

 @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: - "inest exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: no bean defined of type [.... AccountHolderDataMap]: expected single bean match, but 2 found: dataMap, data"

Appreciate your help!

+3


source to share


1 answer


I have used several sources for bean-definitions. You can use this at the starting point (or others on the internet as your question is quite old) I am using spring4 see my other post cucumer for pom

On stepdefs use 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 additional extensions

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

    // nothing to do here

}

      

0


source







All Articles