Scala+Gradle+Cucumber = A holy alliance


It has been some long time that I have not written a blog regarding my new technological learnings. And that is the void I will be wanting to fill in the coming days. This one being the start.

So, I have been working on work items that include Scala, Gradle and Cucumber together. That’s right. Together. And with all that I was able to learn/read over the internet, their togetherness has proved to be a bumpy ride for many. Well, initially, for me too. But as time passed and my interaction with these three beauties increased, I realized it’s not that bad too. In this blog, I am trying to demonstrate a simple setup of these three together. In future, I may want to expand on the complexity. But today it will be simple and useful.

All the setup will be done using IntelliJ.

  1. Make a new empty Gradle project in IntelliJ. It will give us a build.gradle file to which we need to add Scala plugin. This official link has the entire documentation.
    A portion of our build.gradle should look like

    apply plugin: 'java'
    apply plugin: 'scala'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        compile     'org.scala-lang:scala-library:2.11.8'
        testCompile 'junit:junit:4.12'
    }

    That’s it. This brings Scala in for us. And that’s simple.

  2. Now we only need Cucumber available for us. And to do this, we just need to add dependency. The one we will use for this project is this. And now, our build.gradle should look like
    apply plugin: 'java'
    apply plugin: 'scala'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    configurations {
        cucumberRuntime {
            extendsFrom testRuntime
        }
    }
    
    dependencies {
        compile     'org.scala-lang:scala-library:2.11.8'
        compile     'info.cukes:cucumber-scala_2.11:1.2.5'
        testCompile 'junit:junit:4.12'
        testCompile 'info.cukes:cucumber-junit:1.2.5'
    }
  3. Now that we have done the setup, it is time for some code. We will use the default project layer of src/main/scala and src/test/scala. The project that I have built for this is based on shopping store. The entire code is available here. The concept is simple. A user buys stuff from the store and pays the total amount including the taxes(if any). To understand what the code is trying to do, one should now always start from the feature file. That gives us the sense of business and is easy to read. Feature file comprises of scenarios. One of the scenarios looks like
    Scenario: As a sales representative, I want to calculate total amount that the customer pays when he/she only buys material exempted from tax
    
        Given a user is registered with the store
    
        And the user buys the book of 150 rupees
    
        And the user buys another book of 200 rupees
    
        And the user buys the food item of 150 rupees
    
        When the user asks for a bill
    
        Then the total bill user needs to pay is 500 rupees

    Now that’s easy English. And tells us that for various items bought, the total bill generated by the system will show the gross amount.

  4. The feature file requires a corresponding steps file which contains the code that needs to be executed when every step of the scenario is executed. The detailed explanation of steps file will be a part of a different blog. But you can still have a glance at it as it is included in the project.
  5. Now to run all this together using the command line, we need a Gradle task. Mine is
    task "store-runner"(type: JavaExec) {
        main = "cucumber.api.cli.Main"
        classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
        args = ['--plugin', 'pretty', '--glue', 'com.knoldus', 'src/test/resources']
    }
    

    We can see the description of all the arguments here.  ‘–glue’ specifies from where the glue code, in our case only the step files, are loaded from. ‘src/test/resources’ specifies where the feature files are to be found. And now simply executing the task ./gradlew store-runner will execute all the feature files.
    Another way to execute the feature files is through the IDE only, in our case, IntelliJ. This will need a JUnit Runner class where we provide the required arguments.

    import cucumber.api.CucumberOptions
    import cucumber.api.junit.Cucumber
    import org.junit.runner.RunWith
    
    @RunWith(classOf[Cucumber])
    @CucumberOptions(
    features = Array("src/test/resources")
    )
    class StoreRunner {}

    And now we can simply right click on the class and execute the class which will execute all our scenarios.

And this is it. This is a simple example with the intent to get started with ScalaGradleCucumber. There can be a lot of expansions in this code itself and I myself will be playing with it in the future and so can you. Any suggestions/questions will be appreciated. And I will try to come with the next blog sooner. Till then…Happy Coding!!


knoldus-advt-sticker

Advertisements
This entry was posted in Scala. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s