Cucumber Java: Data Tables Comparison

Source: Youtube Video

Video Title: Cucumber with Java: Compare Data Table with any other type of table

Video URL: https://youtu.be/IPQc5EUuysU

Query:

Umang Sharma: this is great .. thanks for sharing..
just wanted to know if there is any method which returns boolean if we can to compare one datatable instance with another?  
also, what diffence will the method return if the datatables are equal ( in the video if both tables were same, will it show any difference?) thanks ...

Hi,

1. In the data table class equals method exists, which returns true or false, which you can use to compare 2 data tables. For your test to pass or fail, you can use it along with assert like this

Assert.assertTrue(dataTable.equals(actual));

Implementation of the equal method in Data Table Class

@Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    DataTable dataTable = (DataTable) o;
    return raw.equals(dataTable.raw);
  }

2. For diff method when both tables are equal your test will pass otherwise test will fail since diff method throw error when tables are not equal

Usage: 

dataTable.diff(actual);

Implementation in Data Table Class:

public void diff(DataTable actual) throws TableDiffException {
    TableDiffer tableDiffer = new TableDiffer(this, actual);
    DataTableDiff dataTableDiff = tableDiffer.calculateDiffs();
    if (!dataTableDiff.isEmpty()) {
      throw TableDiffException.diff(dataTableDiff);
    }
  }