Tuesday, 27 February 2018

Hamcrest: containsString: match sub string

By using 'containsString' method, you can create a matcher that matches if the examined string contains the specified String anywhere.

Ex
String str = "Hello World";
assertThat("str should contain 'World'", str, containsString("World"));

TestApp.java
package com.sample.model;

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

import org.junit.Test;

public class TestApp {

 @Test
 public void containsString_testWithDiffScenarios() {
  String str = "Hello World";

  assertThat("str should contain 'World'", str, containsString("World"));
  assertThat("str should contain 'Hello'", str, containsString("Hello"));
  assertThat("str should contain ' '", str, containsString(" "));

 }
}




Previous                                                 Next                                                 Home

Hamcrest: Matchers to work with strings

Hamcrest provides below matchers to work with strings


Previous                                                 Next                                                 Home

Hamcrest: notNullValue : Creates a matcher that matches if examined object is not null

notNullValue creates a matcher that matches if examined object is not null. It is shortcut to not(nullValue()).

For example, below statement makes the testcase pass, if the firstName of the employee is not null
assertThat("firstName property of emp should not be to null", emp.getFirstName(), notNullValue());

Find the below working application.

Employee.java
package com.sample.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;

 public Employee() {

 }

 public int getId() {
  return id;
 }

 public String getFirstName() {
  return firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setId(int id) {
  this.id = id;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

}

EmployeeTest.java

package com.sample.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

import org.junit.Test;

public class EmployeeTest {

 @Test
 public void employeeConstructor_properInput_firstNameAndLastNameInUpper() {
  Employee emp = new Employee();
  emp.setId(1);
  emp.setFirstName("Krishna");
  emp.setLastName("Gurram");

  assertThat("id property of emp should be set to null", emp.getId(), equalTo(1));
  assertThat("firstName property of emp should not be set to null", emp.getFirstName(), notNullValue());
  assertThat("lastName property of emp should not be set to null", emp.getLastName(), notNullValue());

 }
}

notNullValue() also provides other overloaded method to facilitate the type inference.
public static <T> org.hamcrest.Matcher<T> notNullValue(java.lang.Class<T> type)

Ex
assertThat("firstName property of emp should be set to null", emp.getFirstName(), notNullValue(String.class));


EmployeeTest.java

package com.sample.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

import org.junit.Test;

public class EmployeeTest {

 @Test
 public void employeeConstructor_properInput_firstNameAndLastNameInUpper() {
  Employee emp = new Employee();
  emp.setId(1);
  emp.setFirstName("Krishna");
  emp.setLastName("Gurram");

  assertThat("id property of emp should be set to null", emp.getId(), equalTo(1));
  assertThat("firstName property of emp should not be set to null", emp.getFirstName(), notNullValue(String.class));
  assertThat("lastName property of emp should not be set to null", emp.getLastName(), notNullValue(String.class));

 }
}


Previous                                                 Next                                                 Home

Wednesday, 21 February 2018

Hamcrest: nullValue: Creates a matcher that matches if examined object is null

'nullValue' creates a matcher that matches if the examined object is null.

For example,
assertThat("firstName property of emp should be set to null", emp.getFirstName(), nullValue());

Above statement checks whether employee firstName is null or not.

Find the below working application.

Employee.java
package com.sample.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;

 public Employee() {

 }

 public int getId() {
  return id;
 }

 public String getFirstName() {
  return firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setId(int id) {
  this.id = id;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

}

EmployeeTest.java

package com.sample.model;

import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.equalTo;

public class EmployeeTest {

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

  assertThat("id property of emp should be set to 0", emp.getId(), equalTo(0));
  assertThat("firstName property of emp should be set to null", emp.getFirstName(), nullValue());
  assertThat("lastName property of emp should be set to null", emp.getLastName(), nullValue());

 }
}

'nullValue' provides other overloaded method, where you can facilitate type inference.
public static <T> org.hamcrest.Matcher<T> nullValue(java.lang.Class<T> type)


EmployeeTest.java

package com.sample.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

import org.junit.Test;

public class EmployeeTest {

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

  assertThat("id property of emp should be set to null", emp.getId(), equalTo(0));
  assertThat("firstName property of emp should be set to null", emp.getFirstName(), nullValue(String.class));
  assertThat("lastName property of emp should be set to null", emp.getLastName(), nullValue(String.class));

  Employee emp2 = null;
  assertThat("emp2 should be null", emp2, nullValue(Employee.class));

 }
}



Previous                                                 Next                                                 Home

Hamcrest: Match null values


Hamcrest provides below matchers to check whether object is null or not.

nullValue    : Creates a matcher that matches if examined object is null
notNullValue: Creates a matcher that matches if examined object is not null. It is shortcut to not(nullValue()).



Previous                                                 Next                                                 Home

Hamcrest: Hello World Application

In this post, I am going to show, how to test model class employee.

Requirements
1. Employee should have fields id of type int, firstName of type String and lastName of type String
2. Model class always stores the firstName and lastName in capital letters
        
TestCase
Define Employee object and make sure id, firstName and lastName set correctly.
        
'org.hamcrest.MatcherAssert.assertThat' method is used to test given value against a matcher.

For example,
assertThat("Employee id should be equal to 1", emp.getId(), equalTo(1));

Above statement pass the test case, if getId() of emp returns 1, else fail the test case. 

First argument of assertThat method gives you the reason in failed test scenarios.

Find the below working application.

Employee.java
package com.sample.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;
 
 public Employee(int id, String firstName, String lastName){
  this.id = id;
  this.firstName = firstName.toUpperCase();
  this.lastName = lastName.toUpperCase();
 }

 public int getId() {
  return id;
 }

 public String getFirstName() {
  return firstName;
 }

 public String getLastName() {
  return lastName;
 }

}

EmployeeTest.java
package com.sample.model;

import org.junit.Test;

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

public class EmployeeTest {

 @Test
 public void employeeConstructor_properInput_firstNameAndLastNameInUpper() {
  Employee emp = new Employee(1, "Chamu", "Majety");

  assertThat("Employee id should be equal to 1", emp.getId(), equalTo(1));
  assertThat("Employee firstName should be equal to CHAMU", emp.getFirstName(), equalTo("CHAMU"));
  assertThat("Employee lastName should be equal to MAJETY", emp.getLastName(), equalTo("MAJETY"));

 }
}




Previous                                                 Next                                                 Home

Sunday, 18 February 2018

Introduction to Hamcrest

Hamcrest library provides number of matchers to perform comparison operations. You can use hamcrest and junit libraries together to write effective test cases.

Hamcrest implementation is available in number of languages.

Java                                                             
Python
Ruby
Objective-C
PHP
Erlang
Swift

I am going to use below maven dependencies throughout this tutorial.

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

                 <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
                 <dependency>
                          <groupId>org.hamcrest</groupId>
                          <artifactId>hamcrest-all</artifactId>
                          <version>1.3</version>
                          <scope>test</scope>
                 </dependency>
         </dependencies>
        
In this tutorial series, I am going to show you how to use hamcrest for unit testing.

Can I use Hamcrest with TestNG?
Yes, Hamcrest can be integrated with other testing frameworks like JUnit and TestNG.

Can I use Hamcrest with EasyMock?
yes, Hamcrest provides adaptors for EasyMock.

To make the programs more readable, import below packages.
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matcher.*;

Conventions
Test method always follow the convention like methodName_inputs_expectedOutputs

At the time of writing this tutorial, Hamcrest provides below matchers.
describedAs
is
anything
not
eventFrom
hasXPath

Reference
http://hamcrest.org/




Previous                                                 Next                                                 Home