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,6 +17,7 @@ class DemoInheritTest {
private DemoInherit demoInherit = new DemoInherit();
public static class Mock {
@MockMethod(targetMethod = "put")
private void put_into_box(Box self, String something) {
self.put("put_" + something + "_into_box");
@ -46,7 +47,7 @@ class DemoInheritTest {
private String get_color_from_blackbox(BlackBox self) {
return "color_from_blackbox";
}
}
@Test
void should_able_to_mock_call_sub_object_method_by_parent_object() {

View File

@ -17,6 +17,7 @@ class DemoMatcherTest {
private DemoMatcher demoMatcher = new DemoMatcher();
public static class Mock {
@MockMethod(targetMethod = "methodToBeMocked")
private void methodWithoutArgument(DemoMatcher self) {}
@ -25,7 +26,7 @@ class DemoMatcherTest {
@MockMethod(targetMethod = "methodToBeMocked")
private void methodWithArrayArgument(DemoMatcher self, Object[] a) {}
}
@Test
void should_match_no_argument() {

View File

@ -20,6 +20,7 @@ class DemoMockTest {
private DemoMock demoMock = new DemoMock();
public static class Mock {
@MockConstructor
private BlackBox createBlackBox(String text) {
return new BlackBox("mock_" + text);
@ -61,11 +62,13 @@ class DemoMockTest {
return "mock_special";
}
switch (SOURCE_METHOD) {
case "callerOne": return "mock_one";
default: return "mock_others";
case "callerOne":
return "mock_one";
default:
return "mock_others";
}
}
}
@Test
void should_able_to_mock_new_object() {

View File

@ -16,16 +16,17 @@ class DemoTemplateTest {
private DemoTemplate demoTemplate = new DemoTemplate();
public static class Mock {
/* 第一种写法:使用泛型定义 */
/* First solution: use generics type */
@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")); }};
}
@MockMethod
private static <K, V> Map<K, V> getMap(DemoTemplate self, K key, V value) {
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")); }};
}
@ -46,12 +47,12 @@ class DemoTemplateTest {
/* Second solution: use object type */
//@MockMethod
//private static List<Object> getList(DemoTemplate self, Object value) {
//private 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) {
//private Map<Object, Object> getMap(DemoTemplate self, Object key, Object value) {
// return new HashMap<Object, Object>() {{ put(key, value.toString() + "_mock_map"); }};
//}
//
@ -67,7 +68,7 @@ class DemoTemplateTest {
// s.add(e.toString() + "_mocked");
// return true;
//}
}
@Test
void should_able_to_mock_single_template_method() {

View File

@ -16,6 +16,7 @@ internal class DemoInheritTest {
private val demoInherit = DemoInherit()
class Mock {
@MockMethod(targetMethod = "put")
private fun put_into_box(self: Box, something: String) {
self.put("put_" + something + "_into_box")
@ -45,7 +46,7 @@ internal class DemoInheritTest {
private fun get_color_from_blackbox(self: BlackBox): String {
return "color_from_blackbox"
}
}
@Test
fun should_able_to_mock_call_sub_object_method_by_parent_object() {

View File

@ -16,6 +16,7 @@ internal class DemoMatcherTest {
private val demoMatcher = DemoMatcher()
class Mock {
@MockMethod(targetMethod = "methodToBeMocked")
private fun methodWithoutArgument(self: DemoMatcher) {
}
@ -27,7 +28,7 @@ internal class DemoMatcherTest {
@MockMethod(targetMethod = "methodToBeMocked")
private fun methodWithArrayArgument(self: DemoMatcher, a: Array<Any>) {
}
}
@Test
fun should_match_no_argument() {

View File

@ -19,6 +19,7 @@ internal class DemoMockTest {
private val demoMock = DemoMock()
class Mock {
@MockConstructor
private fun createBlackBox(text: String) = BlackBox("mock_$text")
@ -60,7 +61,7 @@ internal class DemoMockTest {
}
}
}
}
@Test
fun should_able_to_mock_new_object() {

View File

@ -14,6 +14,7 @@ internal class DemoTemplateTest {
private val demoTemplate = DemoTemplate()
class Mock {
@MockMethod
private fun <T> getList(self: DemoTemplate, value: T): List<T> {
return mutableListOf((value.toString() + "_mock_list") as T)
@ -36,7 +37,7 @@ internal class DemoTemplateTest {
s.add((e.toString() + "_mocked") as E)
return true
}
}
@Test
fun should_able_to_mock_single_template_method() {

View File

@ -7,6 +7,7 @@ import java.io.File
class PathUtilTest {
class Mock {
@MockMethod
fun exists(f: File): Boolean {
return when (f.absolutePath) {
@ -37,6 +38,7 @@ class PathUtilTest {
else -> f.listFiles()
}
}
}
@Test
fun should_able_to_mock_java_method_invoke_in_kotlin() {