TDD with Java

Install Homebrew

We're going to start by installing Homebrew which will make installing everything else we need much easier.

Open a Terminal window and copy and paste the following into your terminal:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then ensure you have the latest version of Homebrew by typing the following:

$ brew update

And finally, ensure that Homebrew's environment is in a healthy state:

$ brew doctor

This may give you some commands to run in order to fix any problems, which you should do before continuing.

Install Java

Now we're ready to install Java on our system. We'll be using Homebrew to do that.

$ brew cask install java

Creating a Project

Let’s create our project structure using Maven:

$ brew install maven
$ mvn archetype:generate -DgroupId=tdd -DartifactId=piglatin-java -Dversion=0.1-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
$ cd piglatin-java

Dependency Management

When building a software project you will often use libraries created by other people. In order to both specify and automatically download these dependencies you'll want to use a dependency management system.

Add Testing Framework

In order to express the requirements of our code we're going to use a testing framework. Testing frameworks, at their most basic, allow you to test the output of a function when given a certain input.

Edit the generated pom.xml to bring in JUnit, the testing library we’ll be using:

<project
  xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>tdd</groupId>
  <artifactId>piglatin-java</artifactId>
  <packaging>jar</packaging>
  <version>0.1-SNAPSHOT</version>
  <name>piglatin-java</name>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Then run mvn test to install the dependency and ensure everything is set up correctly.

Our First Failing Test

Let's create a test for our first requirement - translating apple to appleway.

Create the file src/test/java/tdd/PiglatinTest.java with the following contents:

package tdd;

import org.junit.Test;

public class PiglatinTest {
    @Test
    public void translateWord_startingWithVowel() {
        assertEquals("apple", Piglatin.translateWorld("appleyay"));
    }
}

And try to run your test:

$ mvn test
[INFO] Compiling 2 source files to /Users/bkelly/temp/piglatin-java/target/test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/bkelly/temp/piglatin-java/src/test/java/tdd/PiglatinTest.java:[9,32] cannot find symbol
  symbol:   variable Piglatin
  location: class tdd.PiglatinTest
[INFO] 1 error
[INFO] -------------------------------------------------------------

We’re getting a compilation error because we don’t have a class called Piglatin.

Create the Piglatin Class

Create the file src/main/java/tdd/Piglatin.java with the following contents:

package tdd;

import org.junit.Test;

public class Piglatin {

}

Now run your tests again and you should see:

OUTPUT HERE

Create the Translate Word Method

Fix the Method Signature

Return the Correct Answer

Another Word

Some Actual Code

Let's add some code to actually do something with the word we are passed.

The Pig Latin rules state that when the word starts with a vowel we just add `way` to the end of it.

Since we only have tests for words starting with vowels, let's do the minimum amount of work to get our test passing - appending `way` to the word that's passed it.