Every time I wrote some code in Ruby and executed we cucumber features I craved for something similar in Java. I knew that there were several BDD frameworks for Java like JBehave, Jdave, and EasyB, but none of them is right for me: Either it used another language-like groovy-, had some unnecessary dependencies, or is hard to integrate with Eclipse. A couple of weeks ago, I was looking for something in the source code of cucumber and found out that Aslak Hellesøy-aut Hor of cucumber-is working on a pure Java based implementation. It hasn ' t been released yet, but I decided to give it a-try and see how it can do.
The Basics
If you aren't familiar with the BDD concept I recommend-read Dan North ' s article before continuing. BDD means Behavior Driven development, and in a nutshell, it means, specify-a higher level, in a Reada Ble form, how the system was supposed to work. Programmers tend to call BDD the Big brother of TDD, because TDD works on a class level, and BDD on the system level, but This isn ' t percent true:for me TDD defines what 's Entity-system or class-should exactly do, and BDD def Ines How it should work. There is a tiny difference, but it is recognizable.
Let's see a very simple example:
Feature:simple text Munger Katascenario:do Nothing with a two-letter word Given I had an instance of my class W Hen I call my method with ' an ' then I receive '
The snippet above is Self-explanatory:what My feature are supposed to do are described in Gherkinlanguage.
The Test Classes
When I try out a new framework, I use a Kata exercise. This time, I'll try to implement a simple version of the famous text Munger kata. Given an input sentence, modify it in the following way:in each word keep the first and the last letter, but the rest Sha ll be returned in reverse order-in the original kata the rest should being randomized. For example:
In:and the spice must flow
Out:and the Scipe msut folw
As can see, it'll be a simple, because my goal are to learn how to use cucumber under java-i ' ll refer to it as CU CUMBER-JVM -, not to finish the kata exercise properly. Enough talking, let's take the out of our eclipse and start working.
The first thing is to save the feature above into a .feature
file where cucumber can find it. I ' ll use the standard directory layout and create a simple_text_munger.feature
file with the content above in src/test/resources
:
Every feature should has its own .feature
file. A feature May has multiple scenarios, and every scenario may have multiple steps. A step can is used in different scenarios and even across different features. This is not CUCUMBER-JVM specific, the is how cucumber organizes its files.
Have a single .feature
file with no use. Somehow the system should interpret this file and execute the referenced steps:
Simpletextmunger_test.java
Package Com.zsoltfabok.blog; Import Cucumber.junit.Cucumber; Import cucumber.junit.Feature; Import Org.junit.runner.RunWith; the @RunWith (Cucumber. class = "Classpath:simple_text_munger.feature")publicclass Simpletextmunger_test {}
Java is isn't a dynamic language like Ruby and it cannot "execute" a plain .feature
file:it requires a wrapper file which load s the feature and executes it. Based onThe examples of CUCUMBER-JVM, Let's name this file after the and use the .feature
_Test
suffix. The underscore differentiates the file from a regular Test file (update: After writing a couple of more Features, I felt more comfortable with Feature
the suffix, but for this example I kept the in _Test
order keep the Consisten Cy between the posts and the source code). There is no need to put anything into the body of the wrapper class.
In Cucumber, every "sentence" are considered as a step which needs to be implemented. For example:
Simpletextmungerstepsdef.java
ImportCucumber.annotation.en.Given;ImportCucumber.annotation.en.Then;ImportCucumber.annotation.en.When; Public classsimpletextmungerstepsdef {@Given ("^i has an instance of my class$") Public voidI_have_an_instance_of_my_class () {//Express The Regexp above with the code wish you had} @Then ("^i receive \" ([^\ "]*) \" $ ") Public voidi_receive_ (String arg1) {//Express The Regexp above with the code wish you had} @When ("^i call my method with \" ([^\ "]*) \" $ ") Public voidi_call_my_method_with_ (String arg1) {//Express The Regexp above with the code wish you had }}
Exactly like in cucumber, CUCUMBER-JVM maps Each 'sentence' to a step. When I run my .feature
, it'll call the proper step method. If a step definition cannot be found, CUCUMBER-JVMwould generate it for you and write it to the console. Mind the suffix in the name of the Stepdef
class. It is not mandatory-everything works without it, because CUCUMBER-JVM finds the definitions using reflection -but it's looks like a reasonable convention.
Compiling
The test classes is in place and let's run the test cases. We ll need the jar files of CUCUMBER-JVM. They can installed with Maven, but since I ' am not a huge fan of Maven, and I need something easy-to-use-this example , I ' m going with ivy. You can download and set the dependencies using the command line or the Ivyde Eclipse plugin:
BLOG.CUCUMBERJVM%Ivy ... confs: [Compile, Test] found Junit#junit;4.10 in Publicfound Org.hamcrest#hamcrest-core;1.1 in Publicfound Info.cukes#cucumber-java;1.0.14 in Publicfound Info.cukes#cucumber-core;1.0.14 in Publicfound Info.cukes#cucumber-jvm-deps;1.0.3 in Publicfound Info.cukes#gherkin;2.11.2 in Publicfound Info.cukes#gherkin-jvm-deps;1.0.2 in Publicfound Info.cukes#cucumber-junit;1.0.14 in Public:: Resolution Report:: Resolve 519ms:: Artifacts DL 26ms--------------------------------------------------------------------- | | Modules | | Artifacts | | conf | number| search|dwnlded|evicted| | number|dwnlded| --------------------------------------------------------------------- | Compile | 8 | 0 | 0 | 0 | | 14 | 0 | | Test | 8 | 0 | 0 | 0 | | 14 | 0 | ---------------------------------------------------------------------BLOG.CUCUMBERJVM%
Run the first Test
No compilation errors, let ' s run the as SimpleTextMunger_Test
a JUnit test. It looks good to me:
In the next post I ' m going to continue with more scenarios and see how the CUCUMBER-JVMhandles tables in a .feature
File. Until then, you can find the source for this post under the Episode_1 branch of the repository on GitHub. Stay tuned!
Cucumber (a): preparation