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.
- Make a new empty Gradle project in IntelliJ. It will give us a
build.gradlefile to which we need to add Scala plugin. This official link has the entire documentation.
A portion of our
build.gradleshould look like
That’s it. This brings Scala in for us. And that’s simple.
- 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.gradleshould look like
- Now that we have done the setup, it is time for some code. We will use the default project layer of
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.
- 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.
- Now to run all this together using the command line, we need a Gradle task. Mine is
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-runnerwill 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.
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!!