use standalone mock class in demos

This commit is contained in:
金戟 2021-02-11 14:52:19 +08:00
parent 19f28c32db
commit 74e3cf16a4
9 changed files with 241 additions and 229 deletions

View File

@ -17,36 +17,37 @@ class DemoInheritTest {
private DemoInherit demoInherit = new DemoInherit(); private DemoInherit demoInherit = new DemoInherit();
@MockMethod(targetMethod = "put") public static class Mock {
private void put_into_box(Box self, String something) { @MockMethod(targetMethod = "put")
self.put("put_" + something + "_into_box"); private void put_into_box(Box self, String something) {
} self.put("put_" + something + "_into_box");
}
@MockMethod(targetMethod = "put") @MockMethod(targetMethod = "put")
private void put_into_blackbox(BlackBox self, String something) { private void put_into_blackbox(BlackBox self, String something) {
self.put("put_" + something + "_into_blackbox"); self.put("put_" + something + "_into_blackbox");
} }
@MockMethod(targetMethod = "get") @MockMethod(targetMethod = "get")
private String get_from_box(Box self) { private String get_from_box(Box self) {
return "get_from_box"; return "get_from_box";
} }
@MockMethod(targetMethod = "get") @MockMethod(targetMethod = "get")
private String get_from_blackbox(BlackBox self) { private String get_from_blackbox(BlackBox self) {
return "get_from_blackbox"; return "get_from_blackbox";
} }
@MockMethod(targetMethod = "getColor") @MockMethod(targetMethod = "getColor")
private String get_color_from_color(Color self) { private String get_color_from_color(Color self) {
return "color_from_color"; return "color_from_color";
} }
@MockMethod(targetMethod = "getColor") @MockMethod(targetMethod = "getColor")
private String get_color_from_blackbox(BlackBox self) { private String get_color_from_blackbox(BlackBox self) {
return "color_from_blackbox"; return "color_from_blackbox";
} }
}
@Test @Test
void should_able_to_mock_call_sub_object_method_by_parent_object() { void should_able_to_mock_call_sub_object_method_by_parent_object() {

View File

@ -17,15 +17,16 @@ class DemoMatcherTest {
private DemoMatcher demoMatcher = new DemoMatcher(); private DemoMatcher demoMatcher = new DemoMatcher();
@MockMethod(targetMethod = "methodToBeMocked") public static class Mock {
private void methodWithoutArgument(DemoMatcher self) {} @MockMethod(targetMethod = "methodToBeMocked")
private void methodWithoutArgument(DemoMatcher self) {}
@MockMethod(targetMethod = "methodToBeMocked") @MockMethod(targetMethod = "methodToBeMocked")
private void methodWithArguments(DemoMatcher self, Object a1, Object a2) {} private void methodWithArguments(DemoMatcher self, Object a1, Object a2) {}
@MockMethod(targetMethod = "methodToBeMocked")
private void methodWithArrayArgument(DemoMatcher self, Object[] a) {}
@MockMethod(targetMethod = "methodToBeMocked")
private void methodWithArrayArgument(DemoMatcher self, Object[] a) {}
}
@Test @Test
void should_match_no_argument() { void should_match_no_argument() {

View File

@ -20,53 +20,56 @@ class DemoMockTest {
private DemoMock demoMock = new DemoMock(); private DemoMock demoMock = new DemoMock();
@MockConstructor public static class Mock {
private BlackBox createBlackBox(String text) { @MockConstructor
return new BlackBox("mock_" + text); private BlackBox createBlackBox(String text) {
} return new BlackBox("mock_" + text);
@MockMethod(targetClass = DemoMock.class)
private String innerFunc(String text) {
return "mock_" + text;
}
@MockMethod(targetClass = DemoMock.class)
private String staticFunc() {
return "_MOCK_TAIL";
}
@MockMethod(targetClass = String.class)
private String trim() {
return "trim_string";
}
@MockMethod(targetClass = String.class, targetMethod = "substring")
private String sub(int i, int j) {
return "sub_string";
}
@MockMethod(targetClass = String.class)
private boolean startsWith(String s) {
return false;
}
@MockMethod(targetClass = BlackBox.class)
private BlackBox secretBox() {
return new BlackBox("not_secret_box");
}
@MockMethod(targetClass = DemoMock.class)
private String callFromDifferentMethod() {
if ("special_case".equals(MOCK_CONTEXT.get("case"))) {
return "mock_special";
} }
switch (SOURCE_METHOD) {
case "callerOne": return "mock_one"; @MockMethod(targetClass = DemoMock.class)
default: return "mock_others"; private String innerFunc(String text) {
return "mock_" + text;
}
@MockMethod(targetClass = DemoMock.class)
private String staticFunc() {
return "_MOCK_TAIL";
}
@MockMethod(targetClass = String.class)
private String trim() {
return "trim_string";
}
@MockMethod(targetClass = String.class, targetMethod = "substring")
private String sub(int i, int j) {
return "sub_string";
}
@MockMethod(targetClass = String.class)
private boolean startsWith(String s) {
return false;
}
@MockMethod(targetClass = BlackBox.class)
private BlackBox secretBox() {
return new BlackBox("not_secret_box");
}
@MockMethod(targetClass = DemoMock.class)
private String callFromDifferentMethod() {
if ("special_case".equals(MOCK_CONTEXT.get("case"))) {
return "mock_special";
}
switch (SOURCE_METHOD) {
case "callerOne":
return "mock_one";
default:
return "mock_others";
}
} }
} }
@Test @Test
void should_able_to_mock_new_object() { void should_able_to_mock_new_object() {
assertEquals("mock_something", demoMock.newFunc()); assertEquals("mock_something", demoMock.newFunc());

View File

@ -16,59 +16,60 @@ class DemoTemplateTest {
private DemoTemplate demoTemplate = new DemoTemplate(); private DemoTemplate demoTemplate = new DemoTemplate();
/* 第一种写法:使用泛型定义 */ public static class Mock {
/* First solution: use generics type */ /* 第一种写法:使用泛型定义 */
/* First solution: use generics type */
@MockMethod @MockMethod
private static <T> List<T> getList(DemoTemplate self, T value) { private <T> List<T> getList(DemoTemplate self, T value) {
return new ArrayList<T>() {{ add((T)(value.toString() + "_mock_list")); }}; return new ArrayList<T>() {{ add((T)(value.toString() + "_mock_list")); }};
}
@MockMethod
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")); }};
}
@MockConstructor
private <T> HashSet<T> newHashSet() {
HashSet<T> set = new HashSet<>();
set.add((T)"insert_mock");
return set;
}
@MockMethod
private <E> boolean add(Set s, E e) {
s.add(e.toString() + "_mocked");
return true;
}
/* 第二种写法使用Object类型 */
/* Second solution: use object type */
//@MockMethod
//private List<Object> getList(DemoTemplate self, Object value) {
// return new ArrayList<Object>() {{ add(value.toString() + "_mock_list"); }};
//}
//
//@MockMethod
//private Map<Object, Object> getMap(DemoTemplate self, Object key, Object value) {
// return new HashMap<Object, Object>() {{ put(key, value.toString() + "_mock_map"); }};
//}
//
//@MockConstructor
//private HashSet newHashSet() {
// HashSet<Object> set = new HashSet<>();
// set.add("insert_mock");
// return set;
//}
//
//@MockMethod
//private boolean add(Set s, Object e) {
// s.add(e.toString() + "_mocked");
// return true;
//}
} }
@MockMethod
private static <K, V> Map<K, V> getMap(DemoTemplate self, K key, V value) {
return new HashMap<K, V>() {{ put(key, (V)(value.toString() + "_mock_map")); }};
}
@MockConstructor
private <T> HashSet<T> newHashSet() {
HashSet<T> set = new HashSet<>();
set.add((T)"insert_mock");
return set;
}
@MockMethod
private <E> boolean add(Set s, E e) {
s.add(e.toString() + "_mocked");
return true;
}
/* 第二种写法使用Object类型 */
/* Second solution: use object type */
//@MockMethod
//private static List<Object> getList(DemoTemplate self, Object value) {
// return new ArrayList<Object>() {{ add(value.toString() + "_mock_list"); }};
//}
//
//@MockMethod
//private static Map<Object, Object> getMap(DemoTemplate self, Object key, Object value) {
// return new HashMap<Object, Object>() {{ put(key, value.toString() + "_mock_map"); }};
//}
//
//@MockConstructor
//private HashSet newHashSet() {
// HashSet<Object> set = new HashSet<>();
// set.add("insert_mock");
// return set;
//}
//
//@MockMethod
//private boolean add(Set s, Object e) {
// s.add(e.toString() + "_mocked");
// return true;
//}
@Test @Test
void should_able_to_mock_single_template_method() { void should_able_to_mock_single_template_method() {
String res = demoTemplate.singleTemplateMethod(); String res = demoTemplate.singleTemplateMethod();

View File

@ -16,37 +16,38 @@ internal class DemoInheritTest {
private val demoInherit = DemoInherit() private val demoInherit = DemoInherit()
@MockMethod(targetMethod = "put") class Mock {
private fun put_into_box(self: Box, something: String) { @MockMethod(targetMethod = "put")
self.put("put_" + something + "_into_box") private fun put_into_box(self: Box, something: String) {
} self.put("put_" + something + "_into_box")
}
@MockMethod(targetMethod = "put") @MockMethod(targetMethod = "put")
private fun put_into_blackbox(self: BlackBox, something: String) { private fun put_into_blackbox(self: BlackBox, something: String) {
self.put("put_" + something + "_into_blackbox") self.put("put_" + something + "_into_blackbox")
} }
@MockMethod(targetMethod = "get") @MockMethod(targetMethod = "get")
private fun get_from_box(self: Box): String { private fun get_from_box(self: Box): String {
return "get_from_box" return "get_from_box"
} }
@MockMethod(targetMethod = "get") @MockMethod(targetMethod = "get")
private fun get_from_blackbox(self: BlackBox): String { private fun get_from_blackbox(self: BlackBox): String {
return "get_from_blackbox" return "get_from_blackbox"
} }
@MockMethod(targetMethod = "getColor") @MockMethod(targetMethod = "getColor")
private fun get_color_from_color(self: Color): String { private fun get_color_from_color(self: Color): String {
return "color_from_color" return "color_from_color"
} }
@MockMethod(targetMethod = "getColor") @MockMethod(targetMethod = "getColor")
private fun get_color_from_blackbox(self: BlackBox): String { private fun get_color_from_blackbox(self: BlackBox): String {
return "color_from_blackbox" return "color_from_blackbox"
}
} }
@Test @Test
fun should_able_to_mock_call_sub_object_method_by_parent_object() { fun should_able_to_mock_call_sub_object_method_by_parent_object() {
val box = demoInherit.putIntoBox() as BlackBox val box = demoInherit.putIntoBox() as BlackBox

View File

@ -16,19 +16,20 @@ internal class DemoMatcherTest {
private val demoMatcher = DemoMatcher() private val demoMatcher = DemoMatcher()
@MockMethod(targetMethod = "methodToBeMocked") class Mock {
private fun methodWithoutArgument(self: DemoMatcher) { @MockMethod(targetMethod = "methodToBeMocked")
} private fun methodWithoutArgument(self: DemoMatcher) {
}
@MockMethod(targetMethod = "methodToBeMocked") @MockMethod(targetMethod = "methodToBeMocked")
private fun methodWithArguments(self: DemoMatcher, a1: Any, a2: Any) { private fun methodWithArguments(self: DemoMatcher, a1: Any, a2: Any) {
} }
@MockMethod(targetMethod = "methodToBeMocked") @MockMethod(targetMethod = "methodToBeMocked")
private fun methodWithArrayArgument(self: DemoMatcher, a: Array<Any>) { private fun methodWithArrayArgument(self: DemoMatcher, a: Array<Any>) {
}
} }
@Test @Test
fun should_match_no_argument() { fun should_match_no_argument() {
demoMatcher.callMethodWithoutArgument() demoMatcher.callMethodWithoutArgument()

View File

@ -19,49 +19,50 @@ internal class DemoMockTest {
private val demoMock = DemoMock() private val demoMock = DemoMock()
@MockConstructor class Mock {
private fun createBlackBox(text: String) = BlackBox("mock_$text") @MockConstructor
private fun createBlackBox(text: String) = BlackBox("mock_$text")
@MockMethod(targetClass = DemoMock::class) @MockMethod(targetClass = DemoMock::class)
private fun innerFunc(text: String) = "mock_$text" private fun innerFunc(text: String) = "mock_$text"
@MockMethod(targetClass = DemoMock::class) @MockMethod(targetClass = DemoMock::class)
private fun staticFunc(): String { private fun staticFunc(): String {
return "_MOCK_TAIL"; return "_MOCK_TAIL";
} }
@MockMethod(targetClass = BlackBox::class) @MockMethod(targetClass = BlackBox::class)
private fun trim() = "trim_string" private fun trim() = "trim_string"
@MockMethod(targetClass = BlackBox::class, targetMethod = "substring") @MockMethod(targetClass = BlackBox::class, targetMethod = "substring")
private fun sub(i: Int, j: Int) = "sub_string" private fun sub(i: Int, j: Int) = "sub_string"
@MockMethod(targetClass = BlackBox::class) @MockMethod(targetClass = BlackBox::class)
private fun startsWith(s: String) = false private fun startsWith(s: String) = false
@MockMethod(targetClass = BlackBox::class) @MockMethod(targetClass = BlackBox::class)
private fun secretBox(): BlackBox { private fun secretBox(): BlackBox {
return BlackBox("not_secret_box") return BlackBox("not_secret_box")
} }
@MockMethod(targetClass = ColorBox::class) @MockMethod(targetClass = ColorBox::class)
private fun createBox(color: String, box: BlackBox): BlackBox { private fun createBox(color: String, box: BlackBox): BlackBox {
return BlackBox("White_${box.get()}") return BlackBox("White_${box.get()}")
} }
@MockMethod(targetClass = DemoMock::class) @MockMethod(targetClass = DemoMock::class)
private fun callFromDifferentMethod(): String { private fun callFromDifferentMethod(): String {
return if (MOCK_CONTEXT["case"] == "special_case") { return if (MOCK_CONTEXT["case"] == "special_case") {
"mock_special" "mock_special"
} else { } else {
when (SOURCE_METHOD) { when (SOURCE_METHOD) {
"callerOne" -> "mock_one" "callerOne" -> "mock_one"
else -> "mock_others" else -> "mock_others"
}
} }
} }
} }
@Test @Test
fun should_able_to_mock_new_object() { fun should_able_to_mock_new_object() {
assertEquals("mock_something", demoMock.newFunc()) assertEquals("mock_something", demoMock.newFunc())

View File

@ -14,30 +14,31 @@ internal class DemoTemplateTest {
private val demoTemplate = DemoTemplate() private val demoTemplate = DemoTemplate()
@MockMethod class Mock {
private fun <T> getList(self: DemoTemplate, value: T): List<T> { @MockMethod
return mutableListOf((value.toString() + "_mock_list") as T) private fun <T> getList(self: DemoTemplate, value: T): List<T> {
} return mutableListOf((value.toString() + "_mock_list") as T)
}
@MockMethod @MockMethod
private fun <K, V> getMap(self: DemoTemplate, key: K, value: V): Map<K, V> { private fun <K, V> getMap(self: DemoTemplate, key: K, value: V): Map<K, V> {
return mutableMapOf(key to (value.toString() + "_mock_map") as V) return mutableMapOf(key to (value.toString() + "_mock_map") as V)
} }
@MockConstructor @MockConstructor
private fun newHashSet(): HashSet<*> { private fun newHashSet(): HashSet<*> {
val set = HashSet<Any>() val set = HashSet<Any>()
set.add("insert_mock") set.add("insert_mock")
return set return set
} }
@MockMethod @MockMethod
private fun <E> add(s: MutableSet<E>, e: E): Boolean { private fun <E> add(s: MutableSet<E>, e: E): Boolean {
s.add((e.toString() + "_mocked") as E) s.add((e.toString() + "_mocked") as E)
return true return true
}
} }
@Test @Test
fun should_able_to_mock_single_template_method() { fun should_able_to_mock_single_template_method() {
val res = demoTemplate.singleTemplateMethod() val res = demoTemplate.singleTemplateMethod()

View File

@ -7,34 +7,36 @@ import java.io.File
class PathUtilTest { class PathUtilTest {
@MockMethod class Mock {
fun exists(f: File): Boolean { @MockMethod
return when (f.absolutePath) { fun exists(f: File): Boolean {
"/a/b" -> true return when (f.absolutePath) {
"/a/b/c" -> true "/a/b" -> true
else -> f.exists() "/a/b/c" -> true
else -> f.exists()
}
} }
}
@MockMethod @MockMethod
fun isDirectory(f: File): Boolean { fun isDirectory(f: File): Boolean {
return when (f.absolutePath) { return when (f.absolutePath) {
"/a/b/c" -> true "/a/b/c" -> true
else -> f.isDirectory else -> f.isDirectory
}
} }
}
@MockMethod @MockMethod
fun delete(f: File): Boolean { fun delete(f: File): Boolean {
return true return true
} }
@MockMethod @MockMethod
fun listFiles(f: File): Array<File>? { fun listFiles(f: File): Array<File>? {
return when (f.absolutePath) { return when (f.absolutePath) {
"/a/b" -> arrayOf(File("/a/b/c"), File("/a/b/d")) "/a/b" -> arrayOf(File("/a/b/c"), File("/a/b/d"))
"/a/b/c" -> arrayOf(File("/a/b/c/e")) "/a/b/c" -> arrayOf(File("/a/b/c/e"))
else -> f.listFiles() else -> f.listFiles()
}
} }
} }