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.
- Set up your development environment
- Create your first unit tests using JUnit
- Structure your tests properly
Before you begin: Please make sure that you have the following installed:
- IntelliJ Community Edition: IntelliJ Community Edition
- Java dev kit: Java SE Development Kit
✏️ Start by selecting "Import Project" in the Welcome screen:
✏️ Then browse to, and select, the pom.xml
file under the "exercise-1" folder.
✏️ Then follow the steps, accepting the defaults in all windows.
✏️ Make sure you select Java JDK 1.8.0 when prompted for project SDK. You might need to browse to the directory where the SDK is installed on your computer.
✏️ After finishing the import steps, your project structure should look like the below screenshot. IntelliJ should've picked 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.
You should see same test result as the earlier screenshot
Hello Nerdschool
Process finished with exit code 0
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
andOrangeAndBananaSmoothie
. 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 whatSmoothieKind
it is, and what ingredients it consists of - A
SmoothieBar
class representing the smoothie bar. It has these public methods:blend
- which takesSmoothieKind
as a parameter and returns aSmoothie
getApplesInStock
,getOrangesInStock
andgetBananasInStock
which gets the current stock of ingredientsrestockApples
,restockOranges
andrestockBananas
which lets you add ingredients to the stock
We are going to focus on testing the SmoothieBar
-class in the next exercises.
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);
assertTrue(smoothie.getKind() == SmoothieKind.OrangeAndAppleSmoothie);
}
✏️ Run the test, and see that it passes (marked green in the Test Runner).
📖 Notice the JUnit assertTrue
method call. This method tests that the given boolean expression is true. 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 assertTrue
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);
assertTrue(smoothieBar.getApplesInStock() == 0);
assertTrue(smoothieBar.getOrangesInStock() == 0);
}
✏️ 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.
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, What sections of the tests you just wrote by inserting comments and line breaks to make it clearer.
❗ You should use Given, Where, What, 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.