DEV Community

Cover image for 🛑🛑 What is TDD (Test Driven Development)? | How to do TDD with Example 🛑🛑
Pramod Dutta
Pramod Dutta

Posted on • Updated on

🛑🛑 What is TDD (Test Driven Development)? | How to do TDD with Example 🛑🛑

✅ Join us -

In this video, We are going to learn What is TDD (Test Driven Development)? and How to do TDD with Example.

TDD (Test Driven Development)

🚀 What is TDD?

  • Iterative development process.
  • Every iteration starts with a set of tests written for a new piece of functionality.
  • Test cases are created before code is written
  • TDD instructs developers to write new code only if an automated test has failed.

🚀 What are the main benefits of TDD?

  • Small Regression Suite
  • We are doing Test First, Reduction in Bugs
  • TDD is used to make the code clearer, simple and bug-free.
  • Avoids duplication of code
  • Refactoring improves the code
  • TDD drives the code design and approach
  • Unit test cases are covered early.

Example fo TDD

  • Create a Maven Project.
  • Add Testcases in Test class.
package com.thetestingacademy;

import static com.thetestingacademy.App.isValidUserName;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.thetestingacademy.App.*;
import org.junit.Test;

 * Unit test for simple App.
public class AppTest 
     * Rigorous Test :-)
    public void shouldAnswerWithTrue() {
        String userName = "PramodDutta";
        String userName1 = "Pramod Dutta";
        String userName2 = "Pramod@123";
        String userName3 = "Pramod_Dutta";
        String userName4 = "Pramod/Dutta";
        String userName5 = "Pramod#Dutta";

  • Now Execute it, It will fail
  • Fix them one by one
  • Repeat
package com.thetestingacademy;

 * Hello world!
public class App 
    public static void main( String[] args )
        System.out.println( "Hello World!" );
        String userName = "PramodDutta";
        String userName1 = "Pramod Dutta";
        // What is Valid UserName

    static boolean isValidUserName(String name){
        if(name.contains("/") || name.contains("#") || name.contains(" ") ||  name.contains("@") || name.contains("_")){
            return false;
        return true;


Be sure to subscribe for more videos like this!


Top comments (0)