JUnit - How to Use Hamcrest Assertions

JUnit - How to Use Hamcrest Assertions

Simple Definition

Hamcrest is a great library that can bring us more expressiveness than JUnit default assertions.

Hamcrest and its own definition

  • Hamcrest are matchers that can be combined to create flexible expressions of intent.

You will see that this definition is really true!

Configuring Hamcrest

JUnit has an integration with the Hamcrest library and you have 2 options of how to use Hamcrest

  • Use Hamcrest from JUnit dependency
  • Use Hamcrest by its ow dependencies

In our case we will use Hamcrest dependency that we got together with JUnit.

Asserting Equal Values with Hamcrest

In this first test, we wil see how to assert that 2 String values are equal by using Hamcrest

Notice that we didn't use the default assertEquals from JUnit. Instead, we used the assertThat.

We will see in the Hamcrest series that there are differences when we are using Hamcrest from JUnit and Hamcrest separately.

Of couse, with static imports, our code is better

Asserting that Values are not Equals

The expressiveness that we got with Hamcrest is really awesome! In the following test we are using CoreMatchers.not() method to negate the Matcher:

Asserting with Hamcrest that String Value Contains Specific Word

In the following example, we will check if the String value contains a specific word

Let's create another great example, asserting that the String value contains 2 or more specific words

Now using static import

Great!

Asserting with Hamcrest that an Array has specific Items

We can check if an array has specific items in it by using CoreMatchers.hasItems() method

Expressiveness again! \o/

Asserting with Hamcrest that Every Item contains a Specific Letter

Now we will check if every item in the array contains a specific letter by using CoreMatchers.everyItem() method

We going to see a lot of more examples in the Hamcrest Guide Series!

That's it!

Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
JUnit Tutorial - How to Configure JUnit 4 and JUnit 5 - Java Tutorial by Hacking Code

JUnit - How to Configure JUnit 4 and JUnit 5

Do you know the differences between JUnit 4 and JUnit 5 Configuration? Let's see!
JUnit Tutorial - Mastering Assertions - Java Tutorial by Hacking Code

JUnit - Mastering JUnit Assertions

Are you ready to see a lot of JUnit Examples of how to use Assertions?
JUnit Tutorial - Hamcrest Assertions - Java Tutorial by Hacking Code

JUnit - How to Use Hamcrest Assertions

Hamcrest Assertions is a great way to get your tests easier to read! Let's see how!
JUnit Tutorial - Tests in Suites - Java Tutorial by Hacking Code

JUnit - Tests in Suite

You can organize your tests in a Suite mode!
JUnit Tutorial - Test Execution Order - Java Tutorial by Hacking Code

JUnit - Test Execution Order

Do you want order your test execution? Probably it is a bed smell, but let's try it out!
JUnit Tutorial - Testing Exceptions - Java Tutorial by Hacking Code

JUnit - Testing Exceptions

Java Exceptions are in all places right? Let's deal with that with honor!

0 Comments

Trackbacks/Pingbacks

  1. JUnit – Mastering JUnit Assertions | Hacking Code - […] 5 […]

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: