Test Driven Development | Code Quality
Within the previous topic we have a brief overview regarding dependency injection and we had a look at a poor code and we changed it to an object oriented code which was robust and cohesive. Link can be found here . Within this blog post we would be looking at the same code base of adding sum of two numbers and apply test driven development to the code base. Before that we would look at the following aspects:
- What is test driven development?
- Tools needed to apply test driven development
- Apply test driven development to a code
- When to apply test driven development
What is test driven development?Before we jump to an example, lets understand what test driven development is using a real life example. Consider a Formula 1 pit crew whose job is to get the car serviced in minimum time as possible and consider a car service company that lazily gets your car serviced. There is a difference between these two approaches. In the first approach, everything is crosschecked twice and everything is done in harmony. No chances of mishaps and a view like this is seen as shown below: Whereas, in the lazy car service, people are breaking your car while servicing and applying patches wherever possible. Something like the car below: Similar is the approach with your code. You can choose to write it as a maintainable code where even if someone else works on it, still works or you can choose it to write a code that is often required to be maintained and is applies patches so that it works. To create your code similar to the F1 service, you have to include test driven development to your project. One of the main things to keep in mind is that the entire process is an agile process. You write a certain test case, then write your code. Then write new test cases and then modify your code. The entire process is a continuous cycle which gives advantage to you in the way such that whenever there is another developer working on the project with you and does changes to your code, it must pass the unit test cases.
What are the tools needed for test driven development?
Junit for unit testJunit is used for testing unit code that uses Java as a programming language. It takes the code in isolation and runs the code against the given test cases. It would show which all test cases failed and why they failed. Cheat sheet related to Junit is shown here.
Mockito for REST API testingMockito allows developers to create and test objects (mock objects) in unit test for test driven development. There are many other tools to write code using test driven development depending upon the programming language.
Apply test driven development to existing code.Let us consider the example of adding two numbers as shown in previous post. We want to make sure that it is able to add two numbers like two positive numbers, two negative numbers, one positive and one negative number. We are going to approach it by:
- Creating a separate test layer within the project
- Next we are going to create mock objects which would simulate the behaviour of a real object and call it MockSum.java
- Next create a separate class for writing test cases and call it SumTest.java within the test layer.
- Next add Junit library to the project by following steps mentioned in the next section.