Hot questions for Using Cucumber in spring

Question:

I have an application where I'm running a trial with different technologies. I have a set of interfaces implemented with each technology and I use spring profiles to decide which technology to run. Each of the technologies has its own Spring java config annotated with the profile they are active for.

I run my cucumber tests defining which profile is the active one but this forces me to manually change the string every time I want to test a different profile, making it impossible to run automated tests for all of them. Is there anyway in cucumber to provide a set of profiles so tests are run once for each of them?

Thanks!


Answer:

You have two possibilities

  1. Standard - use few test classes ran by Cucumber runners
  2. Write custom Cucumber jUnit runner (or take ready one) that supports multiple configurations.

In the first case, it will look like below. The disadvantage is that you have to define different reports for each runner and have almost identical Cucumber runners for each config.

Here's how classes can look like:

CucumberRunner1.java

@RunWith(Cucumber.class)
@CucumberOptions(glue = {"com.abc.def", "com.abc.common"},
        features = {"classpath:com/abc/def/",
                "classpath:com/abc/common.feature"},
        format = {"json:target/cucumber/cucumber-report-1.json"},
        tags = {"~@ignore"},
        monochrome = true)
public class CucumberRunner1 {
}

StepAndConfig1.java

@ContextConfiguration(locations = {"classpath:/com/abc/def/configuration1.xml"})
public class StepsAndConfig1 {
    @Then("^some useful step$")
    public void someStep(){
        int a = 0;
    }
}

CucumberRunner2.java

@RunWith(Cucumber.class)
@CucumberOptions(glue = {"com.abc.ghi", "com.abc.common"},
        features = {"classpath:com/abc/ghi/",
                "classpath:com/abc/common.feature"},
        format = {"json:target/cucumber/cucumber-report-2.json"},
        tags = {"~@ignore"},
        monochrome = true)
public class CucumberRunner2 {
}

OnlyConfig2.java

@ContextConfiguration(classes = JavaConfig2.class)
public class OnlyConfig2 {
    @Before
    public void justForCucumberToPickupThisClass(){}
}

The second approach is to use custom cucumber runner that supports multiple configurations. You can either write it by yourself or take ready one, for example, mine - CucumberJar.java and root of project cucumber-junit. In this case Cucumber runner will looks like this:

CucumberJarRunner.java

@RunWith(CucumberJar.class)
@CucumberOptions(glue = {"com.abc.common"},
        tags = {"~@ignore"},
        plugin = {"json:target/cucumber/cucumber-report-common.json"})
@CucumberGroupsOptions({
        @CucumberOptions(glue = {"com.abc.def"},
                features = {"classpath:com/abc/def/",
                        "classpath:com/abc/common.feature"}
        ),
        @CucumberOptions(glue = {"com.abc.ghi"},
                features = {"classpath:com/abc/ghi/",
                        "classpath:com/abc/common.feature"}
        )
})
public class CucumberJarRunner {
}

Question:

I'm using the aggregator of spring integration in my app. I'm using cucumber to test the flow, and I want to make sure that the aggregator release strategy is executed by the right parameters. I have 2 params for that strategy: timeout, and size. I would like to know if there is a way to load those parameters dynamically during the steps? So far I'm lost about how to make it work.

Thanks,

Thank you Artem,

I used the directFieldAccessor and I'm trying to get the release-strategy-expression and group-timeout attributes from the aggregator, but the problem is that I can't access those fields:

public void setAggregatorConfiguration(String aggregatorName, int aggregationThreshold, long aggregationTimeout) { 
         EventDrivenConsumer aggregator = getAggregator(aggregatorName);           
         DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(aggregator); 
         directFieldAccessor.setPropertyValue("group-timeout", aggregationTimeout); 
} 

Answer:

I have 2 params for that strategy: timeout, and size

Well, I guess you use TimeoutCountSequenceSizeReleaseStrategy. As we see those params are final in that class and they really can't be changed at runtime just with setters.

There is only one way to change such values at runtime - using DirectFieldAccessor.

However if you share more info about your use-case and config and unit test as well, we might find some other way to help you.

I've edited your post to add your code. No, you have to use the real property name, so it should be groupTimeoutExpression, not group-timeout. From other side there is a public setter on the matter: AbstractCorrelatingMessageHandler.setGroupTimeoutExpression(Expression groupTimeoutExpression).

From other side you go a bit wrong way: any MessageHandler component (<aggregator>, <service-activator>) provides several component. Right, the root of them is a AbstractEndpoint (the parent of EventDrivenConsumer). But all of your desired properties are to the AggregatingMessageHandler, which can get from BeanFactory using its alias - aggregatorName + ".handler".

release-strategy-expression is property of ExpressionEvaluatingReleaseStrategy, which you can change using DirectFieldAccessor on the AggregatingMessageHandler:

AggregatingMessageHandler handler = beanFactory.getBean(aggregatorName + ".handler", AggregatingMessageHandler.class);
DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(handler); 
ReleaseStrategy releaseStrategy = (ReleaseStrategy) directFieldAccessor.getPropertyValue("releaseStrategy");
DirectFieldAccessor dfa = new DirectFieldAccessor(releaseStrategy); 
dfa.setPropertyValue("expression", ...);

Anyway, it isn't clear, why you need such a perversion...

Question:

I am using Cucumber testing framing with spring-rest web service. I am using standard cucumber JUnit framework and Gradle build tool. Here are the dependencies

testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('info.cukes:cucumber-java:1.2.4')
testCompile('info.cukes:cucumber-junit:1.2.4')
testCompile ("info.cukes:cucumber-spring:1.2.4")
testCompile('org.springframework.security:spring-security-test')

Here is my feature file, location of feature file is src/main/resources/feature/addition.feature

Feature: Testing a REST API
   Users should be able to add two numbers

Scenario: number addition service
 When baseUri is /messages/add
   Then result should be 3

Here is my step definition code. it resides in test/java/somepackage/

public class StepDefinition {

    @When("^baseUri is (.*)$")
    public void baseUri(String uri) {

    }

    @Then("result should be (\\d+)")
    public void checkResult(int result){
      Assert.assertEquals(result, this.result);
    }
}

My test runner class is, test runner and StepDefinition both are in the same package in /test/java

@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:feature/", glue = {"com.example.restdemo.web.StepDefinition"}, format = {"pretty", "html:target/cucumber"})
public class CucumberIntegrationTest {
}

