Sei sulla pagina 1di 4

Test Driven Development:

 TDD is a software development process

For each feature, Developer needs to clearly understand the feature and its functional
requirement. (Output: Java interface with its functions signature (all the methods, method
name, arguments and return types))

(1) Write a Test Case (Junit) first


Test case should cover the correct functional requirement first (Happy Path)
Test case should cover exception conditions (Alternate Path)
Test case should try to cover all the probable and possible scenarios (Test Coverage)
(2) Run the above written test cases against existing code base (In case of already existing
features) and ensure that the recently written (as per step 1) test cases are failing.
(3) Write your actual implementation now and ensure it passes all the test cases of Step1. You
should also need to make sure that all the existing test cases are also getting passed now.
(4) If there are any failures (existing test cases failure is called regression), it needs to be
corrected/fixed until all the test cases are successfully run against the code base.
(5) Now you have gained confidence and implemented and tested the business requirement
successfully. It is time for you to refactor the code and re-run the test cases again. Any
missed out test cases can be added here and the complete test suite (all test cases) should
be re-run.

Simple TDD Example:

User Story: Write a software that checks if the inputted string is a palindrome or not.

Requirement Analysis:

What is a palindrome?
What if the word or phrase contains any special character? Like in “Madam, I'm Adam”

Business Analyst or the customer has confirmed that these scenarios also need to be implemented.

Design phase begins... (Functional or Technical design for the feature with respect to the overall existing
architecture need to be addressed now).

Input is going to be a text (Parameter is String)

We should say whether the input is Palindrome or not (Return type Boolean)

We should also need to remove special characters (if any) and return the text without special characters
– new method is required, Parameter is String and its Return type is also a String

So, the order is to remove the special characters first and then check if the text is a palindrome or not.

Java Interface:

interface PalindromeInterface{
public String removeSpecialChars(String input);
public boolean checkPalindrome(String input);
}

TFD (Test First Development):

Do you know JUNIT?

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class PalindromeTest {
private boolean expected;
private String palindromeText;

public PalindromeTest(boolean expectedResult, String palindromeText) {


this.expected = expectedResult;
this.palindromeText = palindromeText;
}
@Parameters
public static Collection palindromeInputs() {
return Arrays.asList(new Object[][] { { true,"Sit on a potato pan, Otis" }, {
true,"Able was I ere I saw Elba" },
{true,"Madam, I'm Adam" }, { true,"A man, a plan, a canal - Panama!" },
{true,"12344321" },{false,"ab"},{true,"a"},{true,"LIRIL"},{false,"junk characters"},
{false,"1213"}});
}
@Test
public void testPalindrome() {
Palindrome obj=new Palindrome ();
this.palindromeText=obj.removeSpecialChars(palindromeText);
assertEquals(expected, obj.checkPalindrome(palindromeText));
}
}

Implementation:

package com.oracle.Programs;
import java.util.Scanner;

public class Palindrome


{
public static boolean checkPalindrome(String inputString){
int strLen=inputString.length()-1;
int i=0;
while(i<(strLen-i)){
if(!(inputString.charAt(i)==inputString.charAt(strLen-i)))
return false;
i++;
}

return true;
}

public static String removeSpecialChars(String usrInputCopy){


return usrInputCopy.replaceAll("[^a-zA-Z0-9]+", "").toUpperCase();
}

public static void main( String[] args )


{
Scanner input =new Scanner(System.in);
String usrInput=input.nextLine();
if(usrInput.length()<=0){
System.out.println("Please provide a valid input string");
return;
}
usrInput=removeSpecialChars(usrInput);
boolean result=Palindrome.checkPalindrome(usrInput);
if(result)
System.out.println("It is a Palindrome!");
else
System.out.println("It is NOT a Palindrome!");
}
}

Potrebbero piacerti anche