CoreMatchers和Assert配合做junit
2015-12-15 18:53
585 查看
最近在读multi-thread context(MTC)源码,发现一个好用的工具:CoreMatchers,其提供了很多匹配器,对于junit相对单薄的断言功能是种很好的补充
而CoreMatchers的中文说明非常少,也很少见人使用,先展示一段测试代码
好在方法命名都很简单, 附上其java doc
而CoreMatchers的中文说明非常少,也很少见人使用,先展示一段测试代码
import com.alibaba.mtc.testmodel.Call; import org.junit.AfterClass; import org.junit.Test; import java.util.Arrays; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import static com.alibaba.mtc.Utils.CHILD; import static com.alibaba.mtc.Utils.PARENT_AFTER_CREATE_MTC_TASK; import static com.alibaba.mtc.Utils.PARENT_MODIFIED_IN_CHILD; import static com.alibaba.mtc.Utils.PARENT_UNMODIFIED_IN_CHILD; import static com.alibaba.mtc.Utils.assertMtContext; import static com.alibaba.mtc.Utils.copied; import static com.alibaba.mtc.Utils.createTestMtContexts; import static com.alibaba.mtc.Utils.expandThreadPool; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; public class MtContextCallableTest { static ExecutorService executorService = Executors.newFixedThreadPool(3); static { expandThreadPool(executorService); } @AfterClass public static void afterClass() throws Exception { executorService.shutdown(); } @Test public void test_MtContextCallable_inSameThread() throws Exception { ConcurrentMap<String, MtContextThreadLocal<String>> mtContexts = createTestMtContexts(); Call call = new Call("1", mtContexts); MtContextCallable<String> mtContextCallable = MtContextCallable.<String>get(call); // create after new Task, won't see parent value in in task! MtContextThreadLocal<String> after = new MtContextThreadLocal<String>(); after.set(PARENT_AFTER_CREATE_MTC_TASK); mtContexts.put(PARENT_AFTER_CREATE_MTC_TASK, after); String ret = mtContextCallable.call(); assertEquals("ok", ret); // child Inheritable assertMtContext(call.copied, PARENT_UNMODIFIED_IN_CHILD, PARENT_UNMODIFIED_IN_CHILD, PARENT_MODIFIED_IN_CHILD + 1, PARENT_MODIFIED_IN_CHILD, CHILD + 1, CHILD + 1 ); // child do not effect parent assertMtContext(copied(mtContexts), PARENT_UNMODIFIED_IN_CHILD, PARENT_UNMODIFIED_IN_CHILD, PARENT_MODIFIED_IN_CHILD, PARENT_MODIFIED_IN_CHILD, // restored after call! PARENT_AFTER_CREATE_MTC_TASK, PARENT_AFTER_CREATE_MTC_TASK ); } @Test public void test_MtContextCallable_asyncWithExecutorService() throws Exception { ConcurrentMap<String, MtContextThreadLocal<String>> mtContexts = createTestMtContexts(); Call call = new Call("1", mtContexts); MtContextCallable<String> mtContextCallable = MtContextCallable.get(call); // create after new Task, won't see parent value in in task! MtContextThreadLocal<String> after = new MtContextThreadLocal<String>(); after.set(PARENT_AFTER_CREATE_MTC_TASK); mtContexts.put(PARENT_AFTER_CREATE_MTC_TASK, after); Future future = executorService.submit(mtContextCallable); assertEquals("ok", future.get()); // child Inheritable assertMtContext(call.copied, PARENT_UNMODIFIED_IN_CHILD, PARENT_UNMODIFIED_IN_CHILD, PARENT_MODIFIED_IN_CHILD + 1, PARENT_MODIFIED_IN_CHILD, CHILD + 1, CHILD + 1 ); // child do not effect parent assertMtContext(copied(mtContexts), PARENT_UNMODIFIED_IN_CHILD, PARENT_UNMODIFIED_IN_CHILD, PARENT_MODIFIED_IN_CHILD, PARENT_MODIFIED_IN_CHILD, PARENT_AFTER_CREATE_MTC_TASK, PARENT_AFTER_CREATE_MTC_TASK ); } @Test public void test_removeSameAsNotSet() throws Exception { ConcurrentMap<String, MtContextThreadLocal<String>> mtContexts = createTestMtContexts(); mtContexts.get(PARENT_UNMODIFIED_IN_CHILD).remove(); Call call = new Call("1", mtContexts); MtContextCallable<String> mtContextCallable = MtContextCallable.get(call); // create after new Task, won't see parent value in in task! MtContextThreadLocal<String> after = new MtContextThreadLocal<String>(); after.set(PARENT_AFTER_CREATE_MTC_TASK); mtContexts.put(PARENT_AFTER_CREATE_MTC_TASK, after); Future future = executorService.submit(mtContextCallable); assertEquals("ok", future.get()); // child Inheritable assertMtContext(call.copied, PARENT_MODIFIED_IN_CHILD + 1, PARENT_MODIFIED_IN_CHILD, CHILD + 1, CHILD + 1 ); // child do not effect parent assertMtContext(copied(mtContexts), PARENT_MODIFIED_IN_CHILD, PARENT_MODIFIED_IN_CHILD, PARENT_AFTER_CREATE_MTC_TASK, PARENT_AFTER_CREATE_MTC_TASK ); } @Test public void test_releaseMtContextAfterCall() throws Exception { ConcurrentMap<String, MtContextThreadLocal<String>> mtContexts = createTestMtContexts(); Call call = new Call("1", mtContexts); MtContextCallable<String> mtContextCallable = MtContextCallable.get(call, true); assertSame(call, mtContextCallable.getCallable()); Future future = executorService.submit(mtContextCallable); assertEquals("ok", future.get()); future = executorService.submit(mtContextCallable); try { future.get(); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause(), instanceOf(IllegalStateException.class)); assertThat(expected.getMessage(), containsString("MtContext is released!")); } } @Test public void test_get_same() throws Exception { Call call = new Call("1", null); MtContextCallable<String> mtContextCallable = MtContextCallable.get(call); assertSame(call, mtContextCallable.getCallable()); } @Test public void test_get_idempotent() throws Exception { MtContextCallable<String> call = MtContextCallable.get(new Call("1", null)); try { MtContextCallable.get(call); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage(), containsString("Already MtContextCallable")); } } @Test public void test_get_nullInput() throws Exception { assertNull(MtContextCallable.get(null)); } @Test public void test_gets() throws Exception { Callable<String> call1 = new Call("1", null); Callable<String> call2 = new Call("1", null); Callable<String> call3 = new Call("1", null); @SuppressWarnings("unchecked") List<MtContextCallable<String>> callList = MtContextCallable.gets( Arrays.asList(call1, call2, null, call3)); assertEquals(4, callList.size()); assertThat(callList.get(0), instanceOf(MtContextCallable.class)); assertThat(callList.get(1), instanceOf(MtContextCallable.class)); assertNull(callList.get(2)); assertThat(callList.get(3), instanceOf(MtContextCallable.class)); } }
好在方法命名都很简单, 附上其java doc
限定符和类型 | 方法和说明 |
---|---|
static <T> Matcher<T> | allOf(java.lang.Iterable<Matcher<? super T>> matchers) Creates a matcher that matches if the examined object matches ALL of the specified matchers. |
static <T> Matcher<T> | allOf(Matcher<? super T>... matchers) Creates a matcher that matches if the examined object matches ALL of the specified matchers. |
static <T> Matcher<T> | allOf(Matcher<? super T> first, Matcher<? super T> second) Creates a matcher that matches if the examined object matches ALL of the specified matchers. |
static <T> Matcher<T> | allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third) Creates a matcher that matches if the examined object matches ALL of the specified matchers. |
static <T> Matcher<T> | allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) Creates a matcher that matches if the examined object matches ALL of the specified matchers. |
static <T> Matcher<T> | allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth) Creates a matcher that matches if the examined object matches ALL of the specified matchers. |
static <T> Matcher<T> | allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth, Matcher<? super T> sixth) Creates a matcher that matches if the examined object matches ALL of the specified matchers. |
static <T> Matcher<T> | any(java.lang.Class<T> type) Creates a matcher that matches when the examined object is an instance of the specified type, as determined by calling the Class.isInstance(Object)method on that type, passing the the examined object. |
static <T> AnyOf<T> | anyOf(java.lang.Iterable<Matcher<? super T>> matchers) Creates a matcher that matches if the examined object matches ANY of the specified matchers. |
static <T> AnyOf<T> | anyOf(Matcher<? super T>... matchers) Creates a matcher that matches if the examined object matches ANY of the specified matchers. |
static <T> AnyOf<T> | anyOf(Matcher<T> first, Matcher<? super T> second) Creates a matcher that matches if the examined object matches ANY of the specified matchers. |
static <T> AnyOf<T> | anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third) Creates a matcher that matches if the examined object matches ANY of the specified matchers. |
static <T> AnyOf<T> | anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) Creates a matcher that matches if the examined object matches ANY of the specified matchers. |
static <T> AnyOf<T> | anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth) Creates a matcher that matches if the examined object matches ANY of the specified matchers. |
static <T> AnyOf<T> | anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth, Matcher<? super T> sixth) Creates a matcher that matches if the examined object matches ANY of the specified matchers. |
static Matcher<java.lang.Object> | anything() Creates a matcher that always matches, regardless of the examined object. |
static Matcher<java.lang.Object> | anything(java.lang.String description) Creates a matcher that always matches, regardless of the examined object, but describes itself with the specified String. |
static <LHS> CombinableMatcher.CombinableBothMatcher<LHS> | both(Matcher<? super LHS> matcher) Creates a matcher that matches when both of the specified matchers match the examined object. |
static Matcher<java.lang.String> | containsString(java.lang.String substring) Creates a matcher that matches if the examined Stringcontains the specified Stringanywhere. |
static <T> Matcher<T> | describedAs(java.lang.String description, Matcher<T> matcher, java.lang.Object... values) Wraps an existing matcher, overriding its description with that specified. |
static <LHS> CombinableMatcher.CombinableEitherMatcher<LHS> | either(Matcher<? super LHS> matcher) Creates a matcher that matches when either of the specified matchers match the examined object. |
static Matcher<java.lang.String> | endsWith(java.lang.String suffix) Creates a matcher that matches if the examined Stringends with the specified String. |
static <T> Matcher<T> | equalTo(T operand) Creates a matcher that matches when the examined object is logically equal to the specified operand, as determined by calling the Object.equals(java.lang.Object)method on the examinedobject. |
static <U> Matcher<java.lang.Iterable<U>> | everyItem(Matcher<U> itemMatcher) Creates a matcher for Iterables that only matches when a single pass over the examined Iterableyields items that are all matched by the specified itemMatcher. |
static <T> Matcher<java.lang.Iterable<? super T>> | hasItem(Matcher<? super T> itemMatcher) Creates a matcher for Iterables that only matches when a single pass over the examined Iterableyields at least one item that is matched by the specified itemMatcher. |
static <T> Matcher<java.lang.Iterable<? super T>> | hasItem(T item) Creates a matcher for Iterables that only matches when a single pass over the examined Iterableyields at least one item that is equal to the specified item. |
static <T> Matcher<java.lang.Iterable<T>> | hasItems(Matcher<? super T>... itemMatchers) Creates a matcher for Iterables that matches when consecutive passes over the examined Iterableyield at least one item that is matched by the corresponding matcher from the specified itemMatchers. |
static <T> Matcher<java.lang.Iterable<T>> | hasItems(T... items) Creates a matcher for Iterables that matches when consecutive passes over the examined Iterableyield at least one item that is equal to the corresponding item from the specified items. |
static <T> Matcher<T> | instanceOf(java.lang.Class<?> type) Creates a matcher that matches when the examined object is an instance of the specified type, as determined by calling the Class.isInstance(Object)method on that type, passing the the examined object. |
static <T> Matcher<T> | is(java.lang.Class<T> type) 已过时。 use isA(Class type) instead. |
static <T> Matcher<T> | is(Matcher<T> matcher) Decorates another Matcher, retaining its behaviour, but allowing tests to be slightly more expressive. |
static <T> Matcher<T> | is(T value) A shortcut to the frequently used is(equalTo(x)). |
static <T> Matcher<T> | isA(java.lang.Class<T> type) A shortcut to the frequently used is(instanceOf(SomeClass.class)). |
static <T> Matcher<T> | not(Matcher<T> matcher) Creates a matcher that wraps an existing matcher, but inverts the logic by which it will match. |
static <T> Matcher<T> | not(T value) A shortcut to the frequently used not(equalTo(x)). |
static Matcher<java.lang.Object> | notNullValue() A shortcut to the frequently used not(nullValue()). |
static <T> Matcher<T> | notNullValue(java.lang.Class<T> type) A shortcut to the frequently used not(nullValue(X.class)). |
static Matcher<java.lang.Object> | nullValue() Creates a matcher that matches if examined object is null. |
static <T> Matcher<T> | nullValue(java.lang.Class<T> type) Creates a matcher that matches if examined object is null. |
static <T> Matcher<T> | sameInstance(T target) Creates a matcher that matches only when the examined object is the same instance as the specified target object. |
static Matcher<java.lang.String> | startsWith(java.lang.String prefix) Creates a matcher that matches if the examined Stringstarts with the specified String. |
static <T> Matcher<T> | theInstance(T target) Creates a matcher that matches only when the examined object is the same instance as the specified target object. |
相关文章推荐
- 【Nginx】优化配置
- JavaScript异常捕获、事件
- DNS隧道之DNS2TCP实现
- iOS开发笔记(2)
- iOS开发笔记(1)
- 简单的彷spring注解初始化action
- HDOJ--2016
- 关于一些对location认识的误区(转)
- li下沉 margin-top越界 浮动带来的影响
- C语言08
- Maven工程整合SpringMVC和ibatis和Spring
- 字符串哈希函数
- 网络系统开发 20151215
- POJ 2533-Longest Ordered Subsequence(DP)
- iOS开发环境C语言基础
- C语言07
- UVa 111 - History Grading (by 最长公共子序列 )
- 网络包分析--tcp数据发送不出去
- CAS实现SSO单点登录原理
- C语言06