Wednesday, 18 September 2013

Adding cucumber-jvm to projects

Add these 2 dependencies in the pom.xml

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.1.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.1.2</version>
        <scope>test</scope>

    </dependency>


In the Test add a acceptance folder


Then Add the following class

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber-html-report", "json-pretty:target/cucumber-json-report.json"}, tags = "~@wip")
public class RunCukesTest {
}


Now when you run 

>mvn test


It will complain that there are no feature file

Now in the resources folder based on your package structure create acceptance folder and then add a sample feature

Feature: Testing for sample core

Scenario Outline: Successful login

        Given I want to use the browser [browser]
        When I type testuser1 in the username input field
        When I type testuser1 in the password input field
        And I click the login button
        Then I am on the launcher page

        Examples:
                |     browser            |
                |     Firefox            |
                |     Chrome             |
                |     InternetExplorer   |

Now when you run >mvn test

it will ask us to implement the feature


You can implement missing steps with the snippets below:

@Given("^I want to use the browser \\[browser\\]$")
public void I_want_to_use_the_browser_browser() throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

@When("^I type testuser(\\d+) in the username input field$")
public void I_type_testuser_in_the_username_input_field(int arg1) throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

you have configured cucumber-jvm to your projects


Adding Apache log4j

in pom.xml add the log4j dependency

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>

    </dependency>

in resources add a new file log4j.properties

# Root logger option
log4j.rootLogger=debug, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n


Now you should be able to log in messages


For mockito add this dependency

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>

        </dependency>


We can have exception rules as

    @Rule

    public ExpectedException expectedEx = ExpectedException.none();

    @Test
    public void shouldAcceptOnlyIncrementOf100Messages() {
        expectedEx.expect(IllegalArgumentException.class);
        expectedEx.expectMessage("The number of messages per seconds must be multiple of 100 )");
        new FixedRateMessageInjector(queueDriver, 108, 2);
    }


In case if you want to use AWS add

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.4.1</version>

        </dependency>