From the course: Test-Driven Development in Spring Boot with JUnit and Mockito
Unlock this course with a free trial
Join today to access over 24,900 courses taught by industry experts.
Repeat: Adding more features
From the course: Test-Driven Development in Spring Boot with JUnit and Mockito
Repeat: Adding more features
- [Instructor] Woohoo! One cycle done. Let's proceed to the next test, an edge case, division by zero. So back to our test class, I'm going to add another test, testDivideByZero. And of course, I'll annotate it with @Test. Again, I'm going to create new calculator. For my arrange phase, new Calculator(). And this time, I want to make sure that it's going to go ahead and throw the ArithmeticException if I try to divide by zero. So say assertThrows, an I want a ArithmeticException class when, well, I want it to happen, when I do a calculator.divide(), and I'll choose 5 over 0. Now, if we run this test, it fails because our deified method doesn't currently handle division by zero. It just returns infinity. Let's make it pass by handling this case explicitly in our defined method. I'm going to say if(b != 0), and I want to return the result, so let me declare it up here for now. And then here I'll just simply say result = a / b. And at the very bottom, I'll return the results. But if it…