mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-10 20:30:11 +08:00
add demo for template method mocking
This commit is contained in:
parent
495c08f716
commit
857cbac81c
@ -0,0 +1,35 @@
|
|||||||
|
package com.alibaba.testable.demo;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class DemoTemplate {
|
||||||
|
|
||||||
|
private <T> List<T> getList(T value) {
|
||||||
|
List<T> l = new ArrayList<>();
|
||||||
|
l.add(value);
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
private <K, V> Map<K, V> getMap(K key, V value) {
|
||||||
|
Map<K, V> m = new HashMap<>();
|
||||||
|
m.put(key, value);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String singleTemplateMethod() {
|
||||||
|
List<String> list = getList("demo");
|
||||||
|
return list.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String doubleTemplateMethod() {
|
||||||
|
Map<String, String> map = getMap("hello", "testable");
|
||||||
|
return map.get("hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<?> newTemplateMethod() {
|
||||||
|
Set<String> set = new HashSet<>();
|
||||||
|
set.add("world");
|
||||||
|
return set;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -9,6 +9,10 @@ import org.junit.jupiter.api.Test;
|
|||||||
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示父类变量引用子类对象时的Mock场景
|
||||||
|
* Demonstrate scenario of mocking method from sub-type object referred by parent-type variable
|
||||||
|
*/
|
||||||
class DemoInheritTest {
|
class DemoInheritTest {
|
||||||
|
|
||||||
@TestableMock(targetMethod = "put")
|
@TestableMock(targetMethod = "put")
|
||||||
@ -44,42 +48,42 @@ class DemoInheritTest {
|
|||||||
private DemoInherit demoInherit = new DemoInherit();
|
private DemoInherit demoInherit = new DemoInherit();
|
||||||
|
|
||||||
@Test
|
@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();
|
BlackBox box = (BlackBox)demoInherit.putIntoBox();
|
||||||
verify("put_into_box").withTimes(1);
|
verify("put_into_box").withTimes(1);
|
||||||
assertEquals("put_data_into_box", box.get());
|
assertEquals("put_data_into_box", box.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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();
|
BlackBox box = demoInherit.putIntoBlackBox();
|
||||||
verify("put_into_blackbox").withTimes(1);
|
verify("put_into_blackbox").withTimes(1);
|
||||||
assertEquals("put_data_into_blackbox", box.get());
|
assertEquals("put_data_into_blackbox", box.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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();
|
String content = demoInherit.getFromBox();
|
||||||
verify("get_from_box").withTimes(1);
|
verify("get_from_box").withTimes(1);
|
||||||
assertEquals("get_from_box", content);
|
assertEquals("get_from_box", content);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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();
|
String content = demoInherit.getFromBlackBox();
|
||||||
verify("get_from_blackbox").withTimes(1);
|
verify("get_from_blackbox").withTimes(1);
|
||||||
assertEquals("get_from_blackbox", content);
|
assertEquals("get_from_blackbox", content);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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();
|
String color = demoInherit.getColorViaColor();
|
||||||
verify("get_color_from_color").withTimes(1);
|
verify("get_color_from_color").withTimes(1);
|
||||||
assertEquals("color_from_color", color);
|
assertEquals("color_from_color", color);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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();
|
String color = demoInherit.getColorViaBox();
|
||||||
verify("get_color_from_blackbox").withTimes(1);
|
verify("get_color_from_blackbox").withTimes(1);
|
||||||
assertEquals("color_from_blackbox", color);
|
assertEquals("color_from_blackbox", color);
|
||||||
|
@ -9,6 +9,10 @@ import static com.alibaba.testable.core.matcher.InvokeMatcher.*;
|
|||||||
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示Mock方法调用校验器
|
||||||
|
* Demonstrate mock method invocation verifier
|
||||||
|
*/
|
||||||
class DemoMatcherTest {
|
class DemoMatcherTest {
|
||||||
|
|
||||||
private DemoMatcher demoMatcher = new DemoMatcher();
|
private DemoMatcher demoMatcher = new DemoMatcher();
|
||||||
|
@ -4,12 +4,17 @@ import com.alibaba.testable.core.annotation.TestableMock;
|
|||||||
import com.alibaba.testable.demo.model.BlackBox;
|
import com.alibaba.testable.demo.model.BlackBox;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
||||||
import static com.alibaba.testable.core.tool.TestableTool.*;
|
import static com.alibaba.testable.core.tool.TestableTool.*;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示基本的Mock功能
|
||||||
|
* Demonstrate basic mock functionality
|
||||||
|
*/
|
||||||
class DemoMockTest {
|
class DemoMockTest {
|
||||||
|
|
||||||
@TestableMock(targetMethod = CONSTRUCTOR)
|
@TestableMock(targetMethod = CONSTRUCTOR)
|
||||||
@ -56,7 +61,7 @@ class DemoMockTest {
|
|||||||
private DemoMock demoMock = new DemoMock();
|
private DemoMock demoMock = new DemoMock();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_new_object() throws Exception {
|
void should_able_to_mock_new_object() {
|
||||||
assertEquals("mock_something", demoMock.newFunc());
|
assertEquals("mock_something", demoMock.newFunc());
|
||||||
verify("createBlackBox").with("something");
|
verify("createBlackBox").with("something");
|
||||||
}
|
}
|
||||||
@ -68,7 +73,7 @@ class DemoMockTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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());
|
assertEquals("trim_string__sub_string__false", demoMock.commonFunc());
|
||||||
verify("trim").withTimes(1);
|
verify("trim").withTimes(1);
|
||||||
verify("sub").withTimes(1);
|
verify("sub").withTimes(1);
|
||||||
@ -76,7 +81,7 @@ class DemoMockTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_static_method() throws Exception {
|
void should_able_to_mock_static_method() {
|
||||||
assertEquals("not_secret_box", demoMock.getBox().get());
|
assertEquals("not_secret_box", demoMock.getBox().get());
|
||||||
verify("secretBox").withTimes(1);
|
verify("secretBox").withTimes(1);
|
||||||
}
|
}
|
||||||
|
@ -6,19 +6,23 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示私有成员访问功能
|
||||||
|
* Demonstrate private member access functionality
|
||||||
|
*/
|
||||||
@EnablePrivateAccess
|
@EnablePrivateAccess
|
||||||
class DemoPrivateAccessTest {
|
class DemoPrivateAccessTest {
|
||||||
|
|
||||||
private DemoPrivateAccess demoPrivateAccess = new DemoPrivateAccess();
|
private DemoPrivateAccess demoPrivateAccess = new DemoPrivateAccess();
|
||||||
|
|
||||||
@Test
|
@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", demoPrivateAccess.privateFunc("hello", 1));
|
||||||
assertEquals("hello - 1", PrivateAccessor.invoke(demoPrivateAccess, "privateFunc", "hello", 1));
|
assertEquals("hello - 1", PrivateAccessor.invoke(demoPrivateAccess, "privateFunc", "hello", 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_access_private_field() throws Exception {
|
void should_able_to_access_private_field() {
|
||||||
demoPrivateAccess.count = 2;
|
demoPrivateAccess.count = 2;
|
||||||
assertEquals(new Integer(2), demoPrivateAccess.count);
|
assertEquals(new Integer(2), demoPrivateAccess.count);
|
||||||
|
|
||||||
@ -27,13 +31,13 @@ class DemoPrivateAccessTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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", DemoPrivateAccess.privateStaticFunc("hello", 1));
|
||||||
assertEquals("hello + 1", PrivateAccessor.invokeStatic(DemoPrivateAccess.class, "privateStaticFunc", "hello", 1));
|
assertEquals("hello + 1", PrivateAccessor.invokeStatic(DemoPrivateAccess.class, "privateStaticFunc", "hello", 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_access_private_static_field() throws Exception {
|
void should_able_to_access_private_static_field() {
|
||||||
//DemoPrivateAccess.staticCount = 2;
|
//DemoPrivateAccess.staticCount = 2;
|
||||||
//assertEquals(new Integer(2), DemoPrivateAccess.staticCount);
|
//assertEquals(new Integer(2), DemoPrivateAccess.staticCount);
|
||||||
|
|
||||||
@ -42,7 +46,7 @@ class DemoPrivateAccessTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_update_final_field() throws Exception {
|
void should_able_to_update_final_field() {
|
||||||
demoPrivateAccess.pi = 4.13;
|
demoPrivateAccess.pi = 4.13;
|
||||||
assertEquals(4.13, demoPrivateAccess.pi);
|
assertEquals(4.13, demoPrivateAccess.pi);
|
||||||
}
|
}
|
||||||
|
@ -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 <T> List<T> getList(DemoTemplate self, T value) {
|
||||||
|
return new ArrayList<T>() {{ add((T)(value.toString() + "_mock_list")); }};
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestableMock
|
||||||
|
private <K, V> Map<K, V> getMap(DemoTemplate self, K key, V value) {
|
||||||
|
return new HashMap<K, V>() {{ put(key, (V)(value.toString() + "_mock_map")); }};
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestableMock(targetMethod = TestableTool.CONSTRUCTOR)
|
||||||
|
public HashSet newHashSet() {
|
||||||
|
HashSet<Object> set = new HashSet<>();
|
||||||
|
set.add("insert_mock");
|
||||||
|
return set;
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestableMock
|
||||||
|
private <E> 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user