TDD
Main goal
The main purpose of this class is to show students the advantages of using IDE (i.e. IntelliJ IDEA) and following
the concept of Test Driven Development – including both unit tests and mocking the behavior of objects. They will
also have the opportunity to implement basic Java programs.
After laboratory students should be able:
- Use IDE (IntelliJ IDEA) to write, compile, run and debug Java code.
- Use JUnit library to create unit test.
- Use Mockito to simulate objects behavior.
- Configure project to add required dependencies (Maven).
- Understand value of TDD process and be able to adhere to it.
Required for the lab
Knowledge of basic Java syntax. Understanding of concepts: IDE, unit testing, mocking, TDD, debugging,
compilation.
TDD steps
- Create a String calculator with a method int sum(String numbers)
- The method can take 0, 1, or 2 numbers and will return their sum.
- An empty string will return 0.
- Example inputs: “”, “1”, or “1,2”.
- Start with the simplest test case of an empty string. Then 1 number. Then 2 numbers.
- Remember to solve things as simply as possible, forcing yourself to write tests for things you didn’t think about.
- Remember to refactor after each passing test.
- Allow the sum method to handle an unknown number of arguments/numbers.
- Negative numbers should be ignored.
- Example: “-1,2” returns 2.
- Calling sum with a sum bigger than 100 will throw an exception.
- Example “1, 100” throws IllegalArgumentException.