3

I currently have to test the data that is being processed from a file into the database. Currently I'm using Java to extract the data from the database and compare the data mapped to the table compared with the data coming from the source file. I'm using Cucumber for BDD purposes. An issue I'm running into is check whether all the fields are equal between the fields coming from the source file vs the fields coming from the database table.

Lets say we have a User class in java which maps to a User table in the Database.

I have a cucumber datatable with one record which has columns for all the fields of a User (i.e: first name, last name, username,etc). In the database User Table I have those same fields mapped to columns; first_name, last_name, user_name.

Currently I was comparing the field values from the DataTable vs the fields coming from the database kinda in the form of this:

In the Cucumber data table I have something like this:

| user_name | first_name | last_name |
| bob1      | bob        |  bob      |

I am getting the userEntity object using jdbcTemplate:

String bobUserName = userList.get(1).get(0);
String sql = "SELECT * FROM USER WHERE USER_NAME = ?";
UserEntity userEntity = (UserEntity) jdbcTemplate.queryForObject(sql, new Object[] { bobUserName }, new UserRowMapper());

Then inside the same method I want to check the field values:

assertEquals(userList.get(1).get(0), userEntity.getUserName());
assertEquals(userList.get(1).get(1), userEntity.getFirstName());
assertEquals(userList.get(1).get(2)), userEntity.getLastName());

the issue is when I run the test lets say the 2nd assertEquals [checking the first names] fails, then the test just stops there and doesn't continue. Ideally I'd like it to go through all the assertEquals and then give me all the ones that failed.

Another way was that I create another User instance and fill its properties from the fields in the cucumber DataTable and then just do a simple:

userFromDataTable.equals(userEntity);

but when this is false it doesn't tell me why its false.

Is there any efficient way in Java to compare the fields like in my scenario?

  • If the data table is huge, it will be difficult to test it one entry by one entry. Is it possible to select a number of entries and test them? For example, you can test odd numbered data column in one test and test even numbered data column next time. – Yu Zhang Feb 1 '17 at 19:16
  • @YuZhang, I could do that but the problem is that wouldnt be the solution because if one assertEquals fails,it gives an assertion error and the test stops there. I would like it to keep on going and check the other assertEquals too – Nabz Feb 1 '17 at 19:18
  • I see what you meant, you can catch the exception and continue with your test from there. If you do not catch an exception, the default behavior is to stop execution. – Yu Zhang Feb 1 '17 at 20:38
  • The problem with that is that each assertEquals would need a try catch block, if i put them all inside one try block and one of the assertEquals fails then the others wouldn't get checked – Nabz Feb 2 '17 at 1:24
2

You might want to take look at the DataTable of cucumber-jvm since you've been using it anyway. The idea is to use one of its diff methods to compare the expected to the actual state of the database.

In your case you could have done the diff along the following (pseudo)lines:

List<User> actualUsers = new ArrayList<>();
for (User expectedUser: userList) {
    User actualUser = loadUserFromDbByName(expectedUser.getName());
    if (actualUser != null) {
         actualUsers.add(actualUser);
    }
}

DataTable.create(actualUsers).diff(userList);

You will have to play a bit with the proper input parameter to your glue method and conversions to the consistent column names, but at the end you will get a nice difference printout on difference between expected and actual user list.

Might require basic acquaintance with java though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.