Skip to main content

The Mars Rover Saga - Applying the State Pattern

2024-07-1710 min
Clean CodeTestingJava

Introduction

This article was co-written with my colleague Aitor Reviriego, and will be the first article in a series where we'll share how we developed the Mars Rover kata, explaining step by step the patterns or strategies we used in each section of the exercise, with the goal of documenting and sharing these techniques.

First Steps

The language we used for the kata is Java, specifically version 17 with Gradle (Groovy). The idea is that once the exercise is complete, we'll implement a service with Spring (Webflux) and be able to interact with it through a frontend.

The first thing we did was write an initial test that would allow us to start building the code we knew we wanted to write, using the IDE (IntelliJ) to help us:

java
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class RoverShould {
    @Test
    void start_with_and_initial_position_facing_initial_direction () {

        Rover rover = new Rover(new Position(0,0), new North());

        assertThat(rover.getPosition()).isEqualTo(new Position(0,0));
        assertThat(rover.getDirection()).isEqualTo(new North());
    }
}

Read the full article on Leanmind