Giter VIP home page Giter VIP logo

sample-kata-tdd's Introduction

TDD Code Kata Starter Project

  • This project was generated using Spring Initializr
  • This project comes with JUnit5 built in and does not include JUnit4.
  • To build and run this project, you will need to have JDK 17 installed on your work station

Getting Started

  1. Choose your IDE. IntelliJ is recommended, but eclipse could be used if that is the team's preference. Ideally all participants in the code kata can use the same IDE.
  2. This project contains a final branch that can be referenced to see an example possible solution for each step. The main branch contains an empty project and should be used to start the kata.

What is a Code Kata?

A Code Kata is a small group coding exercise where each participant writes small snippets of code in one sitting, sometimes repeatedly, in order to build muscle memory and practise their craft.

What is TDD?

TDD stands for Test Driven Development and in a nutshell it is a practice where you implement a feature by first writing the test, getting the test to fail, then implement the simplest minimal code needed to make the test pass, refactor the code, and then repeat.

Executing this TDD Code Kata

In this Code Kata, the exercise will involve building a simple Rock, Paper, Scissors game. The goal is to write a method that takes two parameters; hand one and hand two. The method will then determine the result and return a value indicating that either Hand One Won, Hand Two Won, or the result was a Tie. The team should also decide which assertion framework they will use. The first Kata should probably use org junit assertions, but subsequent Katas could use others such as assertj.

Step 1

Write a failing test. Refer to the User Stories and review the first test case which is rock vs rock.

@Test
void rockVsRock() {
    assertEquals(true, false);
}

Run the test and validate that it failed for the expected reason. If the failure looks good, then make the test pass.

@Test
void rockVsRock() {
    assertEquals(true, true);
}

Run the test again and verify that the test passes.

Step 2

Rewrite the test with the goal that this iteration could potentially be the final implementation.

@Test
void rockVsRock() {
    assertEquals(Result.DRAW, new Rps().play(Hand.ROCK, Hand.ROCK));
}

The key to this step is to write the code so that it has compile errors and then let the IDE generate the code. Once the IDE has generated the code, the implementation of our method should look like below.

public Result play(Hand hand1, Hand hand2) {
    return null;
}

Run the test and validate that it fails for the expected reason. If the failure is correct, then implement the simplest solution that will make the test pass.

public Result play(Hand hand1, Hand hand2) {
    return Result.DRAW;
}

Re-run the test and validate that it passes. If the test is green, then review code and refactor if necessary. At this point, only the test may need some clean up.

class RpsTest {

    Rps rps = new Rps();

    @Test
    void rockVsRock() {
        assertEquals(Result.DRAW, rps.play(Hand.ROCK, Hand.ROCK));
    }
}

Subsequent Steps

The next step then would be to implement the remainder of User Stories. Following the Gherkin, the tests should be implemented first and executed and the results validated that all the new tests fail and all the existing tests still pass. After this, the simplest code is then implemented to get each test to pass one at a time. Once all tests are green, then the code is reviewed and refactored as needed. After refactoring, all tests are ran once again to validate that everything is still green.

sample-kata-tdd's People

Watchers

Keith Kester avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.