Thursday, 5 April 2018

Hamcrest: samePropertyValuesAs: Check two beans properties

By using 'samePropertyValuesAs' method, you can create a matcher that matches when the examined object has values for all of its JavaBean properties that are equal to the corresponding values of the specified bean.

Ex
Employee emp1 = new Employee(1, "Krishna", 12345.6);
Employee emp2 = new Employee(1, "Krishna", 12345.6);
assertThat("emp1 and emp2 must have same property values", emp1, samePropertyValuesAs(emp2));

Find the below working application.

TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.samePropertyValuesAs;

import org.junit.Test;

import com.sample.model.Employee;

public class TestApp {

 @Test
 public void testmethod() {
  Employee emp1 = new Employee(1, "Krishna", 12345.6);
  Employee emp2 = new Employee(1, "Krishna", 12345.6);
  assertThat("emp1 and emp2 must have same property values", emp1, samePropertyValuesAs(emp2));
 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment