- Asserting a test script determines whether the test actually passed or failed.
- We can assert that a test passed by using Junit assertion or Hamcrest core matchers with assertThat( )
- Junit Assert can be used by importing : org.junit.Assert;
- Junit Assert generally includes the following assertions:
Image may be NSFW.
Clik here to view.
- Hamcrest Core Matchers used with assertThat( ) increases the readability of the assertions made in the tests.
- Hamcrest Core Matchers can be used by importing : static org.hamcrest.CoreMatchers.*;
- Some of the hamcrest core matchers are: is( ) , is(not( )), everyItem( ), either( ).or( ), both( ).and( ), hastItem( ), hasItems( ), containsString( )
- When Hamcrest Core Matchers is used with assertThat( ), the first parameter is the actual result and the second is the matcher.
- AssertThat( ) can be used by importing: static org.junit.Assert.assertThat;
- Some examples of matchers with assertThat are as follows:
int x =5; assertThat(x,is(5)); assertThat(x,is(not(3))); ArrayList<String> test = new ArrayList<String>(); assertThat(test,hasItem("Mindfire")); assertThat(test, hasItems("Mindfire","Testing")); assertThat(test.get(0),containsString("Mindfire")); assertThat(test.get(0),either(containsString("Mindfire")).or(containsString("Testing"))); assertThat(test.get(0),both(containsString("Mindfire")).and(containsString("Test ing"))); assertThat(test,everyItem(containsString("Testing")));
Written By: - Jaya Mohanty, QA Engineer, Mindfire Solutions
Image may be NSFW.
Clik here to view.

Clik here to view.
