From the course: Test-Driven Development in Spring Boot with JUnit and Mockito
Red phase: Write a failing test
From the course: Test-Driven Development in Spring Boot with JUnit and Mockito
Red phase: Write a failing test
- [Instructor] All right, the red phase, let's write a failing test. Writing a failing test might actually not be as easy as you think now, and I'll walk you through the whole process. Let's start with something silly. Let's say we need to write the code for a calculator that needs to be able to perform several operations, additions, subtractions, division, multiplication, maybe more, but we won't write all of that, so let's not worry about what calculators are capable of. One thing that you shouldn't do, write a failing test for all the features, add, subtract, divide and multiply. No, no, no, no, we'll do this in iterations. So here's our projects, and I'm going to create a separate package for our first test, firsttddtest. And I'm going to mimic this one inside my test folder. So I'm going to write firsttddtest. Then I'm going to create a new test. I'm going to call it CalculatorTest. Let's start by writing the test for dividing two positive numbers. Write the @Test annotation. I'm going to say testDivideTwoPositiveNumbers. And the first line I'm going to write is Calculator calculator equals new Calculator. All right, and here's a common question I get, "What to do now that it's not compiling?" There's a difference of opinion, but a general opinion leans toward write minimal stubs to avoid compilation errors when writing tests first. I personally really don't like compilation errors, so I always go back and forth and write stubs as I go. A stub is the minimal code constructs to satisfy the compiler. So let's create a stub for the calculator. Inside my firsttddtest package, I'm going to create a Calculator Class. And this is going to be my stub for now. And as you can see, now that I've got my calculator class stubbed out to satisfy the compiler, I don't see any compilation errors. So back to the test. I want to test the division operator. So let's write the test for simple case dividing two positive numbers. So let's go ahead and say double result equals calculator.divide six and second argument two. Uh oh, another issue arises. The divide method doesn't exist in my calculator class yet. Again, to avoid any compilation errors, I'll create a stub for the divide method. It's going to return a double. It's called divide, and it's going to take two numbers. We can make it int, we can make it double. I don't care too much for now. And in order to make this compile, I need to return some sort of value. So for now, I'll create a dummy implementation, just returning zero. It's my stub implementation. So now it compiles again. Let's go ahead and assert that our result is correct. So I'm going to use the assert equals to do that, and I don't want to import it from here actually. I want to go ahead and use this one. Assertions, and I'll import all of them. And that's the assert equals I'll be using. So make sure to use the correct one. And I want to make sure that the expected result is three and that result is matching that. So I have to comment arrange, acts, and assert pattern here, which is a very common way to structure your tests. And as you can see, when I run the test, it's failing, and that's perfect because that's exactly what we want in the red phase. A failing test tells us our code doesn't yet meet the requirements. That means it's time to move to the next phase, green.