Tag Archives: eclipse

Cucumber JVM: Mocking

In the previous cucumber-jvm post I introduced dependency injection, which makes it possible to use mocks during testing. Usually, it is a bad idea to introduce mocks in cucumber scenarios, because they are supposed to test the whole system as it is, however there are cases when mocking comes in handy: for example, a module or component of your system communicates with a 3rd party system. In this case, running the scenarios may be difficult, and the best option here is to mock or simulate that 3rd party system so that your application or product can still be tested. Technically, it is really easy to use mocks with cucumber-jvm, but there are certain limitations, which you’ll see at the end of this post.

Let’s say that the munger functionality in our SimpleTextMunger application is a 3rd party system, which communicates through the network, but that network is not reachable from our test system. Here is the current code:

public String execute(String sentence) {
  List words = sentenceHelper.split(sentence);
  for (int i = 0; i < words.size(); i++) {
    words.set(i, munger.munge(words.get(i)));
  }
  return sentenceHelper.join(words);
}

In order to mock the calls of the munger object we need something like this – I’m going use mockito as a mocking framework:

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
// ...
when(munger.munge(inputWord)).thenReturn(mungedWord);
// ...

Read more »

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)

Cucumber JVM: Dependency Injection

Last time I finished with a failing test case which drove the development to a phase where I had to deal with a sentence instead of a word. The fix was not a big deal, but I ended up with a method in SimpleTextMunger which did three things:

// ...
public String execute(String sentence) {
  StringBuilder mungedSentence = new StringBuilder();
  for (String word : sentence.split(" ")) {
    mungedSentence.append(mungeAWord(word)).append(" ");
  }
  return mungedSentence.toString().trim();
}
 
private String mungeAWord(String word) {
  // ... prior content of the execute(String) method
}
  1. convert the sentence into words
  2. munge a word
  3. collect the munged words and create the new sentence

The first and the third one belong together because they work with a sentence, so I created two classes – SentenceHelper and Munger -, which are used by the SimpleTextMunger:

public class SimpleTextMunger {
  public String execute(String sentence) {
    SentenceHelper sentenceHelper = new SentenceHelper();
    Munger munger = new Munger();
 
    List words = sentenceHelper.split(sentence);
    for (int i = 0; i < words.size(); i++) {
      words.set(i, munger.munge(words.get(i)));
    }
    return sentenceHelper.join(words);
  }
}
 
class Munger {
  public String munge(String word) {
    // the same as before
  }
}
 
class SentenceHelper {
  public List split(String sentence) {
    // ...
  }
 
  public String join(List words) {
    // ...
  }
}

One day, I’d like to write unit tests, mocks – you know, the whole package -, so I must move those two new statements out of the scope of the execute method. Fortunately, I already have spring, so I’m just going to hack a solution together to see whether it works or not. I can do it now, because all the scenarios are green.

This is the best thing about scenarios and BDD, that I can do experimenting, spiking, refactoring or anything else without changing the test cases. Try to do it with unit tests: the moment you move something, the mocks will turn red and you have to do a lot of unnecessary work by fixing them. Read more »

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)

Cucumber JVM: More Scenarios

In my previous post I started to play around with cucumber-jvm to see how it could be used for Java development.  Last time I finished with one scenario and now I’m going to move forward with my simple text munger implementation and see how to use cucumber-jvm with multiple scenarios.

Before doing anything, I installed infinitest so that I didn’t have run the test cases manually. This really sped up the development. I recommend you to do the same.

I filled out my steps, did a bit of implementation and now this is how the steps look like:

Quick Detour – Playing Around With Step Definitions

The ruby version of cucumber is quite flexible. For example, Given, When and Then are not bound to the step definitions, so the following works:

Feature: simple text munger kata
 
Scenario: Do nothing with a two-letter word
   Then I have an instance of my class
  Given I call my method with "an"
   When I receive "an"

The good news is that it also works with cucumber-jvm, and there is more! Sometimes you have to give multiple steps with the same Given, When and Then prefix. In this case you can use the And prefix. So instead of multiple Thens:

Feature: simple text munger kata
 
Scenario: Do nothing with a two-letter word
   Then I have an instance of my class
   Then I call my method with "an"
   Then I receive "an"

Use one Then with several Ands:

Feature: simple text munger kata
 
Scenario: Do nothing with a two-letter word
   Then I have an instance of my class
    And I call my method with "an"
    And I receive "an"

These formats are ugly and I don’t recommend using them, but the goal of my posts is to show the capabilities of cucumber-jvm, and so far it has all the features the ruby version has. Read more »

VN:F [1.9.17_1161]
Rating: 6.7/10 (3 votes cast)

Cucumber JVM: Preparation

Every time I wrote some code in ruby and executed our 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 was right for me: either it used another language – like groovy -, had some unnecessary dependencies, or was 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 – author 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 what it can do.

The Basics

If you are not familiar with the BDD concept I recommend to read Dan North’s article before continuing. BDD means Behavior Driven Development, and in a nutshell, it means that you specify on a higher level, in a readable form, how the system is 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 100 percent true: for me TDD defines what an entity – system or class – should exactly do, and BDD defines how it should work. There is a tiny difference, but it is recognizable.

Let’s see a very simple example:

Feature: simple text munger kata
Scenario: Do nothing with a two-letter word
  Given I have an instance of my class
   When I call my method with "an"
   Then I receive "an"

The snippet above is self-explanatory: what my feature is supposed to do is described in Gherkin language. Read more »

VN:F [1.9.17_1161]
Rating: 7.5/10 (2 votes cast)