Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this article, we’ll explore how we can verify that an object is of a specific type. We’ll be looking at different testing libraries and what methods they offer to assert the object type.

The scenario in which we might need to do this can vary. A common one is when we utilize an interface as a return type of a method but then, according to the specific object returned, we want to perform different operations. Unit tests can help us determine if the object returned has the class we expect.

2. Example Scenario

Let’s imagine that we are sorting Trees according to whether they lose their leaves over winter or not. We have two classes, Evergreen and Deciduous, both implementing a Tree interfaceWe have a simple sorter that returns the correct type according to the name of the tree:

Tree sortTree(String name) {

    List<String> deciduous = List.of("Beech", "Birch", "Ash", "Whitebeam", "Hornbeam", "Hazel & Willow");
    List<String> evergreen = List.of("Cedar", "Holly", "Laurel", "Olive", "Pine");

    if (deciduous.contains(name)) {
        return new Deciduous(name);
    } else if (evergreen.contains(name)) {
        return new Evergreen(name);
    } else {
        throw new RuntimeException("Tree could not be classified");
    }
}

Let’s explore how we can test what type of Tree is actually returned.

2.1. Testing with JUnit5

If we want to use JUnit5, we can check if the class of our object equals the class we are testing against by using the assertEquals method:

@Test
public void sortTreeShouldReturnEvergreen_WhenPineIsPassed() {
    Tree tree = tested.sortTree("Pine");
    assertEquals(tree.getClass(), Evergreen.class);
}

2.2. Testing with Hamcrest

When using the Hamcrest library, we can use the assertThat and instanceOf methods:

@Test
public void sortTreeShouldReturnEvergreen_WhenPineIsPassed() {
Tree tree = tested.sortTree("Pine");
assertThat(tree, instanceOf(Evergreen.class));
}

There is a shortcut version available to us when we import with org.hamcrest.Matchers.isA:

assertThat(tree, isA(Evergreen.class));

2.3. Testing with AssertJ

We can also use AssertJ Core library’s isExactlyInstanceOf method:

@Test
public void sortTreeShouldReturnEvergreen_WhenPineIsPassed() {
    Tree tree = tested.sortTree("Pine");
    assertThat(tree).isExactlyInstanceOf(Evergreen.class);
}

Another way to accomplish the same test is with the hasSameClassAs method:

@Test
public void sortTreeShouldReturnDecidious_WhenBirchIsPassed() {
    Tree tree = tested.sortTree("Birch");
    assertThat(tree).hasSameClassAs(new Deciduous("Birch"));
}

3. Conclusion

In this tutorial, we’ve seen a few different examples of verifying the type of an object in unit tests. We’ve shown a simple Junit5 example as well as using methods of Hamcrest and AssertJ libraries. Both Hamcrest and AssertJ offer additional helpful information in their error messages.

As always, the code for this example is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.