JUnit - Mastering JUnit Assertions

JUnit Assertions

JUnit assertEquals

Asserting that the values are Equal

Asserting that the values are Equal and configuring an error message if not

Throwing an exception when the values are not equal

As you can see, JUnit accepts an error message and will use that when errors happen. In JUnit5, this message now is reversed and should be added in the last parameter.

The output should be the following error:

Notice that JUnit shows us the expected value and the actual value

JUnit assertTrue and assertFalse

Testing if the boolean value is true

Testing if the boolean value is true and configures an error message if not

Throwing an exception when the boolean value is not true

As you can see, we have an exception being thrown in our face

The same steps can be take for boolean values that should be false, using assertFalse

JUnit assertNull and assertNotNull

Asserting that the value is null

Asserting that the value is not null

JUnit assertSame and assertNotSame

JUnit assertSame will compare if 2 objects is the same object and will not compare its values. Notice that assertEquals compares values. If you try to compare 2 objects with assertEquals, actually JUnit will compare its values based on the equals method.

Let's check the following test, using assertSame

First, we will create the User class to get a better test

As you can see, we didn't overriden the equals method, so the default equals implementation will be used here

Now let's create the test that will check if the user objects are the same

Yeh, this test will fail, since firstUser and secondUser are not the same object, even they have the same value id

Let's compare with assertEquals?

Again, our test will fail because assertEquals will use the equals method implementation, and in this case, we are using the default one.

Now it's time to implement a decent equals methods here!

Let's run the first test again that check if the values are same using assertSame

As you can see, we have the same error as before

Of course we have, since the objects are really different, with different instances.

But let's run the second one

Now the test will pass! Again, assertEquals will actually compare both users using its equals method implementation and not its instances.

As you may guess, we will now try to check if users are the same, but using the same instance

Great! The test will pass!

Hey, but what will happen if we remove the equals implementation? Yes, the test will pass, try it yourself 🙂

As you may guess, we have the assertNotSame method 🙂

This test will pass, since the users instances are different!

JUnit Asserting Arrays with assertArrayEquals

The following test will asserts that two objects arrays are equal.

The test will pass, since each array has the same values.

Different from others JUnit assertions, assertArrayEquals method doesn't have its assertArrayNotEquals equivalent method.

Warning:

You can see tests using assertEquals to compare two object arrays, and you can do that, but is deprecated by JUnit. You should use the new assertArrayEquals method instead.

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

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: