DEV Community

Cover image for Understanding TDD
Ravina Deogadkar
Ravina Deogadkar

Posted on

Understanding TDD

Test Driven Development (TDD) is a very powerful development methodology that completely turns traditional development around. When you first go to implement a new feature, the first question that you ask is whether the existing design is the right design for that feature. If it is not, you make changes to the existing design locally to enable that feature to be implemented as quickly and easily as possible. The result is that you are constantly improving the quality of your code and ensuring that your designs match the actual requirements that you will actually use in production. This also helps to ensure that the final code works as expected and does not introduce any unwanted side-effects or bugs.

When implementing new features in a software system it is important to keep sight of the bigger picture at all times and make sure that the system is working as efficiently and accurately as possible. This includes running tests against your production environment. These tests will check whether the new feature is working correctly and identify any potential problems that might arise as a result. If these tests fail then the underlying design of the code will need to be changed to ensure that the new feature can be successfully implemented.

The continuous integration and deployment (CI/CD) process is designed to automate this process. TDD is essentially a test-first development methodology in which you write an automated test suite for your code before you write the actual code itself. These tests are written using programming language-specific syntax and are not actual examples of user behavior.

Looking at few examples for understanding TDD.

//Main.java
public String helloString(){
    return "Hello";
}
Enter fullscreen mode Exit fullscreen mode

This is a simple method returning string "Hello" .To test the returned string we will be adding a test method.

//MainTest.java
    @Test
    void shouldReturnHello(){
        Assertions.assertEquals("Hello", Main.helloString());
    }
Enter fullscreen mode Exit fullscreen mode

To make tests readable have meaningful name for the test methods and aggregate testcases to different classes as per their respective purpose.

Directory structure

Here is a directory structure of LibraryMgmt project. I have separate test class for controller, service and application class.

You can find complete source code here.

That's it for today! Thank you and Happy Coding!...

Top comments (0)