What is BDD?

  Given some preconditions (Arrange) -> When an action occurs (Act) -> Then verify the output (Assert)

I.E.

  Given three items in Cart
  When one Item is deleted
  Then Two items should be left in Cart
@ExtendWith(MockitoExtension.class)
public class EmpServiceImplTest {

    @Mock
    EmployeeDB employeeDB;

    @InjectMocks
    EmpServiceImpl empServiceImpl;

    @Test
    @DisplayName("Check Employee in BDD Style")
    public void test_getEmployees_Success(){
        //Given - Employee List has only one row with List size as 1
        //When  - New Employee is added to the List
        //Then  -  Employee List should contain two rows

        //Given - Employee List has 3 Employees with List size as 3
        List<Employee> arrEmployees = new ArrayList<>();
        arrEmployees.add(new Employee("101", "Mugil"));
        arrEmployees.add(new Employee("102", "Max"));
        arrEmployees.add(new Employee("103", "Mani"));

        given(employeeDB.getEmployeesFromDB()).willReturn(arrEmployees);

        //When
        List<Employee> arrFilteredEmps = empServiceImpl.getEmployees();

        //Then - there should 3 EmployeegetEmployeeByIdFromDB
        Assertions.assertEquals(3, arrFilteredEmps.size());
    }
}
  1. @BeforeAll, @AfterAll – Runs Before and After Class and should be static
  2. @DisplayName – Displays description about the Test Method
  3. @Disabled – @Ignore in Junit 4. Disables the Test Method
  4. @Nested – Helps in grouping of similar test methods together
  5. @ParameterizedTest – Supplying more than one input for same method in row using array
  6. @ValueSource – Provides multiple paramters to same test method for ParameterizedTest
  7. @CsvSource – Provides multiple paramters to same test method for ParameterizedTest in Key Value Pair. Key is Input and Value is expected Output

AccountUtils.java

import java.io.IOException;

public class AccountUtils {

    public boolean validateAccountId(String acctNo) throws IOException {
        if (getAccountIDLength(acctNo)) {
            return true;
        } else {
            throw new IOException("Account ID is Invalid");
        }
    }

    public boolean getAccountIDLength(String acctNo)
    {
        if(acctNo.length() < 5) return false;
        if(acctNo.length() > 10) return false;
        if(!acctNo.contains("-")) return false;

        return true;
    }

    public  String getFormattedAccID(String accountNo){
        return accountNo.toUpperCase();
    }

    public String[] getBankDetails(String accountNo){
        String[] arrAccountDetails = accountNo.split("-");
        return arrAccountDetails;
    }
}

AccountUtilsTest.java

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.fail;

public class AccountUtilsTest {

    AccountUtils systemUnderTest = new AccountUtils();

    @BeforeAll
    static void init(){
        System.out.println("Initilize connection to Database");
    }

    @AfterAll
    static void destroy(){
        System.out.println("Deallocate connection to Database");
    }

    @Test
    @DisplayName("AccountID Length Validation Success")
    void test_getAccountIDLength_Success(){
        boolean expectedOutput = true;
        boolean actualOutput = systemUnderTest.getAccountIDLength("SBI-104526");

        Assertions.assertEquals(expectedOutput, actualOutput);
        Assertions.assertTrue(actualOutput);
    }

    @Test
    @Disabled
    void test_validateAccountId_Success() throws IOException {
        Assertions.assertTrue(systemUnderTest.validateAccountId("SBI-104526"));
    }

    @Test
    @DisplayName("AccountID is Invalid ")
    void test_validateAccountId_Exception(){
        Assertions.assertThrows(IOException.class, ()->{
            systemUnderTest.validateAccountId("");
        });
    }

    @Test
    @DisplayName("Account Bank Details Validation")
    void test_AccountIDForBankName_Success(){
        String[] expectedOutput = new String[]{"SBI", "104526"};
        String[] actualOutput = systemUnderTest.getBankDetails("SBI-104526");

        Assertions.assertArrayEquals(expectedOutput, actualOutput);
    }

