本文主要是介绍如何用JUnit单元测试List,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题
JUnit测试List时差强人意。
解法
引入依赖
hamcrest-library
包含许多有用方法来测试List
数据类型。
<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope><exclusions><exclusion><groupId>org.hamcrest</groupId><artifactId>hamcrest-core</artifactId></exclusion></exclusions></dependency><!-- This will get hamcrest-core automatically --><dependency><groupId>org.hamcrest</groupId><artifactId>hamcrest-library</artifactId><version>1.3</version><scope>test</scope></dependency>...
断言含有String的List
查阅org.hamcrest.collection
包,它包含许多有用方法来测试Collection
或List
。
import org.junit.Test;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;import org.hamcrest.collection.IsEmptyCollection;import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.hamcrest.MatcherAssert.assertThat;public class StringListTest {@Testpublic void test() {List<String> actual = Arrays.asList("a", "b", "c");List<String> expected = Arrays.asList("a", "b", "c");// All passed / true// 1. Test equal.assertThat(actual, is(expected));// 2. If List has this value?assertThat(actual, hasItems("b"));// 3. Check List SizeassertThat(actual, hasSize(3));assertThat(actual.size(), is(3));// 4. List order// Ensure Correct orderassertThat(actual, contains("a", "b", "c"));// Can be any orderassertThat(actual, containsInAnyOrder("c", "b", "a"));// 5. check empty listassertThat(actual, not(IsEmptyCollection.empty()));assertThat(new ArrayList<>(), IsEmptyCollection.empty());}
}
断言含有Integer的List
查阅org.hamcrest.number
包,它包含许多有用方法来测试数目。
import org.junit.Test;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;import org.hamcrest.collection.IsEmptyCollection;import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo;
import static org.hamcrest.number.OrderingComparison.lessThan;import static org.hamcrest.MatcherAssert.assertThat;public class IntegerListTest {@Testpublic void test() {List<Integer> actual = Arrays.asList(1, 2, 3, 4, 5);List<Integer> expected = Arrays.asList(1, 2, 3, 4, 5);// All passed / true// 1. Test equal.assertThat(actual, is(expected));// 2. Check List has this valueassertThat(actual, hasItems(2));// 3. Check List SizeassertThat(actual, hasSize(5));assertThat(actual.size(), is(5));// 4. List order// Ensure Correct orderassertThat(actual, contains(1, 2, 3, 4, 5));// Can be any orderassertThat(actual, containsInAnyOrder(5, 4, 3, 2, 1));// 5. check empty listassertThat(actual, not(IsEmptyCollection.empty()));assertThat(new ArrayList<>(), IsEmptyCollection.empty());// 6. Test numeric comparisonsassertThat(actual, everyItem(greaterThanOrEqualTo(1)));assertThat(actual, everyItem(lessThan(10)));}}
Note
Bothorg.hamcrest.collection
andorg.hamcrest.number
are belong tohamcrest-library
。
断言含有Object的List
一个POJO类:
import java.util.Objects;public class Fruit {public Fruit(String name, int qty) {this.name = name;this.qty = qty;}private String name;private int qty;public int getQty() {return qty;}public void setQty(int qty) {this.qty = qty;}public String getName() {return name;}public void setName(String name) {this.name = name;}// Test equal, override equals() and hashCode()@Overridepublic boolean equals(Object o) {if (this == o)return true;if (o == null || getClass() != o.getClass())return false;Fruit fruit = (Fruit) o;return qty == fruit.qty && Objects.equals(name, fruit.name);}@Overridepublic int hashCode() {return Objects.hash(name, qty);}
}
测试类:
import org.junit.Test;import java.util.Arrays;
import java.util.List;import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.Assert.assertThat;public class ObjectListTest {@Test@SuppressWarnings("unchecked")public void test() {List<Fruit> list = Arrays.asList(new Fruit("Banana", 99), new Fruit("Apple", 20));// Test equalsassertThat(list, hasItems(new Fruit("Banana", 99), //new Fruit("Apple", 20)));assertThat(list, containsInAnyOrder(new Fruit("Apple", 20), // new Fruit("Banana", 99)));// Test class property, and its valueassertThat(list, containsInAnyOrder(hasProperty("name", is("Apple")), hasProperty("name", is("Banana"))));}
}
参考
- JUnit – How to test a List
- Hamcrest official site
这篇关于如何用JUnit单元测试List的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!