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

No comments:

Post a Comment