Now when I am running my test runner class i.e CucumberIntegrationTest.java, I am getting following O/P.

            "C:\Program Files\Java\jdk1.8.0_144\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:C:\Users\govindas\Downloads\ideaIU-2017.2.5.win\lib\idea_rt.jar=52114:C:\Users\govindas\Downloads\ideaIU-2017.2.5.win\bin -Dfile.encoding=UTF-8 -classpath "C:\Users\govindas\Downloads\ideaIU-2017.2.5.win\lib\idea_rt.jar;C:\Users\govindas\Downloads\ideaIU-2017.2.5.win\plugins\junit\lib\junit-rt.jar;C:\Users\govindas\Downloads\ideaIU-2017.2.5.win\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\rt.jar;C:\Users\govindas\IdeaProjects\restdemo\out\test\classes;C:\Users\govindas\IdeaProjects\restdemo\out\production\classes;C:\Users\govindas\IdeaProjects\restdemo\out\production\resources;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-cache\1.5.7.RELEASE\98480d5e70a79ff279ea0bb156f2d18222ba8211\spring-boot-starter-cache-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-data-rest\1.5.7.RELEASE\cc092ea664a736c5fdf6077dd86f3459bcdd8caa\spring-boot-starter-data-rest-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-hateoas\1.5.7.RELEASE\be7eb70b2ac27fb270278530238323e441afd1cf\spring-boot-starter-hateoas-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-jdbc\1.5.7.RELEASE\98c652a5461ccbec70b55fd0f7815d9c9a37a082\spring-boot-starter-jdbc-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-jersey\1.5.7.RELEASE\dc608d6e96755fdad671da5f9a11f86b345fe30d\spring-boot-starter-jersey-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-security\1.5.7.RELEASE\e680aa1a8990a6e98e7a843c88f0a8bc378bb4d\spring-boot-starter-security-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-web\1.5.7.RELEASE\a862305be8b7512a41f6768d825ed37251ccbbe0\spring-boot-starter-web-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-test\1.5.7.RELEASE\d23f37a72c2d4e46aba66bc69a649213f9cadeee\spring-boot-starter-test-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-spring\1.2.4\f3cff393c13087ac43e11936a90ec0c8792c534\cucumber-spring-1.2.4.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-java\1.2.4\57cca534b7abe43f6dd7624b90d3d97d33d3023d\cucumber-java-1.2.4.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.apache.derby\derby\10.13.1.1\29c42babe5c5463c6eea45487d3cfbfe8348ccd1\derby-10.13.1.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-junit\1.2.4\8a57b68486cff4da4f10019cb9c62c597a6d2861\cucumber-junit-1.2.4.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.security\spring-security-test\4.2.3.RELEASE\f93339f597290d85a596f25a7841b9109b79ced6\spring-security-test-4.2.3.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-validation\1.5.7.RELEASE\a2e87bf66a1db8037e1feeba7e7063996fa72090\spring-boot-starter-validation-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter\1.5.7.RELEASE\b42fe6962dd8b3be4ac8c1d7134d0ca2d0a35c7e\spring-boot-starter-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-context-support\4.3.11.RELEASE\a74bdcbce2b35804cdfdac1ca820820100b5fd9b\spring-context-support-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.data\spring-data-rest-webmvc\2.6.7.RELEASE\9a638b76b290b52a241b59a7341a042a063bad3d\spring-data-rest-webmvc-2.6.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.data\spring-data-rest-core\2.6.7.RELEASE\fe15f604fa9b35f16af608c05266fcba53a7d17c\spring-data-rest-core-2.6.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.hateoas\spring-hateoas\0.23.0.RELEASE\678ffa0798f417a794fea592dc8066e325611919\spring-hateoas-0.23.0.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.plugin\spring-plugin-core\1.2.0.RELEASE\f380e7760032e7d929184f8ad8a33716b75c0657\spring-plugin-core-1.2.0.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.security\spring-security-config\4.2.3.RELEASE\9f3771903616e33521836fd999d63efbfebf90d\spring-security-config-4.2.3.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.security\spring-security-web\4.2.3.RELEASE\7a03e737484ca232d7146852f06d067ac21427ac\spring-security-web-4.2.3.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-webmvc\4.3.11.RELEASE\814f91ec4dc324e724bbe7cbc5045b234604c539\spring-webmvc-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.security\spring-security-core\4.2.3.RELEASE\5c0e47a47036c94d6fdd02696bf52be6d1adbd4d\spring-security-core-4.2.3.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-test-autoconfigure\1.5.7.RELEASE\8bead16b97c9f1b5fe7c47f3e030dd9351a6974d\spring-boot-test-autoconfigure-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-test\1.5.7.RELEASE\b8d77445d48f4320f404d8f13f4e5cb95bc36b30\spring-boot-test-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-autoconfigure\1.5.7.RELEASE\e83f1407ff991e9e7eb156f49fa19d868bb73289\spring-boot-autoconfigure-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\1.5.7.RELEASE\1006870df7b3dc203df7ce84463c5dd7049fdaa4\spring-boot-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.ext\jersey-spring3\2.25.1\a31bfcd2fcae5beb979d3f41079b6f4020d6fbc9\jersey-spring3-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\spring-bridge\2.5.0-b32\f38ecef23edc769942a95c062efd63541044de42\spring-bridge-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-web\4.3.11.RELEASE\50a3dfce550bdf9459dfb2c6282aa104b040258e\spring-web-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-context\4.3.11.RELEASE\3efec2d7c7469d49108a2b21c3f15831f7297569\spring-context-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.media\jersey-media-json-jackson\2.25.1\19d1e4276eb7b6386640c344d9e5c01eba7eae5d\jersey-media-json-jackson-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.jaxrs\jackson-jaxrs-json-provider\2.8.10\f8837cf03871569eb02a491dea64929e5da14b4\jackson-jaxrs-json-provider-2.8.10.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.jaxrs\jackson-jaxrs-base\2.8.10\50a3d02a31cb427272a96fe696b5ac9442c7cfa1\jackson-jaxrs-base-2.8.10.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.module\jackson-module-jaxb-annotations\2.8.10\22df8efd0cff7661bf136148c73e2651748f7600\jackson-module-jaxb-annotations-2.8.10.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.8.10\f7b83cb2bc4b88d53961e749e1ad32f49ef017b7\jackson-databind-2.8.10.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations\2.8.0\45b426f7796b741035581a176744d91090e2e6fb\jackson-annotations-2.8.0.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.apache.tomcat\tomcat-jdbc\8.5.20\10d0d288d837ddfc92096920e4571054a3934160\tomcat-jdbc-8.5.20.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-jdbc\4.3.11.RELEASE\642ed7497d327f299caccb930af6b65e229864c4\spring-jdbc-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-tomcat\1.5.7.RELEASE\eae533e2a0111a51d812912acb3d3e2368736d1b\spring-boot-starter-tomcat-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.containers\jersey-container-servlet\2.25.1\cf5f7a76fcea38158b890ab7a0142d4db709a882\jersey-container-servlet-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.containers\jersey-container-servlet-core\2.25.1\400e30bb035a0cdf3c554530224141ce659a0d1e\jersey-container-servlet-core-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.ext\jersey-bean-validation\2.25.1\1971927d79cad0ad2b5a3bfda24967748a2023d\jersey-bean-validation-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.core\jersey-server\2.25.1\276e2ee0fd1cdabf99357fce560c5baab675b1a2\jersey-server-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-aop\4.3.11.RELEASE\461d0bb58f1de30203d9331adf4b51de9554de5a\spring-aop-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2\2.5.0-b32\c3accae585955e49c771d464899e906ecc9ffb4\hk2-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\config-types\2.5.0-b32\686bbe7f80b1b879d64c06bc6606c97721a795f2\config-types-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-config\2.5.0-b32\dce05ac4225dbc0c1c382ad02e3b5bee51f0168a\hk2-config-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.hibernate\hibernate-validator\5.3.5.Final\622a9bcef2eed6d41b5b8e0662c36212009e375\hibernate-validator-5.3.5.Final.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.jayway.jsonpath\json-path\2.2.0\22290d17944bd239fabf5ac69005a60a7ecbbbcb\json-path-2.2.0.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\junit\junit\4.12\2973d150c0dc1fefe998f834810d68f278ea58ec\junit-4.12.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.assertj\assertj-core\2.6.0\b532c3fc4f66bcfee4989a3514f1cd56203a33ad\assertj-core-2.6.0.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.mockito\mockito-core\1.10.19\e8546f5bef4e061d8dd73895b4e8f40e3fe6effe\mockito-core-1.10.19.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-library\1.3\4785a3c21320980282f9f33d0d1264a69040538f\hamcrest-library-1.3.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.3\42a25dc3219429f0e5d060061f71acb49bf010a0\hamcrest-core-1.3.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.skyscreamer\jsonassert\1.4.0\9cdbb373a06f6513e51e8c545ee6a5e981463edb\jsonassert-1.4.0.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-test\4.3.11.RELEASE\cf765b5d6d6bdc38c67522ecae3fe9549da43e1\spring-test-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-tx\4.3.11.RELEASE\c931ee363d2269f03edfeb83d7d7fe23924ab8a1\spring-tx-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.data\spring-data-commons\1.13.7.RELEASE\cf4538abda1f0a9cf2323d6e4204df0130aeec73\spring-data-commons-1.13.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-beans\4.3.11.RELEASE\591cc35fd39292adae13f01aa13e978d0bb11936\spring-beans-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-expression\4.3.11.RELEASE\fca662a2fccdad90ec22b2aaecb021047dcbe249\spring-expression-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-core\4.3.11.RELEASE\eb30ed093f628279d3aead068fd478fa343f1dad\spring-core-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-core\1.2.4\72790b1da44d8d3d2764c6aef29865ee228bbeb1\cucumber-core-1.2.4.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-logging\1.5.7.RELEASE\c84c4ea09d7f789a0d5e936a961471e5dbd9295e\spring-boot-starter-logging-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.yaml\snakeyaml\1.17\7a27ea250c5130b2922b86dea63cbb1cc10a660c\snakeyaml-1.17.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.8.10\eb21a035c66ad307e66ec8fce37f5d50fd62d039\jackson-core-2.8.10.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.slf4j\jcl-over-slf4j\1.7.25\f8c32b13ff142a513eeb5b6330b1588dcb2c0461\jcl-over-slf4j-1.7.25.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-classic\1.1.11\ccedfbacef4a6515d2983e3f89ed753d5d4fb665\logback-classic-1.1.11.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.slf4j\jul-to-slf4j\1.7.25\af5364cd6679bfffb114f0dec8a157aaa283b76\jul-to-slf4j-1.7.25.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.slf4j\log4j-over-slf4j\1.7.25\a87bb47468f47ee7aabbd54f93e133d4215769c3\log4j-over-slf4j-1.7.25.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api\1.7.25\da76ca59f6a57ee3102f8f9bd9cee742973efa8a\slf4j-api-1.7.25.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.apache.tomcat\tomcat-juli\8.5.20\6c0579d329696059f377585d2a51455bf083d9ee\tomcat-juli-8.5.20.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-websocket\8.5.20\4df9055507926a2651f691cc9964c50493a0ab29\tomcat-embed-websocket-8.5.20.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-core\8.5.20\b9026ee20f1f6a2b0fc3e51dd806d800901448b0\tomcat-embed-core-8.5.20.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-el\8.5.20\42960f481a8a80f4ffd3b865c2232820e3565bf1\tomcat-embed-el-8.5.20.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.core\jersey-client\2.25.1\4d563b1f93352ee9fad597e9e1daf2c6159993c6\jersey-client-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.media\jersey-media-jaxb\2.25.1\d7da0beeed5614a3bfd882662faec602699e24b\jersey-media-jaxb-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.core\jersey-common\2.25.1\2438ce68d4907046095ab54aa83a6092951b4bbb\jersey-common-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.ext\jersey-entity-filtering\2.25.1\4a5805060f796ec2c9bb1ba0ce91c1db6d889524\jersey-entity-filtering-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\javax.ws.rs\javax.ws.rs-api\2.0.1\104e9c2b5583cfcfeac0402316221648d6d8ea6b\javax.ws.rs-api-2.0.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\javax.annotation\javax.annotation-api\1.2\479c1e06db31c432330183f5cae684163f186146\javax.annotation-api-1.2.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-core\2.5.0-b32\8cb6a8a9522ec523b7740d29f555bdbe9d936af2\hk2-core-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-runlevel\2.5.0-b32\60dc979763ea885e796cba4d3e322af8d500ba74\hk2-runlevel-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-locator\2.5.0-b32\195474f8ad0a8d130e9ea949a771bcf1215fc33b\hk2-locator-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-api\2.5.0-b32\6a576c9653832ce610b80a2f389374ef19d96171\hk2-api-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2.external\javax.inject\2.5.0-b32\b2fa50c8186a38728c35fe6a9da57ce4cc806923\javax.inject-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\javax.validation\validation-api\1.1.0.Final\8613ae82954779d518631e05daa73a6a954817d5\validation-api-1.1.0.Final.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\aopalliance\aopalliance\1.0\235ba8b489512805ac13a8f9ea77a1ca5ebe3e8\aopalliance-1.0.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.jboss.logging\jboss-logging\3.3.1.Final\c46217ab74b532568c0ed31dc599db3048bd1b67\jboss-logging-3.3.1.Final.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml\classmate\1.3.4\3d5f48f10bbe4eb7bd862f10c0583be2e0053c6\classmate-1.3.4.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\net.minidev\json-smart\2.2.1\5b9e5df7a62d1279b70dc882b041d249c4f0b002\json-smart-2.2.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.objenesis\objenesis\2.1\87c0ea803b69252868d09308b4618f766f135a96\objenesis-2.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.vaadin.external.google\android-json\0.0.20131108.vaadin1\fa26d351fe62a6a17f5cda1287c1c6110dec413f\android-json-0.0.20131108.vaadin1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-html\0.2.3\624a0c986088e32910336dd77aee5191c04a8201\cucumber-html-0.2.3.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-jvm-deps\1.0.5\69ed0efe4b81f05da3c0bdc7281cbdc43f5ceb26\cucumber-jvm-deps-1.0.5.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\gherkin\2.12.2\17138631fa20fd0e44a13e50d6b7be59cee1a94\gherkin-2.12.2.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.atteo\evo-inflector\1.2.2\2551aad98d65ac5464d81fe05f0e1516cfe471c9\evo-inflector-1.2.2.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.bundles.repackaged\jersey-guava\2.25.1\a2bb4f8208e134cf2cf71dfb8824e42942f7bd06\jersey-guava-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\osgi-resource-locator\1.0.1\4ed2b2d4738aed5786cfa64cba5a332779c4c708\osgi-resource-locator-1.0.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-utils\2.5.0-b32\5108a926988c4ceda7f1e681dddfe3101454a002\hk2-utils-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2.external\aopalliance-repackaged\2.5.0-b32\6af37c3f8ec6f9e9653ec837eb508da28ce443cd\aopalliance-repackaged-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.javassist\javassist\3.21.0-GA\598244f595db5c5fb713731eddbb1c91a58d959b\javassist-3.21.0-GA.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\class-model\2.5.0-b32\17f054f3e91898c0c0fc52163ad904b13c24e8b\class-model-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\net.minidev\accessors-smart\1.1\a527213f2fea112a04c9bdf0ec0264e34104cd08\accessors-smart-1.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-core\1.1.11\88b8df40340eed549fb07e2613879bf6b006704d\logback-core-1.1.11.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2.external\asm-all-repackaged\2.5.0-b32\dc705f1d54cd5a96cbc5a473525e75ef1cb59a9e\asm-all-repackaged-2.5.0-b32.jar;C:\Users\xyz\.gradle\caches\modules-2\files-2.1\org.ow2.asm\asm\5.0.3\dcc2193db20e19e1feca8b1240dbbc4e190824fa\asm-5.0.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.example.restdemo.web.CucumberIntegrationTest
                    Feature: Testing a REST API
                    Users should be able to add two numbers
                    19:00:03.815 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Registering scope 'cucumber-glue' with implementation [cucumber.runtime.java.spring.GlueCodeScope@2a54a73f]

                    Test ignored.

                    Test ignored.

                    Test ignored.

                    Test ignored.

                    Scenario: number addition service # feature/addition.feature:4
                    When baseUri is /messages/add
                    Then result should be 3

                    1 Scenarios (1 undefined)
                    2 Steps (2 undefined)
                    0m0.000s


                    You can implement missing steps with the snippets below:

            @When("^baseUri is /messages/add$")
            public void baseuri_is_messages_add() throws Throwable {
                    // Write code here that turns the phrase above into concrete actions
                    throw new PendingException();
                    }

            @Then("^result should be (\\d+)$")
            public void result_should_be(int arg1) throws Throwable {
                    // Write code here that turns the phrase above into concrete actions
                    throw new PendingException();
                    }


                    Process finished with exit code 0

Answer:

Remove the class name from the glue attribute

@CucumberOptions(features = "classpath:feature/", glue = {"com.example.restdemo.web"}, format = {"pretty", "html:target/cucumber"})

Question:

I have multiple features in my project which each contain multiple scenarios.

I want to know if I can skip one or more of the features or scenarios from running (while the rest still run) based on a specific value set in my application.yaml. (or if there's any better way of doing it)

I thought I can add an And statement after the Given and check the property but I don't know how to skip the scenario within its implementation.

example:

I have a property called "type" which can be "apple" or "orange" and I have a feature with the following scenarios:

  • scenario A: to only run when type is apple
  • scenario B: to only run when type is orange
  • scenario C: will run regardless of the type

How can I do that ?


Answer:

  1. You can use Tag expressions to tag your features / scenarios. In your example: @apple, @orange
  2. You can use these tags to specify which features / scenarios you want to run. How to do so, depends on how you are running your tests, but you'll need to provide tags=@apple or tags=@orange depending on which you want to run, or omit tags if you want to run all of them.

Question:

I have a working integration test for my Spring Web MVC app that looks like this:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ShibaApplication.class)
@WebAppConfiguration
public class EchoControllerTests {

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Before
    private void setup() throws Exception {
        this.mockMvc = webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void echo() throws Exception {
        mockMvc.perform(get("/echo/blargh"))
            .andExpect(status().isOk())
            .andExpect(content().string("blargh"));
    }
}

Leaving that (successful) test in place, I tried to create an identical Cucumber test. The Cucumber runner is:

@RunWith(Cucumber.class)
@CucumberOptions(features="src/test/resources",
                 glue={"co.masslab.shiba", "cucumber.api.spring"})
public class CucumberTests {
}

The class that defines the Cucumber steps looks like:

@WebAppConfiguration
@Import(ShibaApplication.class)
@ContextConfiguration(classes=CucumberTests.class)
public class WebStepDefs {

    @Autowired
    private WebApplicationContext webApplicationContext;

    private MockMvc mockMvc;
    private ResultActions resultActions;

    @When("^the client calls the echo endpoint$")
    public void the_client_calls() throws Exception {
        Assert.notNull(webApplicationContext);
        this.mockMvc = webAppContextSetup(webApplicationContext).build();
        this.resultActions = mockMvc.perform(get("/echo/blargh"));
    }

    @Then("^the client receives a status code of 200$")
    public void the_client_receives_a_status_code() throws Exception {
        resultActions.andExpect(status().isOk());
    }
}

However, the cucumber test fails, as the result is not a 200 but a 404.

I suspect this is because the WebApplicationContext getting autowired into the WebStepDefs class isn’t the same as the one that gets autowired into the EchoControllerTests. I’ve been going over the Spring JavaConfig Reference Guide v1.0.0.M4, but I haven’t yet figured out where I’m going wrong.


Answer:

I kept trying different combinations of annotations, and finally figured this one out. The annotations for WebStepsDef that worked for me were:

@ContextConfiguration(classes=ShibaApplication.class, loader=SpringApplicationContextLoader.class)
@IntegrationTest
@WebAppConfiguration

Question:

I have a cucumber test that is throwing me an illegal access error when I try to fetch a bean. I haven't been able to figure out why this isn't okay.

@Given('^test$')
public void myTest(){
    ApplicationContext ctx = new AnnotationConfigApplicationContext(AppInjector.class);
    MyBean bean = ctx.getBean(MyBean.class)
}

java.lang.IllegalAccessError: tried to access class MyBean from StepDef

Answer:

The solution is to add

@ContextConfiguration(classes = AppInjector.class)

to the top of the class, and then use @Inject to pull in the dependency.