20-21 - Unit Tests
Goals
- Learn about different types of software testing
- Deep dive into unit tests - what is the purpose of unit tests
- Learn about libraries that helps you write unit tests
Libraries
Slides
Exercises
Exercise 1
Given the following operations, write unit tests to make sure they work as expected Write test that test the behavior in the happy case and test that test exceptional behaviors (i.e. dividing by zero).
public class Operations {
public int divide(int x, int y) {
return x / y;
}
public int multiply(int x, int y) {
return x * y;
}
public int sum(int x, int y) {
return x + y;
}
public int difference(int x, int y) {
return x - y;
}
}
Exercise 2
Given the following program with failing tests, implement the methods to make the tests pass. The tests should act like requirements for what your methods need to do. Clone the code from this repository
Exercise 3
Given exercise 2 with passing tests, modify the tests to use AssertJ library
Homework
Given the previous exercises, add an expiration date to the products. Modify the inventory method to return only the products which are not expired. Add tests to make sure that you do not return expired products. Use AssertJ library in your test.