Java Deep

Pure Java, what else

Given, When, Then…

The words given, when, then should ring the bell for anybody, who was writing BDD tests. If not, then you should read a recent article of Martin Fowler.

The structure of a test as for BDD has three parts:

  • GIVEN the part, that describes the structure of the system before the tests starts. The test itself “describes” this state with a series of setup commands to move the system under test (SUT) into the state that is prerequisite of the test.
  • WHEN is the part that triggers the SUT. The triggers are usually method calls to the SUT and ideally contains only a few calls that are necessary to test the feature of the system that is the tested feature of the system.
  • THEN test that the state of the SUT has changed appropriately and shows the observable changes that the test is focusing on.

As Martin Fowler writes “Some people like to put Given-When-Then as comments to mark informal blocks inside unit tests.” I also followed this approach and I was busy writing //GIVEN, //WHEN, //THEN comments, but I was always bothered by comments. I am not excluding comments from my programming arsenal, but many times when I feel to write comments, especially inline comments, I can find better ways. Many times I find it to be a better approach to move even a single line, not to mention two or more lines, into a separate method with a speaking name and calling the private method from the place where the lines originally were. This proves to be much better in most cases than writing comment on the lines.

What is it then with the //GIVEN, //WHEN, //THEN comments? Is there any way to express the test better?

Certainly there are better ways. One way is to use some framework to express your tests, like Cucumber, or JBehave or if you use some non-Java language some other framework as described on the wiki page of Behavior driven development. I personally recommend to devote the time and learn those frameworks, and use one that fits your taste. Just as in the general case: time invested into testing in any form is never wasted.

Time invested into testing is never wasted.

But what to do if you just write some simple test project. Something of a few classes that does not justify the use of any such test frameworks. Of course, you still need testing, as there is only one project that does not need testing: the null project (or /dev/null for old school gentlemen). So you just start a very small project, say a half afternoon, and you start to write your code (of the unit test at first place of course). Should you write //GIVEN, //WHEN, //THEN comments?

Well, your taste. Recently I realized that I can also write the GIVEN, WHEN, THEN as labels in front of the blocks that contain the ‘given’, ‘when’ and ‘then’ code. Do not feel surprised, if you have never heard of labels in Java. Most Java programmers did not. They are stone age residues, totally useless and are kept for compatibility reason. I opt that they are in the language by mistake inherited from C, Perl and other fossils. However, if they are there, why not use them? The example code

	@Test
	@SuppressWarnings("unused")
	public void given_AnACL_when_SubjectQueryAFunction_then_GivesPermisssion() {
		final Subject subject;
		final AccessControlList acl;
		final Function function;
		GIVEN: {
			subject = mock(Subject.class);
			function = mock(Function.class);
			acl = new AccessControlList.Builder().add(
					new AccessControlElement.Builder().subject(subject)
							.function(function).build()).build();
		}
		final boolean permitted;
		WHEN: {
			permitted = can(subject).using(acl).execute(function);
		}
		THEN: {
			assertThat(permitted, is(true));
		}
	}

shows you the idea. Is it better than

	@Test
	public void given_AnACL_when_SubjectQueryAFunction_then_GivesPermisssion() {
		final Subject subject;
		final AccessControlList acl;
		final Function function;
		// GIVEN
		subject = mock(Subject.class);
		function = mock(Function.class);
		acl = new AccessControlList.Builder().add(
				new AccessControlElement.Builder().subject(subject)
						.function(function).build()).build();

		final boolean permitted;
		// WHEN
		permitted = can(subject).using(acl).execute(function);

		// THEN
		assertThat(permitted, is(true));

	}

? Matter of taste. Your opinion?

About these ads

5 responses to “Given, When, Then…

  1. Norbert Csík December 7, 2013 at 10:02 pm

    Well, I think using barely known language elements aren’t good for code readability. However, the structure of the code is better than before.
    I would prefer the inline comments.

    Like

  2. stokito December 8, 2013 at 11:48 am

    I would like to recommend you Spock. It looks like exactly what you need.

    https://code.google.com/p/spock/wiki/SpockBasics

    class HelloSpock extends spock.lang.Specification {
    def “length of Spock’s and his friends’ names”() {
    expect:
    name.size() == length

    where:
    name | length
    “Spock” | 5
    “Kirk” | 4
    “Scotty” | 6
    }
    }

    Like

  3. lukaseder December 12, 2013 at 8:18 am

    That’s quite clever, Peter! I had never thought of using labels this way.

    I love labels, by the way. They’re the window functions / grouping sets of Java. A device whose mystery is only exceeded by its power ;-)

    Like

  4. tomekkaczanowski January 2, 2014 at 12:21 pm

    Interesting idea to use labels – I have never thought of this. I use inline comments cause my whole team like this, however sometimes blank lines are enough.
    BTW. If you interested in such things you might enjoy my latests (free) book “Bad Tests, Good Tests” (at http://practicalunittesting.com). It includes many examples of imperfect tests and discusses how they could be improved. Cheers!

    Liked by 1 person

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

Follow

Get every new post delivered to your Inbox.

Join 946 other followers

%d bloggers like this: