Showing posts with label hamcrest. Show all posts
Showing posts with label hamcrest. Show all posts

Tuesday, 12 July 2022

How to test the ordering of collection or list elements in junit?

Sometimes you may want to check whether given collection has elements in the specific order or not. Using IsIterableContainingInOrder class, you can address this use case.

 


CollectionElementsOrderTest.java

package com.sample.app;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.hamcrest.collection.IsIterableContainingInOrder;
import org.junit.Assert;
import org.junit.Test;

public class CollectionElementsOrderTest {
	
	@Test
	public void collectionTest() {
		final List<Integer> actual = Arrays.asList(11, 3,7, 5, 2);
		Collections.sort(actual);
		
		Assert.assertThat(actual , IsIterableContainingInOrder.contains(2, 3, 5, 7, 11));
	}

}

 

 

 

Previous                                                 Next                                                 Home

Thursday, 5 April 2018

Hamcrest: Combining matchers

Hamcrest provides below matchers to working with number of matchers at a time.


Previous                                                 Next                                                 Home

Wednesday, 4 April 2018

Hamcrest: isOneOf: Matches of altest one element matches

By using ‘isOneOf’ method, you can create a matcher that matches when the examined object is equal to one of the specified elements.

Ex
assertThat("India", isOneOf("Africa", "India", "Sri Lanka"));

TestApp.java

package com.sample.tests;

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

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  assertThat("India", isOneOf("Africa", "India", "Sri Lanka"));
 }
}



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

Hamcrest tutorial

Basics

      Introduction to Hamcrest
      Hello World Application

Test null values

      Match null values
      nullValue: Creates a matcher that matches if examined object is null
      notNullValue : Creates a matcher that matches if examined object is not null

Matchers to work with strings

      Hamcrest: Matchers to work with strings
      containsString: match sub string
      startsWith: Check whether string starts with given string
      endsWith: Check whether string ends with given string
      equalToIgnoringCase: check equality of strings by ignoring case
      equalToIgnoringWhiteSpace: check equality of strings by ignoring case and white spaces
      isEmptyString: Match if the string is empty
      isEmptyOrNullString: Match if the string is empty or null
      stringContainsInOrder: Matches a string against all the substrings

Work with Arrays

      Matchers to work with arrays
       array: Match every element of array with specific matcher
      hasItemInArray: Check the existence of item in the array
      arrayContaining: Match each item in the array
      arrayContainingInAnyOrder: Check array euality, order doesn't matter
      arrayWithSize: Check array length
      emptyArray: match empty array

Compare Objects

      Matchers to compare objects
      greaterThan: check whether given object is greater than this value
      greaterThanOrEqualTo: check whether given object is greater than or equal to this value
      
      lessThanOrEqualTo: Check whether object is <= other object
      comparesEqualTo: Check objects equality

Marchers to check instances

      Marchers to check instances
      instanceOf: Check whether object is instance of class or not
      sameInstance: objects equality checker
      theInstance: objects equality checker
      equalTo: objects equality using equals method

Matchers to work with collections

      Matchers to work with collections
      everyItem: All elements must match to given matcher
      hasItem: Check for the existence of at least one item
      hasItems: Check for the existence of multiple items
      hasSize: Checks the size of collection
      empty: Check emptiness of collection
      emptyCollectionOf: Check the emptiness of collection
      emptyIterable: Check the emptiness of iterables
      emptyIterableOf: Check the emptiness of iterable
      contains: Check for the existence of elements in the collection
      containsInAnyOrder: Search the elements of collection (order doesn’t matter)
      iterableWithSize: Check the size of iterable
      hasEntry: Check for the entry in map
      hasKey: Check for the existence of key
      hasValue: Check the existence of value in map
      isIn: Check the object existence in the collection
      isOneOf: Matches of altest one element matches
      IsIterableContainingInOrder: test the ordering of collection or list elements

Combining matchers

      Combining matchers
      allOf: Match against all the matchers
      both: Match against two matchers
      either: match against either of the matcher
      anyOf: match against any one of the matcher

Working with classes and objects

      Working with classes and objects
      hasProperty: Check whether given object has specific property or not
      isA: Check whether object is of given type
      typeCompatibleWith: Check the compatibility of one type with other
      samePropertyValuesAs: Check two beans properties
      any: Match an object that is instance of class

Working With Numbers

      closeTo: Working with numbers

Previous                                                 Next                                                 Home