    @Nested
    class checkValidAccountId
    {
        @ParameterizedTest
        @ValueSource(strings={"SBI", "SBI-123456789", " ", ""})
        @DisplayName("Parameterized Test AccountID Length Validation Failiure")
        void test_getAccountIDLength_Failiure(String accountId){
            Assertions.assertFalse(systemUnderTest.getAccountIDLength(accountId));
        }

        @ParameterizedTest(name="AccountID {0} formatted to {1}")
        @CsvSource(value={"sbi-123456, SBI-123456", "cbi-123456, CBI-123456"})
        @DisplayName("AccountID Format Validation")
        void getFormattedAccID_Success(String inputString, String expectedOutput){
            Assertions.assertEquals(expectedOutput, systemUnderTest.getFormattedAccID(inputString));
        }
    }
}
Junit 5 = Platform + Jupiter + Vintage 
  1. Platform = Engine + Runner + Launcher
  2. All the Class files needed for Coding Test Cases
  3. Provides Support for Junit3 and 4

Adding Dependency in pom.xml for Junit 5

JUnit 5 Platform
includes junit-jupiter-api + junit-platform-engine

<dependencies>
    [...]
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>
    [...]
</dependencies>

If you want to write and execute JUnit 3 or 4 tests via the JUnit Platform add the Vintage Engine to the dependencies

<dependencies>
    [...]
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>
    [...]
</dependencies>

In the below code we are going to mock the List Interface and override the method behaviour of List Methods
Methods mocked

  1. get(index)
  2. size()
  3. exception

ListTest.java

package com.mugil.org;

import org.junit.Before;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.junit.Assert.assertEquals;

public class ListTest {
    List arrEmployee;

    @Before
    public void init(){
        arrEmployee = mock(List.class);
    }

    @Test
    public void ListMock_SizeMethod(){
        when(arrEmployee.size()).thenReturn(3);
        assertEquals(3, arrEmployee.size());
    }

    @Test
    public void ListMock_GetMethod(){
        when(arrEmployee.get(0)).thenReturn("Employee1");
        when(arrEmployee.get(1)).thenReturn("Employee2");
        when(arrEmployee.get(2)).thenReturn("Employee3");

        assertEquals("Employee2", arrEmployee.get(1));
        assertNotEquals(null, arrEmployee.get(2));
    }

    @Test(expected = RuntimeException.class)
    public void ListMock_ThrowException(){
        when(arrEmployee.get(anyInt())).thenThrow(new RuntimeException());
        arrEmployee.get(1);
    }
}

Below is a simple class which uses stub for Testing

  1. Below is a simple business implementation class which EmployeeDetailsImpl which uses EmployeeService API methods
  2. Now to test EmployeeDetailsImpl we need to substitude the methods in service class with stub methods for which we create EmployeeServiceStub
  3. EmployeeServiceStub is a stub class which implements the EmployeeService and provide method definitions
  4. EmployeeDetailsImplTest is the test class for EmployeeDetailsImpl which performs the unittest for methods in EmployeeDetailsImpl

EmployeeService.java

 
import java.util.List;

public interface EmployeeService {
    public List<String> GetEmployeeDetails();
}

EmployeeDetailsImpl.java

 
import com.mugil.org.api.EmployeeService;

import java.util.List;

public class EmployeeDetailsImpl {
    EmployeeService employeeService;

    public EmployeeDetailsImpl(EmployeeService pEmployeeService){
        this.employeeService = pEmployeeService;
    }

    public List<String> getEmployeeList(){
        List<String> arrEmp = this.employeeService.GetEmployeeDetails();
        return arrEmp;
    }
}

EmployeeServiceStub.java

 
import java.util.Arrays;
import java.util.List;

public class EmployeeServiceStub  implements  EmployeeService{

    @Override
    public List<String> GetEmployeeDetails() {
        return Arrays.asList("Mugil", "Mani", "Vinu");
    }
}

EmployeeDetailsImplTest.java

 
import com.mugil.org.api.EmployeeServiceStub;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class EmployeeDetailsImplTest {
    @Test
    public void getEmployeeList_success(){
        EmployeeServiceStub employeeService = new  EmployeeServiceStub();
        EmployeeDetailsImpl employeeDetailsImpl = new EmployeeDetailsImpl(employeeService);
        assertEquals(3, employeeDetailsImpl.getEmployeeList().size());
    }
}

Dis-advantage of the above implementation

  1. In the above implementation when ever new method is added to the API interface, it should be added to the EmployeeServiceStub which implements EmployeeService
  2. EmployeeServiceStub is extra java file which should be taken care, incase there are multiple services used in class multiple service stub needs to be created

Replacing stub with mocks
In the below code, the same stub service(EmployeeServiceStub.java) is replaced with mock class and its methods are replaced using when and return.This prevents the need for another class

EmployeeDetailsImplTest.java

 
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class EmployeeDetailsImplTest {
    @Test
    public void getEmployeeList_success(){
     
        //Replacement code for Stub class
        EmployeeService employeeService = mock(EmployeeService.class);
        when(employeeService.GetEmployeeDetails()).thenReturn(Arrays.asList("Mugil", "Mani", "Vinu"));

        EmployeeDetailsImpl employeeDetailsImpl = new EmployeeDetailsImpl(employeeService);
        assertEquals(3, employeeDetailsImpl.getEmployeeList().size());
    }
}

Testing Output using assertEquals
Scenario1.java

public class Scenario
{	
  public int Square(int no)
  {
    return no*no;
  }
}

SquareTest.java

public class SquareTest 
{
  @Test
  public void test() 
  {
    int SquareNo = new Test1().Square(5);		
    assertEquals(25, SquareNo);
  }
}

Scenario2.java(word Counter)

public class Scenario2 
{	
 public int repeatedWords(String pWord)
 {
   int count = 0;
	
   for (int j = 0; j < pWord.length(); j++) 
   {
     if(pWord.charAt(j) == 'a')
     {
       count++;	
     }
   }	
    return count;
 }
}

RepeatTest.java

public class RepeatTest {
   @Test
   public void test() {
     Scenario2 objScenario2 = new Scenario2();		
     assertEquals(objScenario2.repeatedWords("alphabet"), 2);		
   }
}

Test suite is used to bundle a few unit test cases and run them together. In JUnit, both @RunWith and @SuiteClasses annotations are used to run the suite tests.

AllTests.java (TestSuite)

@RunWith(Suite.class)
@SuiteClasses({ RepeatTest.class, SquareTest.class })
public class AllTests {

}

Now Lets take a Real Life Scenario of Bank Account
Account.java

public class Account 
{
	public int Balance;
	
	public Account()
	{
		Balance = 0;
	}
	
	public int getAccBalance()
	{
		return Balance;
	}
	
	public void getCash(int pCash)
	{
		Balance = Balance - pCash;
	}
	
	public void putCash(int pCash)
	{
		Balance = Balance + pCash;
	}
}

BankTest.java

public class BankTest 
{
	Account objAcc = new Account();
	
	@Test
	public void checkAccBalanceTest()
	{
		assertEquals(objAcc.getAccBalance(), 0);
	}
	
	@Test
	public void checkBalAfterDepositTest()
	{
		objAcc.putCash(50);
		assertEquals(objAcc.getAccBalance(), 50);
	}
	
	@Test
	public void checkBalAfterWithdrawTest()
	{
		objAcc.getCash(30);		
		assertEquals(objAcc.getAccBalance(), 20);
	}
}

Points to Note

  1. The methods in BankTest ends with Test Suffix.This ensures the test cases are executed in order.
  2. The Account.java and BankTest.java are two different projects.In BankTest project the other project is added in Java Build Path

DBUtilsTest.java

public class DBUtilsTest
{	
	public void getDBRecordsSQLTest()
	{	
		DBUtils objDBUtils 	 = new DBUtils();
		ResultSet resultSet  = objDBUtils.getDBRecordsSQL("SELECT 78985450.1245487986418648 decimal_val FROM dual");
		
		double expectedValue   = 1245.654618764;
		double dbValue 	 	   = 0; 
		double toleranceLimit  = 0.000000001;
		
		try
		{
			 while(resultSet.next())
			 {
				dbValue =  resultSet.getDouble("decimal_val");
				assertEquals(expectedValue, dbValue, toleranceLimit);
			 }
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		objDBUtils.closeConnection();
	}
	
	@Test
	public void getDBRecordsProcTest()
	{	
		DBUtils objDBUtils 	 = new DBUtils();
		ResultSet resultSet  = objDBUtils.getDBRecordsProc("{call TESTJUNIT.junitproc(?)}");
		
		double expectedValue   = 7.89854501245488E7;
		double dbValue 	 	   = 0; 
		double toleranceLimit  = 0.000000001;
		
		try
		{
			 while(resultSet.next())
			 {
				dbValue =  (double) resultSet.getDouble(1);				
				assertEquals(expectedValue, dbValue, toleranceLimit);
			 }
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		objDBUtils.closeConnection();
	}
}

DBUtils.java

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.jdbc.OracleTypes;

public class DBUtils
{
	Connection  conn  = null;
    Statement 	stmt  = null;
    ResultSet   rs    = null;
	
	public ResultSet getDBRecordsSQL(String pQuery)
	{    
	    try
	    {
	    	conn = DriverManager.getConnection("hostname", "admin", "admin123");
	        stmt = (Statement)conn.createStatement();
	        rs 	 = stmt.executeQuery(pQuery);
	     	
	    } catch (SQLException e)
		{
			e.printStackTrace();
		}
	    
		return rs;
	}
	
	public ResultSet getDBRecordsProc(String pQuery)
	{	
		CallableStatement stmt  = null; 
		
		try
	    {
	    	conn = DriverManager.getConnection("hostname", "admin", "admin123");	        
	        stmt = conn.prepareCall(pQuery);	        
	        stmt.registerOutParameter(1, OracleTypes.CURSOR);
	        stmt.execute();
	        rs = (ResultSet) stmt.getObject(1);
	    } catch (SQLException e)
		{
			e.printStackTrace();
		}
		
		return rs;
	}
	
	public void closeConnection()
	{
		if (rs  != null) try { rs.close();  } catch (SQLException ignore) {}
        if (stmt  != null) try { stmt.close();  } catch (SQLException ignore) {}
        if (conn != null) try { conn.close(); } catch (SQLException ignore) {}
	}
}

Element Locators using XPath

Expressions

 xpath=xpathExpression
   xpath=//img[@alt='The image alt text']
   xpath=//table[@id='table1']//tr[4]/td[2]
   xpath=//a[contains(@href,'#id1')]
   xpath=//a[contains(@href,'#id1')]/@class
   xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td
   xpath=//input[@name='name2' and @value='yes']
   xpath=//*[text()="right"]

DOM – Javascript

 xpath=xpathExpression
 dom=document.forms['myForm'].myDropdown
 dom=document.images[56]
 dom=function foo() { return document.links[1]; }; foo();

How to find particular text in web driver method

WebDriver driver = new FirefoxDriver();
driver.get("https://localhost:8080/Login");

//Finds the xpath of element which contains UserName
WebElement strElemnt1 = driver.findElement(By.xpath("html/body/div[1]/div[2]/div[2]/div[2]/p[1]/b"));

How to get Last Row in Table

//How to get Last Row in Table 
String cssLast="table[class='dataTable']>tr:first-child>td:last-child"
String cssFirst="table[class='dataTable']>tr:last-child>td:last-child"

driver.findElement(By.cssSelector(cssLast)).getText();
driver.findElement(By.cssSelector(cssFirst)).getText();

By Using XPath

//By Using XPath
WebElement lastCellInFirstRow = driver.findElement(By.xpath("table[@class='dataTable']//tr[1]//td[last()]"));
WebElement lastCellInLastRow = driver.findElement(By.xpath("table[@class='dataTable']//tr[last()]//td[last()]"));

How to detect custom attribute

//How to detect custom attribute 
assertTrue(selenium.isElementPresent("//*[@btn-tag-title='Sign In']"));
selenium.click("//*[@btn-tag-title='Sign In']");

assertTrue(selenium.isElementPresent("css=*[btn-tag-title='Sign In']"));
selenium.click("css=*[btn-tag-title='Sign In']");

Finding custom attribute of span where attr=”data-automation-id” and val=”SEL_ERR”

//HTML Code
<span data-automation-id="SEL_ERR">
  Error Description
</span>

//Selenium Code
driver.findElement(By.cssSelector("span[data-automation-id='SEL-ERR']"));

If Selenium cannot find it, it’ll throw an exception. Exceptions are costly. You can use .findElements instead (mentioned in 1)), which will simply return an empty list if it cannot find the element you are looking for

driver.findElements(By.cssSelector("span[data-automation-id='SEL-ERR']")).size() != 0;

The below examples are specific to Junit4

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

Basic Test case which Fails

import static org.junit.Assert.*;
import org.junit.Test;

public class Junit1 {	
 @Test
 public void basicTestFail()
 {
  fail("The Test Failed");
 }
}

There is No pass method in JUnit like fail method.

As long as the test doesn’t throw an exception, it passes, unless your @Test annotation specifies an expected exception. I suppose a pass() could throw a special exception that JUnit always interprets as passing, so as to short circuit the test, but that would go against the usual design of tests (i.e. assume success and only fail if an assertion fails) and, if people got the idea that it was preferable to use pass(), it would significantly slow down a large suite of passing tests (due to the overhead of exception creation). Failing tests should not be the norm, so it’s not a big deal if they have that overhead.

Tests to verify the correctness of a program’s behavior.Verifying the correctness of a program’s behaviour by inspecting the content of output statements.Its looking manually for abnormal output.

Example1

StringHelper.java

package com.mugil.junit.tutor;

public class StringHelper
{	
	public static void main(String[] args)
	{
		StringHelper objStringHelper = new StringHelper();
		System.out.println(objStringHelper.wordATrimmer("PABBB"));
	}
	
