From 857cbac81c591760cae0490195d8130680a4b0e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Wed, 9 Dec 2020 20:03:19 +0800 Subject: [PATCH] add demo for template method mocking --- .../alibaba/testable/demo/DemoTemplate.java | 35 +++++++++++ .../testable/demo/DemoInheritTest.java | 16 +++-- .../testable/demo/DemoMatcherTest.java | 4 ++ .../alibaba/testable/demo/DemoMockTest.java | 11 +++- .../testable/demo/DemoPrivateAccessTest.java | 14 +++-- .../testable/demo/DemoTemplateTest.java | 63 +++++++++++++++++++ 6 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 demo/java-demo/src/main/java/com/alibaba/testable/demo/DemoTemplate.java create mode 100644 demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoTemplateTest.java diff --git a/demo/java-demo/src/main/java/com/alibaba/testable/demo/DemoTemplate.java b/demo/java-demo/src/main/java/com/alibaba/testable/demo/DemoTemplate.java new file mode 100644 index 0000000..d2e52ad --- /dev/null +++ b/demo/java-demo/src/main/java/com/alibaba/testable/demo/DemoTemplate.java @@ -0,0 +1,35 @@ +package com.alibaba.testable.demo; + +import java.util.*; + +public class DemoTemplate { + + private List getList(T value) { + List l = new ArrayList<>(); + l.add(value); + return l; + } + + private Map getMap(K key, V value) { + Map m = new HashMap<>(); + m.put(key, value); + return m; + } + + public String singleTemplateMethod() { + List list = getList("demo"); + return list.get(0); + } + + public String doubleTemplateMethod() { + Map map = getMap("hello", "testable"); + return map.get("hello"); + } + + public Set newTemplateMethod() { + Set set = new HashSet<>(); + set.add("world"); + return set; + } + +} diff --git a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoInheritTest.java b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoInheritTest.java index 873e5b8..baf5a3e 100644 --- a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoInheritTest.java +++ b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoInheritTest.java @@ -9,6 +9,10 @@ import org.junit.jupiter.api.Test; import static com.alibaba.testable.core.matcher.InvokeVerifier.verify; import static org.junit.jupiter.api.Assertions.*; +/** + * 演示父类变量引用子类对象时的Mock场景 + * Demonstrate scenario of mocking method from sub-type object referred by parent-type variable + */ class DemoInheritTest { @TestableMock(targetMethod = "put") @@ -44,42 +48,42 @@ class DemoInheritTest { private DemoInherit demoInherit = new DemoInherit(); @Test - void should_able_to_mock_call_sub_object_method_by_parent_object() throws Exception { + void should_able_to_mock_call_sub_object_method_by_parent_object() { BlackBox box = (BlackBox)demoInherit.putIntoBox(); verify("put_into_box").withTimes(1); assertEquals("put_data_into_box", box.get()); } @Test - void should_able_to_mock_call_sub_object_method_by_sub_object() throws Exception { + void should_able_to_mock_call_sub_object_method_by_sub_object() { BlackBox box = demoInherit.putIntoBlackBox(); verify("put_into_blackbox").withTimes(1); assertEquals("put_data_into_blackbox", box.get()); } @Test - void should_able_to_mock_call_parent_object_method_by_parent_object() throws Exception { + void should_able_to_mock_call_parent_object_method_by_parent_object() { String content = demoInherit.getFromBox(); verify("get_from_box").withTimes(1); assertEquals("get_from_box", content); } @Test - void should_able_to_mock_call_parent_object_method_by_sub_object() throws Exception { + void should_able_to_mock_call_parent_object_method_by_sub_object() { String content = demoInherit.getFromBlackBox(); verify("get_from_blackbox").withTimes(1); assertEquals("get_from_blackbox", content); } @Test - void should_able_to_mock_call_interface_method_by_interface_object() throws Exception { + void should_able_to_mock_call_interface_method_by_interface_object() { String color = demoInherit.getColorViaColor(); verify("get_color_from_color").withTimes(1); assertEquals("color_from_color", color); } @Test - void should_able_to_mock_call_interface_method_by_sub_class_object() throws Exception { + void should_able_to_mock_call_interface_method_by_sub_class_object() { String color = demoInherit.getColorViaBox(); verify("get_color_from_blackbox").withTimes(1); assertEquals("color_from_blackbox", color); diff --git a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoMatcherTest.java b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoMatcherTest.java index b115e83..cae529a 100644 --- a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoMatcherTest.java +++ b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoMatcherTest.java @@ -9,6 +9,10 @@ import static com.alibaba.testable.core.matcher.InvokeMatcher.*; import static com.alibaba.testable.core.matcher.InvokeVerifier.verify; import static org.junit.jupiter.api.Assertions.fail; +/** + * 演示Mock方法调用校验器 + * Demonstrate mock method invocation verifier + */ class DemoMatcherTest { private DemoMatcher demoMatcher = new DemoMatcher(); diff --git a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoMockTest.java b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoMockTest.java index 9afe377..9126ac1 100644 --- a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoMockTest.java +++ b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoMockTest.java @@ -4,12 +4,17 @@ import com.alibaba.testable.core.annotation.TestableMock; import com.alibaba.testable.demo.model.BlackBox; import org.junit.jupiter.api.Test; +import java.io.IOException; import java.util.concurrent.Executors; import static com.alibaba.testable.core.matcher.InvokeVerifier.verify; import static com.alibaba.testable.core.tool.TestableTool.*; import static org.junit.jupiter.api.Assertions.assertEquals; +/** + * 演示基本的Mock功能 + * Demonstrate basic mock functionality + */ class DemoMockTest { @TestableMock(targetMethod = CONSTRUCTOR) @@ -56,7 +61,7 @@ class DemoMockTest { private DemoMock demoMock = new DemoMock(); @Test - void should_able_to_mock_new_object() throws Exception { + void should_able_to_mock_new_object() { assertEquals("mock_something", demoMock.newFunc()); verify("createBlackBox").with("something"); } @@ -68,7 +73,7 @@ class DemoMockTest { } @Test - void should_able_to_mock_common_method() throws Exception { + void should_able_to_mock_common_method() { assertEquals("trim_string__sub_string__false", demoMock.commonFunc()); verify("trim").withTimes(1); verify("sub").withTimes(1); @@ -76,7 +81,7 @@ class DemoMockTest { } @Test - void should_able_to_mock_static_method() throws Exception { + void should_able_to_mock_static_method() { assertEquals("not_secret_box", demoMock.getBox().get()); verify("secretBox").withTimes(1); } diff --git a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoPrivateAccessTest.java b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoPrivateAccessTest.java index cafb0f3..a72772d 100644 --- a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoPrivateAccessTest.java +++ b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoPrivateAccessTest.java @@ -6,19 +6,23 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +/** + * 演示私有成员访问功能 + * Demonstrate private member access functionality + */ @EnablePrivateAccess class DemoPrivateAccessTest { private DemoPrivateAccess demoPrivateAccess = new DemoPrivateAccess(); @Test - void should_able_to_access_private_method() throws Exception { + void should_able_to_access_private_method() { assertEquals("hello - 1", demoPrivateAccess.privateFunc("hello", 1)); assertEquals("hello - 1", PrivateAccessor.invoke(demoPrivateAccess, "privateFunc", "hello", 1)); } @Test - void should_able_to_access_private_field() throws Exception { + void should_able_to_access_private_field() { demoPrivateAccess.count = 2; assertEquals(new Integer(2), demoPrivateAccess.count); @@ -27,13 +31,13 @@ class DemoPrivateAccessTest { } @Test - void should_able_to_access_private_static_method() throws Exception { + void should_able_to_access_private_static_method() { //assertEquals("hello + 1", DemoPrivateAccess.privateStaticFunc("hello", 1)); assertEquals("hello + 1", PrivateAccessor.invokeStatic(DemoPrivateAccess.class, "privateStaticFunc", "hello", 1)); } @Test - void should_able_to_access_private_static_field() throws Exception { + void should_able_to_access_private_static_field() { //DemoPrivateAccess.staticCount = 2; //assertEquals(new Integer(2), DemoPrivateAccess.staticCount); @@ -42,7 +46,7 @@ class DemoPrivateAccessTest { } @Test - void should_able_to_update_final_field() throws Exception { + void should_able_to_update_final_field() { demoPrivateAccess.pi = 4.13; assertEquals(4.13, demoPrivateAccess.pi); } diff --git a/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoTemplateTest.java b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoTemplateTest.java new file mode 100644 index 0000000..9443f48 --- /dev/null +++ b/demo/java-demo/src/test/java/com/alibaba/testable/demo/DemoTemplateTest.java @@ -0,0 +1,63 @@ +package com.alibaba.testable.demo; + +import com.alibaba.testable.core.annotation.TestableMock; +import com.alibaba.testable.core.tool.TestableTool; +import org.junit.jupiter.api.Test; + +import java.util.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * 演示模板方法可以被Mock + * Demonstrate template method can be mocked + */ +class DemoTemplateTest { + + private DemoTemplate demoTemplate = new DemoTemplate(); + + @TestableMock + private List getList(DemoTemplate self, T value) { + return new ArrayList() {{ add((T)(value.toString() + "_mock_list")); }}; + } + + @TestableMock + private Map getMap(DemoTemplate self, K key, V value) { + return new HashMap() {{ put(key, (V)(value.toString() + "_mock_map")); }}; + } + + @TestableMock(targetMethod = TestableTool.CONSTRUCTOR) + public HashSet newHashSet() { + HashSet set = new HashSet<>(); + set.add("insert_mock"); + return set; + } + + @TestableMock + private boolean add(Set s, E e) { + s.add(e.toString() + "_mocked"); + return true; + } + + @Test + void should_able_to_mock_single_template_method() { + String res = demoTemplate.singleTemplateMethod(); + assertEquals("demo_mock_list", res); + } + + @Test + void should_able_to_mock_double_template_method() { + String res = demoTemplate.doubleTemplateMethod(); + assertEquals("testable_mock_map", res); + } + + @Test + void should_able_to_mock_new_template_method() { + Set res = demoTemplate.newTemplateMethod(); + assertEquals(2, res.size()); + Iterator iterator = res.stream().iterator(); + assertEquals("insert_mock", iterator.next()); + assertEquals("world_mocked", iterator.next()); + } + +}