1. Overview
1.概述
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
2.示例场景
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 interface. We 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.
让我们探讨一下如何测试实际返回的Tree的类型。
2.1. Testing with JUnit5
2.1.用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:
如果我们想使用JUnit5,我们可以通过使用assertEquals方法检查我们的对象的类是否等于我们要测试的类。
@Test
public void sortTreeShouldReturnEvergreen_WhenPineIsPassed() {
Tree tree = tested.sortTree("Pine");
assertEquals(tree.getClass(), Evergreen.class);
}
2.2. Testing with Hamcrest
2.2.用Hamcrest测试
When using the Hamcrest library, we can use the assertThat and instanceOf methods:
在使用Hamcrest库时,我们可以使用assertThat和instanceOf方法。
@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:
当我们用org.hamcrest.Matchers.isA导入时,有一个快捷的版本可供使用:。
assertThat(tree, isA(Evergreen.class));
2.3. Testing with AssertJ
2.3.用AssertJ测试
We can also use AssertJ Core library’s isExactlyInstanceOf method:
我们还可以使用AssertJ Core library的isExactlyInstanceOf方法。
@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:
另一种完成相同测试的方式是使用hasSameClassAsmethod。
@Test
public void sortTreeShouldReturnDecidious_WhenBirchIsPassed() {
Tree tree = tested.sortTree("Birch");
assertThat(tree).hasSameClassAs(new Deciduous("Birch"));
}
3. Conclusion
3.总结
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.
在本教程中,我们看到了一些在单元测试中验证对象类型的不同例子。我们展示了一个简单的Junit5例子,以及使用Hamcrest和AssertJ库的方法。Hamcrest和AssertJ都在其错误信息中提供了额外的有用信息。
As always, the code for this example is available over on GitHub.
像往常一样,这个例子的代码可以在GitHub上找到。