	public String wordATrimmer(String pWord)
	{
  	   String pTempString = pWord.substring(0, 2);			   
           if(pTempString.contains("A")) return pWord.replace("A", "");		
 	   pTempString = pWord.substring(2);		
	   return pWord;
	}
}

StringHelperTest.java

package com.mugil.junit.tutor;

import static org.junit.Assert.*;
import org.junit.Test;

public class StringHelperTest 
{
	@Test
	public void test() 
        {
		StringHelper objStringHelper = new StringHelper();
		String expectedOP            = "MMG";
		String actOP                 = objStringHelper.wordATrimmer("AAMMG");
		
		assertEquals(expectedOP, actOP);
	}
}

@Test comes from org.junit.Test Package
assertEquals – helps in performing String, Boolean and Other Comparison.

As you have seen above StringHelper.java is the actual file that has the function.StringHelperTest.java is the file in which test of the functionality is to be carried out. This java file is created in a new source folder test. By using the assert equals method we are comparing whether the expected Op and actual OP are the same.

@Test annotation at the top of the test method tells the JVM to run this method

assertTrue(boolean)
assertTrue(boolean true) will return true
assertFalse(boolean false) will return true

To make sure the code is behaving the right way we can use asserttrue() and assertfalse() methods.
StringHelper.java

package com.mugil.junit.tutor;

public class StringHelper
{	
	public boolean areFirstTwoandLastTwoStringsSame(String Name)
	{
		if(Name.length() <  2) return false;		
		if(Name.length() == 2) return true;
		
		String firstChars = Name.substring(0, 2);
		String lastChars  = Name.substring(Name.length()-2);
		
		if(firstChars == lastChars)
		  return true;
		else
		  return false;
	}
}

StringHelperTest.java

