Test Driven Development Integration Test

Beside unit tests, it is a good idea to have some integration tests. An integration test only makes sure that the new feature is in place. An integration test does not ponder on edge cases. It’s crucial to keep an integration test as simple as possible.

Make the Greeter App Modular

The greeter app from the Test Driven Development is not very modular and hence it is not possible to write integration tests for the new greeter app. In fact, in the first TDD blog post we only could write a system test to check if reading from the keyboard is working.

For the integration test, we need a ReadAndGreet module where we read from a scanner and return the greeting text. Let’s transform the greeter app from the second test driven development post

class Hello {
    void main() {
        Scanner input = new Scanner(System.in);
        String name = input.next();
        System.out.println("Hello " + name);
    }
}

to

public class ReadAndGreet {
    private Scanner scanner;

    public ReadAndGreet(Scanner scanner) {
        this.scanner = scanner;
    }

    public getGreeting() {
        return "Hello " + scanner.next());
    }
}

and

class Hello {
    void main() {
        Scanner input = new Scanner(System.in);
        readAndGreet = new ReadAndGreet(input);
        System.out.println(readAndGreet.getGreeting());
    }
}

Write a Failing Integration Test

And now we can write a simple failing integration test for the not yet integrated Greeter feature, which says "Good morning", "Good afternoon" or "Good evening" depending on the day time.

public class ReadAndGreetIntegrationTest {
    @Mock
    Scanner scanner;

    @Test
    void shouldGreetTomWithGoodMorning() {
        when(scanner.next).thenReturn("Tom");
        ReadAndGreet readAndGreet = new ReadAndGreet(scanner);
        assertEquals("Good morning Tom", readAndGreet.getGreeting())
    }
}

Perfect works… but wait, what if the test runs in the afternoon? It will fail and we don’t want to test the edge cases. So we need to relax the test a bit, we are only interested in "Good" and "Tom" and omit the in-between "morning", "afternoon" and "evening".

public class ReadAndGreetIntegrationTest {
    @Mock
    Scanner scanner;

    @Test
    void shouldGreetTomWithGoodMorning() {
        when(scanner.next).thenReturn("Tom");
        ReadAndGreet readAndGreet = new ReadAndGreet(scanner);
        assertThat(readAndGreet.getGreeting(), MatchesPattern("^Good .* Tom$", ))
    }
}

Now we have a failing and relaxed integration test because we don’t have our Greeter in place yet. That is the next step

public class ReadAndGreet {
    private Scanner scanner;
    private Greeter greeter;

    public ReadAndGreet(Scanner scanner) {
        this.scanner = scanner;
        this.greeter = new Greeter();
    }

    public getGreeting() {
        return greeter.sayHelloTo(scanner.next());
    }
}

And the integration test is happy. You have now a test in place that will act as an alarm if somebody accidentally removes the Greeter from your code. Or if somebody replaces the Greeter with the old "Hello" greeter functionality. Furthermore writing tests forces you to make your code modular which leads to more reusable code, think of a different scanner than the standard input. So again testing helps you in the microarchitecture and favors simple code over complex code.
If you have questions or improvements or any other kind of suggestions let me know in the comments below.

If you are interested in an online or onside workshop for test driven development write me an email artificials_ch@gmx.ch

Test Driven Development Fixtures

We all tend to write helper classes for testing, which clutters the test code and makes it harder to understand. A test should be like a story that you simply can read from top-down. I like test story better than fixture and I will use story and fixture interchangeable.

Why a Story Is More Powerful Than a Helper

If you have helpers you have code somewhere else, most often at the end of the test or even in another class where you hold all these helper utilities. If I read a test and I see method calls I tend to think that this is the code I test, but it is not. Helper utilities are also a sign that you probably need these utils also in your code and you should have tests for these utilities. As well helpers break the flow of your test story, it is like a reference to another story which you also should read before you can go on with this story. How awkward would that be in a book? Well, it is awkward as well in tests.
Unfortunately, this is hard to teach and to make visible why helper utilities are not a good thing.
When I write a test, I start with one single test and pack everything in that test I need, like calling methods, filling in entities, whatever is needed. Then I write a second test and do the same and after the third test, I put all the similarities into the setup method which is called before every test. And this holds the story. In Java JUnit5 it is the @BeforeEach annotation.

Let’s Jump Into The First Story

We base this blog article on the Test Driven Development Mantra. If not done yet read that first as it will give you a better understanding of the test refactoring we are going to make now.

We have two tests from that article (and maybe you did the exercise and wrote the next test with for "good afternoon")

  @Mock
    private DateTime dateTime

@Test
    void shouldSayGoodMorningToTom() {
        when(dateTime.getHourOfDay()).thenReturn(6);
        assertEquals("Good morning Tom", sayHelloTo("Tom", new DateTime()));
    }

   @Test
    void shouldSayGoodMorningToJerry() {
        when(dateTime.getHourOfDay().thenReturn(6);
        assertEquals("Good morning Jerry", sayHelloTo("Jerry", new DateTime()));
    }

The similarities are very obvious here and you can make a good morning fixture out of it like this

public class GoodMorningTest {
    @Mock
    DateTime dateTime;

    @BeforeEach
    void setUp() {
        when(dateTime.getHourOfDay().thenReturn(6);
    }

@Test
    void shouldSayGoodMorningToTom() {
        assertEquals("Good morning Tom", sayHelloTo("Tom", new DateTime()));
    }

   @Test
    void shouldSayGoodMorningToJerry() {
        assertEquals("Good morning Jerry", sayHelloTo("Jerry", new DateTime()));
    }
}

That’s it. You can read it top-down without any distractions. You have all together which makes tests very easy to read. Avoid helpers at all cost, it asks for troubles and confused programmers. And I’m well aware that this here is a very simple example, but honestly, most often it is not much more complicated than this here, test driven development favors simplicity.

Let me know if you have suggestions or questions in the comments below.

If you are interested in an online or onside workshop for test driven development write me an email artificials_ch@gmx.ch

Test Driven Development Mantra

This is the mantra

  • Think of a piece of code you want to add
  • Write a test which fails because that piece of code is not in place
  • Write that piece of code to make the failing test succeed
  • Refactor the code
  • Refactor the test

and you do that over and over again. Why did I not start with writing a test which fails? Because if you think for the next test without having code in your mind it’s incredible hard to figure out especially in the begining. This first step is a help and that works surprisingly well for me and for the people I taught test driven development with my workshops. It also keeps the steps as small as possible and in my opinion, that’s key if you want to test all the aspects of your new class.
And I do refactor the test as well and organize that around stories mostly referred to as fixtures. I will write about fixtures in one of the next blog posts about test driven development.

Simple App

Let’s carry on with the greeter app from the last blog post

Gretter.java

public class Greeter {
    public String sayHelloTo(String name) {
        return "Hello " + name;
    }
}

Hello.java

class Hello {
    void main() {
        Scanner input = new Scanner(System.in);
        String name = input.next();
        System.out.println(new Greeter().sayHelloTo(name));
    }
}

On the last meeting with the customer, we agreed that our greeting app must greet different depending on the daytime. Or more precisely our greeting app must say "Good morning" after 12 am, "Good afternoon" after 12 am and "Good evening" after 6 pm.

Practice

I will guide you to know how to write the needed code test driven step by step.

First Failing Test

What could be the next line of code we want to write? The easiest way ist to have an additional method in the Greeter class like this

public String sayHelloTo(String name, DateTime time) {
    return null;
}

We don’t write this, it’s just that we think of it. That is the first step. But wait… how does the failing test look like if that code is not even in place? Well it doesn’t compile and that is the failing test and it looks like

public class GreeterTest {

    @Test
    void shouldSayGoodMorningToTom() {
        sayHelloTo("Tom", new DateTime());
    }
}

Make the Test Happy

To make it compile we simply add the piece of code we thought about above and it compiles and even works.

Skip Refactor The Code and The Test

In this case, we don’t need to refactor the code or the test as there is nothing to improve so far. We skip these two steps in the mantra consciously.

Next Piece Of Code

The next piece of code is probably something like this

    public String sayHelloTo(String name, DateTime time) {
        return "Good morning " + name;
    }

but… resist writing the code!

Let’s Think of The Next Test

What could be the next test which fails? Exactly we add now an assertion for this. And here it goes

    @Mock
    private DateTime dateTime
    @Test
    void shouldSayGoodMorningToTom() {
        when(dateTime.getHourOfDay()).thenReturn(6);
        assertEquals("Good morning Tom", sayHelloTo("Tom", new DateTime()));
    }

I made already all the proper assumptions to say good morning and mocked the DateTime to return 6 in the morning if getHourOfDay is called.

And it will fail because our code just returns null.

Make The Second Test Happy Too

This is now really important and it is the only way to test all edge and aspects of your application in a test driven way. What do you think is the minimal code you need to make the app say "Good morning Tom"? What if I say just return exactly that? I mean literally this static string?

    public String sayHelloTo(String name, DateTime time) {
        return "Good morning Tom";
    }

I give you some time to digest… and while digesting run the test to see it is green. I mean it! Hit the damn button and run it.

And yes I know it is not our initial piece of code we thought of, but that is ok because that only helps us to find the next failing test. It’s a help and actually optional if you can do that without, I can not.

What Next?

The first thing we want to go rid of is this super static string. I guess that is your inner urge. Well then let’s think of a less static string, like this

    public String sayHelloTo(String name, DateTime time) {
        return "Good morning " + name;
    }

But… don’t write this code, just think of this code, because we need first a failing test!

Let it Fail Again

But how can we make the test fail? We simply write a new test with a different name, boom!

    @Test
    void shouldSayGoodMorningToJerry() {
        when(dateTime.getHourOfDay().thenReturn(6);
        assertEquals("Good morning Jerry", sayHelloTo("Jerry", new DateTime()));
    }

Isn’t that amazing? So simple to think of the next failing test if you keep thinking of a small portion of code.

That’s It For Now

What are the next steps for daytime addition to making the app say "Good afternoon Tom" if the getHourDay() method returns 14?

Write in the comment about how you would do that. What is the code you think of and what is the test which fails because that piece of code is not yet there? And finally what is the code to make the test happy.

If you are interested in an online or onside workshop for test driven development write me an email artificials_ch@gmx.ch

Test Driven Development Get Started

The start is always the hard part. Most often we face existing code where we have to add some new functionality. In always all cases there are little to no unit tests, but ugly and complicated looking integration or system tests which sometimes fail because of timing, network, or database issues. If you have already had a huge pile of unit tests in place, then you can stop reading. For the rest of us, proceed.

The Greeting App

Let’s dive into a simple example to illustrate how to get out of the mess. We have a very simple say hello app

class Hello {
    void main() {
        Scanner input = new Scanner(System.in);
        String name = input.next();
        System.out.println("Hello " + name);
    }
}

Just something which says hello. Simple. Can you test that? Well yes, you can start it and see if it says "Hello Tom" if you enter "Tom". Maybe there is a shell script based system test which tests that the app says hello to everyone. So whenever you improved your code you would have to start the system tests afterward. Nothing wrong with that. But it is slow to start the Java app and depending on what else you need, like a database or a REST call, you would also have to maintain the surrounding stuff with test data. Network and database add more failure points to the tests. The network could be down, the database could be in a funny state because your workmate did change something for his new feature and deleted all your test users. So system tests tend to be a fragile. I use system tests as well, but as simple as possible and as few as possible.

Refactor It to Make It Testable

To be able to test that with unit tests we have to refactor this class and put the greeting part into a class. Like this

public class Greeter {
    public String sayHelloTo(String name) {
        return "Hello " + name;
    }
}

And the main class would then just call this Greeter to say hello.

class Hello {
    void main() {
        Scanner input = new Scanner(System.in);
        String name = input.next();
        System.out.println(new Greeter().sayHelloTo(name));
    }
}

So you extracted the functionality into a class that you can test. That’s the way to go to have unit tests. This also helps you on a micro level to organize your code in a reusable way.

Unit Test

And the unit test for the Greeter would look like this

public class GretterTest {
    @Test
    void shouldGreetTom() {
        assertEquals("Hello Tom", new Gretter().sayHelloTo("Tom"));
    }

void shouldGreetJerry() {
        assertEquals("Hello Jerry", new Gretter().sayHelloTo("Jerry"));
    }
}

Yes, I have two tests. If I do that test driven I would start with a null to make the first test fail, then I would write a fixed string to greet Tom and then I would add the Jerry test to replace the fixed string. But more on that in the next blog post.

System Test

So now we are on a level where we can write unit tests for this greeting software. We will have one simple system test in place as well, just to see if it says something with Tom in it. We are only interested in the keyboard input is proper. Could look like this

#!/bin/bash
java -jar greetingApp.jar < Tom | grep "Tom"
exit $?

If the keyboard input does not work it will fail as the grep would return something different than 0. Why didn’t I just write this system test you might ask? Because it is just a matter of time when your customer wants that different. They might want the software to be aware of the time and greet you with "Good morning Tom", "Good afternoon Tom", "Good evening Tom" stead just "Hello Tom". So I only test the input of "Tom" that’s it, the edge cases I try to do on the lowest level possible and not on the highest level. It keeps your tests stable and very very fast.

The End

For now, I already prepared the next blog post where I introduce you to the test driven development mantra. Follow me on twitter @ia97lies and on @artificials_ch. Leave a comment below.

If you are interested in an online or onside workshop for test driven development write me an email artificials_ch@gmx.ch

Test Driven Development is Pretty Cool

A couple of years ago I stumbled over one of the best articles about test-driven development I ever read. Even the title is crispy I mean Stepping Through the Looking Glass: Test-Driven Game Development how cool sounds that? Noel did a decent job to explain the mantra write a test, see it fail, write code, see it proceed, refactor test, refactor code. There are 3 parts and they are worth reading it. And you will not believe but those articles are over ten years old and still relevant still fresh, still entertaining.

My first step was to figure out how to write tests before I write the code. I quickly realized it is not about write all tests upfront but write a test for a small functionality. Still, I had quite some trouble to figure out what kind of functionality I should test next. I learned some tricks to be very efficient with that. And how important it is that the tests fail first because only then you know that it will fail, if somebody breaks your code.

Another topic is the fixture. It took me some time to wrap my head around that fixture thing but it was one of the biggest beneficial ideas I’ve ever heard. A fixture or I also call it sometimes "The Story" of a set of tests helps you to organize your tests code and keep them understandable. Fixtures are part of the refactoring, which I do after I have two or three tests in place. Fixtures are much better for the understanding than helper code. Helpers look nice at first glance but can be a pain to understand what you exactly test in the long run. Helper code is often a sign that you should improve your interface with this functionality or just reorganize the test code around a fixture instead.
I also sometimes have a base fixture to have a higher level story which I can inherit. I don’t write tests for that fixture, else they will be executed for all inherited test class, that makes no sense. The base fixture reduces the copy past of code into different fixtures, which most often lead to helper methods and classes.

There are so many lessons I learned doing software test-driven. Code that is developed test-driven does look significantly different than code which is not tested or tested after the implementation. TDD helps you to keep a class simple, understandable, and testable. It’s just a pain to test complicated methods and classes, you automatically want to keep that simple. And you use your implemented code right away, you don’t need to fire up the application to see if your code does what you expect, no browser involved no server start, just the test. Test-driven tests are fast and give you instant feedback if your changes lately do interfere with something you didn’t expect.

I love this topic also in the game development world tests are a useful tool, especially when you do not work alone on a game. I will write some more articles about this topic.