Skip to content

Latest commit

 

History

History
130 lines (93 loc) · 5.53 KB

exercise-1.md

File metadata and controls

130 lines (93 loc) · 5.53 KB

Exercise 1 - Introduction

This exercise will get you started learning the basics of writing and running some tests in an IDE (Integrated Development Environemnt) using the JUnit testing framework.

We are going to assume you have IntelliJ Community Edition installed.

You will learn how to:

  1. Set up your development environment
  2. Create your first unit tests using JUnit
  3. Structure your tests properly

1.1. Dev environment

Before you begin: Please make sure that you have the following installed:

✏️ Start by selecting "Open" in the Welcome screen, and navigate to the directory where you cloned the all-about-testing-code repository. Make sure the directory all-about-testing-code is selected and select "Open".

✏️ After finishing the import steps, your project structure should look like the below screenshot. IntelliJ should pick up that src/test/java is a content root for tests and color it light green.

✏️ Open the SmoothieBarTest file and click the green circle/arrow next to the line numbers. Run the tests for the file. The result should look something like this:

Intellij starting point

Note the output at the bottom of the screen:

Hello Nerdschool

Process finished with exit code 0

1.2. Code example

In the introductory exercises you will be working with a simple, ready made example. The theme / "problem domain" is a smoothie bar that can blend different types of smoothies and keep track of the stock of ingredients. The smoothie bar is pretty limited, as they only use apples, oranges and bananas in smoothies, and can only serve three different kinds of smoothie: "Orange and apple smoothie", "Banana and apple smoothe" and "Orange and banana smoothie".

The code has the following structure:

  • A SmoothieKind enum having three different enum values: OrangeAndAppleSmoothie, BananaAndAppleSmoothe and OrangeAndBananaSmoothie. Each smoothie has different recipes (how many apples, oranges and bananas required)
  • A Smoothie representing the beverage that the bar can blend. It has information about what SmoothieKind it is, and what ingredients it consists of
  • A SmoothieBar class representing the smoothie bar. It has these public methods:
    • blend - which takes SmoothieKind as a parameter and returns a Smoothie
    • getApplesInStock, getOrangesInStock and getBananasInStock which gets the current stock of ingredients
    • restockApples, restockOranges and restockBananas which lets you add ingredients to the stock

We are going to focus on testing the SmoothieBar-class in the next exercises.

1.3. Creating your first unit tests

Let's start by creating a test that'll test if the SmoothieBar class can blend an orange and apple smoothie.

✏️ Remove the helloNerdSchool test and add the following by writing the code yourself (don't copy & paste):

@Test
public void canBlendOrangeAndAppleSmoothie() {
    SmoothieBar smoothieBar = new SmoothieBar();
    smoothieBar.restockApples(2);
    smoothieBar.restockOranges(2);
    Smoothie smoothie = smoothieBar.blend(SmoothieKind.OrangeAndAppleSmoothie);
    assertEquals(SmoothieKind.OrangeAndAppleSmoothie, smoothie.getKind());
}

✏️ Run the test, and see that it passes (marked green in the Test Runner).
📖 Notice the JUnit assertEquals method call. This method tests that the given first (expected) value is equal to the second (actual) value. If not, the test fails.
✏️ Make the test fail by making a change to the assertion and re-run the test.
📖 Observe what happens in the Test Runner.

The assertEquals assertion is one of many built in to the JUnit framework. More about assertions here.

We also want to test that the SmoothieBar class consumes oranges and apples from its stock when a smoothie is made.

✏️ Add the following test by writing the code yourself (don't copy & paste):

@Test
public void blendingOrangeAndAppleSmoothieConsumesOrangesAndApples() {
    SmoothieBar smoothieBar = new SmoothieBar();
    smoothieBar.restockApples(2);
    smoothieBar.restockOranges(2);
    Smoothie smoothie = smoothieBar.blend(SmoothieKind.OrangeAndAppleSmoothie);
    assertEquals(0, smoothieBar.getApplesInStock());
    assertEquals(0, smoothieBar.getOrangesInStock());
}

✏️ Run the test and see that it passes.
✏️ You can also run all tests at once by clicking the green arrow beside the test class name.
✏️ Add a similar test for creating a banana and apple smoothie.

1.4. Test structure

It is good practice to use the following structure when writing tests:

  • Given
    • What should the world look like when the test happens?
    • The preconditions for the test
  • When
    • What is being tested?
    • The behavior
  • Then
    • What are the changes that happened?
    • The post-condition

✏️ Identify the Given, When, Then sections of the tests you just wrote by inserting comments and line breaks to make it clearer.

❗ You should use Given, Where, Then, or Arrange, Act, Assert comments in all your tests in this workshop. It's not strictly a common thing to do in production code, but it'll be helpful when getting started.