 package com.mugil.junit.tutor;

import static org.junit.Assert.*;
import org.junit.Test;

public class StringHelperTest 
{
  @Test
  public void test() 
  {
    StringHelper objStringHelper = new StringHelper();
    assertTrue(objStringHelper.areFirstTwoandLastTwoStringsSame("AB"));
    assertFalse(objStringHelper.areFirstTwoandLastTwoStringsSame("ABAB"));
  }
}

The above code checks whether First and Last two characters are the same and return true or false based on input to method areFirstTwoandLastTwoStringsSame.

@Before and @After Annotations
The before and after methods are used to run a case before and after the actual test started.

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class StringHelperTest 
{
	
  @Before
  public void before()
  {	
    System.out.println("Before Test");
  }
	
  @After
  public void after()
  {	
    System.out.println("After Test");
  }

  @Test
  public void test1() 
  {
    System.out.println("Test1");
  }
	
  @Test
  public void test2() 
  {
    System.out.println("Test2");
  }	
}

The output of the above code would be
Output

Before Test
Test1
After Test
Before Test
Test2
After Test

You can also run a test before and after the class run as below

import org.junit.AfterClass;
import org.junit.BeforeClass;

@BeforeClass
public static void beforeClass()
{	
  System.out.println("Before Test");
}
	
@AfterClass
public static void afterClass()
{	
  System.out.println("After Test");
}

@Test
public void test1() 
{
  StringHelper objStringHelper = new StringHelper(); 
  System.out.println("During Test");
}

Output

Before Test
During Test
After Test

BeforeClass and AfterClass is run only once when object for class is created.Note the static before beforeClass and afterClass methods.

Typical Example for Using before and after methods where object for class created in before method and object assigned null in after method.

public class StringHelperTest 
{	
  StringHelper objStringHelper1;
	
  @Before
  public void before()
  {	
     objStringHelper1 = new StringHelper(); 
  }
	
  @After
  public void afterClass()
  {	
    objStringHelper1 = null;
  }

  @Test
  public void test1() 
  {   
    System.out.println("Test Cases");
  }	
}

How to check expected array is returned in Output

 
import static org.junit.Assert.*;
import org.junit.Test;

public class StringHelperTest 
{
  @Test
  public void test1() 
  {
    int[] arrNums1 = {5,4,3,2,1};
    Arrays.sort(arrNums1);		
    int[] arrNums2 = {1,2,3,4,5};
		
    assertArrayEquals(arrNums1, arrNums2);
  }
}

assertArrayEquals method comes from org.junit.Assert Package

We are going to look for the expected exception is getting thrown or not

Testing for Exception

import java.util.Arrays;
import org.junit.Test;

public class StringHelperTest 
{
  @Test(expected=NullPointerException.class)
  public void test1() 
  {
    int[] arrNums1 = null;
    Arrays.sort(arrNums1);
  }
}

Testing for Performance – How Much time is taken by code block to execute the code.

In the below code we are going to estimate actual time taken by the code block to execute

 public class StringHelperTest 
 {
   @Test(timeout=100)
   public void test1() 
   {
     for (int i = 0; i < 1000000; i++) 
     {
	    int[] arrNumbs = {i-1, i, i+1};
          Arrays.sort(arrNumbs);
     }
   }
}

timeout=100 – is going to tell the code should complete the execution within 100 milliseconds.If it fails I should optimize the code by performance tweaks to boost codes performance.