Thursday, 5 April 2018

Hamcrest: hasProperty: Check whether given object has specific property or not

By using ‘hasproperty’ method, you can create a matcher that matches when the examined object has a JavaBean property with the specified name whose value satisfies the specified matcher.

Ex
Employee emp = new Employee();

assertThat("Employee must have id property", emp, hasProperty("id"));
assertThat("Employee must have name property", emp, hasProperty("name"));
assertThat("Employee must have experience property", emp, hasProperty("experience"));

Find the below working application.

TestApp.java
package com.sample.tests;

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

import org.junit.Test;

import com.sample.model.Employee;

public class TestApp {

 @Test
 public void testmethod() {
  Employee emp = new Employee();

  assertThat("Employee must have id property", emp, hasProperty("id"));
  assertThat("Employee must have name property", emp, hasProperty("name"));
  assertThat("Employee must have experience property", emp, hasProperty("experience"));
 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment