Skip to content

Instantly share code, notes, and snippets.

@ryanorsinger
Created November 2, 2018 16:54
Show Gist options
  • Select an option

  • Save ryanorsinger/ae0bce9e3de8a35f18d8c80c21996f36 to your computer and use it in GitHub Desktop.

Select an option

Save ryanorsinger/ae0bce9e3de8a35f18d8c80c21996f36 to your computer and use it in GitHub Desktop.
TDD JUnit Intro

First Steps into Test Driven Development

What are we doing and why?

We'll be

Starting a TDD Project in IntelliJ

Open IntelliJ Choose "New Project" Select "Maven Project"

Set your "groupId" com.yourname.tdd.intro Set your "artifactId" to intro Name the project folder "tdd-first-steps"

Create a new file .gitignore in your tdd-first-steps folder

# OS files to ignore
.DS_Store
Thumbs.db

# Editor files to ignore
*.iml
.idea/*

# ignore compiled .class files
out/

Open your terminal git init git status

git add .gitignore git status should show .gitignore as green, which means it's ready to be committed

git commit -m "Add files to ignore to .gitignore" will make a commit containing the gitignore file

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Functionality we'll be testing and implementing

linkedlist doublylinkedlist queue Stack isEmpty push peek pop contains size peekNth

First Steps

Inside of tdd-intro/src/test/, create a new Java class named TruthTest.java

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class TruthTest {
    @Test
    public void baseTruth() {
        assertEquals(true, true);
    }
}
  1. Run your tests.
  2. To run your tests
  3. The mac keyboard shortcut is control then shift then R.
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class TruthTest {

    @Test
    public void baseTruth() {
        assertEquals(true, true);
        assertNotEquals(true, false);
    }

}
  1. Run your tests
  2. Take stock that we now have an automated
import org.junit.Test;

public class StackTest {

    @Test
    public void testIsEmpty() {
        Stack stack = new Stack();
    }
}
public class Stack {
    
}
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class StackTest {

    @Test
    public void testIsEmpty() {
        Stack stack = new Stack();
        assertEquals(true, stack.isEmpty());
    }
}
public class Stack {

    public boolean isEmpty() {
        return true;
    }
}
  1. It's OK that this implementation isn't perfect. The goal is to green the test.
  2. Run all tests

Now it's time to implement a .push method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment