mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-27 12:51:00 +08:00
use MockInvoke and MockNew
This commit is contained in:
parent
28500558fc
commit
c2d456da97
@ -2,7 +2,7 @@
|
||||
|
||||
换种思路写Mock,让单元测试更简单。
|
||||
|
||||
无需初始化,不挑服务框架,甭管要换的是私有方法、静态方法、构造方法还是其他任何类的任何方法,也甭管要换的对象是怎么创建的。写好Mock定义,加个`@MockMethod`注解,一切统统搞定。
|
||||
无需初始化,不挑服务框架,甭管要换的是私有方法、静态方法、构造方法还是其他任何类的任何方法,也甭管要换的对象是怎么创建的。写好Mock定义,加个`@MockInvoke`注解,一切统统搞定。
|
||||
|
||||
- 文档:https://alibaba.github.io/testable-mock/
|
||||
- 国内文档镜像:http://freyrlin.gitee.io/testable-mock/
|
||||
|
@ -3,7 +3,7 @@
|
||||
Write mock faster, make unit testing easier.
|
||||
|
||||
Any test framework, no initialization, no matter private method, static method, construction method, or any other method of any class, and no matter how the object created.
|
||||
Write a mock method, add an `@MockMethod` annotation, everything is done.
|
||||
Write a mock method, add an `@MockInvoke` annotation, everything is done.
|
||||
|
||||
Usage Document: https://alibaba.github.io/testable-mock/#/en-us/
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.alibaba.testable.demo;
|
||||
|
||||
import com.alibaba.testable.demo.model.BlackBox;
|
||||
import com.alibaba.testable.core.annotation.MockConstructor;
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.testable.core.annotation.MockNew;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
@ -23,42 +23,42 @@ public class DemoBasicTest {
|
||||
private DemoBasic demoBasic = new DemoBasic();
|
||||
|
||||
public static class Mock {
|
||||
@MockConstructor
|
||||
@MockNew
|
||||
private BlackBox createBlackBox(String text) {
|
||||
return new BlackBox("mock_" + text);
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = DemoBasic.class)
|
||||
@MockInvoke(targetClass = DemoBasic.class)
|
||||
private String innerFunc(String text) {
|
||||
return "mock_" + text;
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = DemoBasic.class)
|
||||
@MockInvoke(targetClass = DemoBasic.class)
|
||||
private String staticFunc() {
|
||||
return "_MOCK_TAIL";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = String.class)
|
||||
@MockInvoke(targetClass = String.class)
|
||||
private String trim() {
|
||||
return "trim_string";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = String.class, targetMethod = "substring")
|
||||
@MockInvoke(targetClass = String.class, targetMethod = "substring")
|
||||
private String sub(int i, int j) {
|
||||
return "sub_string";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = String.class)
|
||||
@MockInvoke(targetClass = String.class)
|
||||
private boolean startsWith(String s) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = BlackBox.class)
|
||||
@MockInvoke(targetClass = BlackBox.class)
|
||||
private BlackBox secretBox() {
|
||||
return new BlackBox("not_secret_box");
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = DemoBasic.class)
|
||||
@MockInvoke(targetClass = DemoBasic.class)
|
||||
private String callFromDifferentMethod() {
|
||||
if ("special_case".equals(MOCK_CONTEXT.get("case"))) {
|
||||
return "mock_special";
|
||||
|
@ -3,7 +3,7 @@ package com.alibaba.testable.demo;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@ -21,7 +21,7 @@ public class DemoServiceTest {
|
||||
private DemoService demoService;
|
||||
|
||||
public static class Mock {
|
||||
@MockMethod(targetClass = Log.class, targetMethod = "d")
|
||||
@MockInvoke(targetClass = Log.class, targetMethod = "d")
|
||||
public static int log(String tag, String msg) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,26 +1,26 @@
|
||||
package com.alibaba.demo.association;
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
import com.alibaba.testable.core.model.MockScope;
|
||||
|
||||
class CookerServiceMock {
|
||||
|
||||
@MockMethod(targetClass = CookerService.class)
|
||||
@MockInvoke(targetClass = CookerService.class)
|
||||
public static String hireSandwichCooker() {
|
||||
return "Fake-Sandwich-Cooker";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = CookerService.class, scope = MockScope.ASSOCIATED)
|
||||
@MockInvoke(targetClass = CookerService.class, scope = MockScope.ASSOCIATED)
|
||||
public static String hireHamburgerCooker() {
|
||||
return "Fake-Hamburger-Cooker";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = CookerService.class)
|
||||
@MockInvoke(targetClass = CookerService.class)
|
||||
private String cookSandwich() {
|
||||
return "Faked-Sandwich";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = CookerService.class, scope = MockScope.ASSOCIATED)
|
||||
@MockInvoke(targetClass = CookerService.class, scope = MockScope.ASSOCIATED)
|
||||
private String cookHamburger() {
|
||||
return "Faked-Hamburger";
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package com.alibaba.demo.basic;
|
||||
import com.alibaba.demo.basic.model.mock.BlackBox;
|
||||
import com.alibaba.demo.basic.model.mock.Box;
|
||||
import com.alibaba.demo.basic.model.mock.Color;
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
|
||||
@ -18,37 +18,37 @@ class DemoInheritTest {
|
||||
private DemoInherit demoInherit = new DemoInherit();
|
||||
|
||||
public static class Mock {
|
||||
@MockMethod(targetMethod = "put")
|
||||
@MockInvoke(targetMethod = "put")
|
||||
private void put_into_box(Box self, String something) {
|
||||
self.put("put_" + something + "_into_box");
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "put")
|
||||
@MockInvoke(targetMethod = "put")
|
||||
private void put_into_blackbox(BlackBox self, String something) {
|
||||
self.put("put_" + something + "_into_blackbox");
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "get")
|
||||
@MockInvoke(targetMethod = "get")
|
||||
private String get_from_box(Box self) {
|
||||
return "get_from_box";
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "get")
|
||||
@MockInvoke(targetMethod = "get")
|
||||
private String get_from_blackbox(BlackBox self) {
|
||||
return "get_from_blackbox";
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "getColor")
|
||||
@MockInvoke(targetMethod = "getColor")
|
||||
private String get_color_from_color(Color self) {
|
||||
return "color_from_color";
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "getColor")
|
||||
@MockInvoke(targetMethod = "getColor")
|
||||
private String get_color_from_blackbox(BlackBox self) {
|
||||
return "color_from_blackbox";
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "getColorIndex")
|
||||
@MockInvoke(targetMethod = "getColorIndex")
|
||||
private String get_colorIdx_from_color(Color self) {
|
||||
return "colorIdx_from_color";
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.demo.basic;
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@ -12,7 +12,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
class DemoInnerClassTest {
|
||||
|
||||
public static class Mock {
|
||||
@MockMethod(targetClass = DemoInnerClass.class)
|
||||
@MockInvoke(targetClass = DemoInnerClass.class)
|
||||
String methodToBeMock() {
|
||||
return "MockedCall";
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.alibaba.demo.basic;
|
||||
|
||||
import com.alibaba.demo.basic.model.mock.BlackBox;
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
import com.alibaba.testable.core.error.VerifyFailedError;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -18,13 +18,13 @@ class DemoMatcherTest {
|
||||
private DemoMatcher demoMatcher = new DemoMatcher();
|
||||
|
||||
public static class Mock {
|
||||
@MockMethod(targetMethod = "methodToBeMocked")
|
||||
@MockInvoke(targetMethod = "methodToBeMocked")
|
||||
private void methodWithoutArgument(DemoMatcher self) {}
|
||||
|
||||
@MockMethod(targetMethod = "methodToBeMocked")
|
||||
@MockInvoke(targetMethod = "methodToBeMocked")
|
||||
private void methodWithArguments(DemoMatcher self, Object a1, Object a2) {}
|
||||
|
||||
@MockMethod(targetMethod = "methodToBeMocked")
|
||||
@MockInvoke(targetMethod = "methodToBeMocked")
|
||||
private void methodWithArrayArgument(DemoMatcher self, Object[] a) {}
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.alibaba.demo.basic;
|
||||
|
||||
import com.alibaba.demo.basic.model.mock.BlackBox;
|
||||
import com.alibaba.testable.core.annotation.MockConstructor;
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.testable.core.annotation.MockNew;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
@ -21,42 +21,42 @@ class DemoMockTest {
|
||||
private DemoMock demoMock = new DemoMock();
|
||||
|
||||
public static class Mock {
|
||||
@MockConstructor
|
||||
@MockNew
|
||||
private BlackBox createBlackBox(String text) {
|
||||
return new BlackBox("mock_" + text);
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = DemoMock.class)
|
||||
@MockInvoke(targetClass = DemoMock.class)
|
||||
private String innerFunc(String text) {
|
||||
return "mock_" + text;
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = DemoMock.class)
|
||||
@MockInvoke(targetClass = DemoMock.class)
|
||||
private String staticFunc() {
|
||||
return "_MOCK_TAIL";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = String.class)
|
||||
@MockInvoke(targetClass = String.class)
|
||||
private String trim() {
|
||||
return "trim_string";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = String.class, targetMethod = "substring")
|
||||
@MockInvoke(targetClass = String.class, targetMethod = "substring")
|
||||
private String sub(int i, int j) {
|
||||
return "sub_string";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = String.class)
|
||||
@MockInvoke(targetClass = String.class)
|
||||
private boolean startsWith(String s) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = BlackBox.class)
|
||||
@MockInvoke(targetClass = BlackBox.class)
|
||||
private BlackBox secretBox() {
|
||||
return new BlackBox("not_secret_box");
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = DemoMock.class)
|
||||
@MockInvoke(targetClass = DemoMock.class)
|
||||
private String callFromDifferentMethod() {
|
||||
if ("special_case".equals(MOCK_CONTEXT.get("case"))) {
|
||||
return "mock_special";
|
||||
|
@ -1,8 +1,7 @@
|
||||
package com.alibaba.demo.basic;
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockConstructor;
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.demo.basic.DemoTemplate;
|
||||
import com.alibaba.testable.core.annotation.MockNew;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
@ -21,24 +20,24 @@ class DemoTemplateTest {
|
||||
/* 第一种写法:使用泛型定义 */
|
||||
/* First solution: use generics type */
|
||||
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
private <T> List<T> getList(DemoTemplate self, T value) {
|
||||
return new ArrayList<T>() {{ add((T)(value.toString() + "_mock_list")); }};
|
||||
}
|
||||
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
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
|
||||
@MockNew
|
||||
private <T> HashSet<T> newHashSet() {
|
||||
HashSet<T> set = new HashSet<>();
|
||||
set.add((T)"insert_mock");
|
||||
return set;
|
||||
}
|
||||
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
private <E> boolean add(Set s, E e) {
|
||||
s.add(e.toString() + "_mocked");
|
||||
return true;
|
||||
@ -47,24 +46,24 @@ class DemoTemplateTest {
|
||||
/* 第二种写法:使用Object类型 */
|
||||
/* Second solution: use object type */
|
||||
|
||||
//@MockMethod
|
||||
//@MockInvoke
|
||||
//private List<Object> getList(DemoTemplate self, Object value) {
|
||||
// return new ArrayList<Object>() {{ add(value.toString() + "_mock_list"); }};
|
||||
//}
|
||||
//
|
||||
//@MockMethod
|
||||
//@MockInvoke
|
||||
//private Map<Object, Object> getMap(DemoTemplate self, Object key, Object value) {
|
||||
// return new HashMap<Object, Object>() {{ put(key, value.toString() + "_mock_map"); }};
|
||||
//}
|
||||
//
|
||||
//@MockConstructor
|
||||
//@MockNew
|
||||
//private HashSet newHashSet() {
|
||||
// HashSet<Object> set = new HashSet<>();
|
||||
// set.add("insert_mock");
|
||||
// return set;
|
||||
//}
|
||||
//
|
||||
//@MockMethod
|
||||
//@MockInvoke
|
||||
//private boolean add(Set s, Object e) {
|
||||
// s.add(e.toString() + "_mocked");
|
||||
// return true;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.demo.lambda;
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
|
||||
@ -14,27 +14,27 @@ public class ExternalLambdaDemoTest {
|
||||
@SuppressWarnings("unused")
|
||||
public static class Mock {
|
||||
|
||||
@MockMethod(targetClass = String.class, targetMethod = "contains")
|
||||
@MockInvoke(targetClass = String.class, targetMethod = "contains")
|
||||
public boolean mockContains(CharSequence s) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = Byte.class, targetMethod = "floatValue")
|
||||
@MockInvoke(targetClass = Byte.class, targetMethod = "floatValue")
|
||||
public float mockFloatValue() {
|
||||
return 0.1f;
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = Double.class, targetMethod = "compareTo")
|
||||
@MockInvoke(targetClass = Double.class, targetMethod = "compareTo")
|
||||
public int mockCompareTo(Double anotherDouble) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = LambdaDemo.class, targetMethod = "methodReference0")
|
||||
@MockInvoke(targetClass = LambdaDemo.class, targetMethod = "methodReference0")
|
||||
public String mockMethodReference0() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = ExternalLambdaDemo.class, targetMethod = "f3")
|
||||
@MockInvoke(targetClass = ExternalLambdaDemo.class, targetMethod = "f3")
|
||||
public Boolean mockF3(String s1, Long l) {
|
||||
return true;
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.demo.lambda;
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
|
||||
@ -14,32 +14,32 @@ public class LambdaDemoTest {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static class Mock {
|
||||
@MockMethod(targetClass = LambdaDemo.class, targetMethod = "run")
|
||||
@MockInvoke(targetClass = LambdaDemo.class, targetMethod = "run")
|
||||
private void mockRun() {
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = LambdaDemo.class)
|
||||
@MockInvoke(targetClass = LambdaDemo.class)
|
||||
private String function0() {
|
||||
return "mock_function0";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = LambdaDemo.class)
|
||||
@MockInvoke(targetClass = LambdaDemo.class)
|
||||
private String function1(Integer i) {
|
||||
return "mock_function1";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = LambdaDemo.class)
|
||||
@MockInvoke(targetClass = LambdaDemo.class)
|
||||
private String function2(Integer i, Double d) {
|
||||
return "mock_function2";
|
||||
}
|
||||
|
||||
@SuppressWarnings("RedundantThrows")
|
||||
@MockMethod(targetClass = LambdaDemo.class)
|
||||
@MockInvoke(targetClass = LambdaDemo.class)
|
||||
private String function1Throwable(Integer i) throws Throwable{
|
||||
return "mock_function1Throwable";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = StaticMethod.class, targetMethod = "function1")
|
||||
@MockInvoke(targetClass = StaticMethod.class, targetMethod = "function1")
|
||||
public static String staticFunction1(Integer i) {
|
||||
return "mock_staticFunction1";
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.alibaba.demo.one2multi;
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
|
||||
public class ASvcMock {
|
||||
|
||||
@MockMethod(targetClass = String.class, targetMethod = "format")
|
||||
@MockInvoke(targetClass = String.class, targetMethod = "format")
|
||||
public String a_format(String format, Object... args) {
|
||||
return "a_mock";
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.alibaba.demo.one2multi;
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
|
||||
public class BSvcMock {
|
||||
|
||||
@MockMethod(targetClass = String.class, targetMethod = "format")
|
||||
@MockInvoke(targetClass = String.class, targetMethod = "format")
|
||||
public String b_format(String format, Object... args) {
|
||||
return "b_mock";
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.alibaba.demo.one2multi;
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod;
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
|
||||
public class CSvcMock {
|
||||
|
||||
@MockMethod(targetClass = String.class, targetMethod = "format")
|
||||
@MockInvoke(targetClass = String.class, targetMethod = "format")
|
||||
public String c_format(String format, Object... args) {
|
||||
return "c_mock";
|
||||
}
|
||||
|
@ -1,26 +1,26 @@
|
||||
package com.alibaba.demo.association
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.annotation.MockInvoke
|
||||
import com.alibaba.testable.core.model.MockScope
|
||||
|
||||
internal class CookerServiceMock {
|
||||
|
||||
@MockMethod(targetClass = CookerService::class)
|
||||
@MockInvoke(targetClass = CookerService::class)
|
||||
private fun cookSandwich(): String {
|
||||
return "Faked-Sandwich"
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = CookerService::class, scope = MockScope.ASSOCIATED)
|
||||
@MockInvoke(targetClass = CookerService::class, scope = MockScope.ASSOCIATED)
|
||||
private fun cookHamburger(): String {
|
||||
return "Faked-Hamburger"
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = CookerService::class)
|
||||
@MockInvoke(targetClass = CookerService::class)
|
||||
fun hireSandwichCooker(): String {
|
||||
return "Fake-Sandwich-Cooker"
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = CookerService::class, scope = MockScope.ASSOCIATED)
|
||||
@MockInvoke(targetClass = CookerService::class, scope = MockScope.ASSOCIATED)
|
||||
fun hireHamburgerCooker(): String {
|
||||
return "Fake-Hamburger-Cooker"
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.demo.basic
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.annotation.MockInvoke
|
||||
import com.alibaba.testable.core.matcher.InvocationVerifier
|
||||
import com.alibaba.demo.basic.model.mock.BlackBox
|
||||
import com.alibaba.demo.basic.model.mock.Box
|
||||
@ -17,32 +17,32 @@ internal class DemoInheritTest {
|
||||
private val demoInherit = DemoInherit()
|
||||
|
||||
class Mock {
|
||||
@MockMethod(targetMethod = "put")
|
||||
@MockInvoke(targetMethod = "put")
|
||||
private fun put_into_box(self: Box, something: String) {
|
||||
self.put("put_" + something + "_into_box")
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "put")
|
||||
@MockInvoke(targetMethod = "put")
|
||||
private fun put_into_blackbox(self: BlackBox, something: String) {
|
||||
self.put("put_" + something + "_into_blackbox")
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "get")
|
||||
@MockInvoke(targetMethod = "get")
|
||||
private fun get_from_box(self: Box): String {
|
||||
return "get_from_box"
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "get")
|
||||
@MockInvoke(targetMethod = "get")
|
||||
private fun get_from_blackbox(self: BlackBox): String {
|
||||
return "get_from_blackbox"
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "getColor")
|
||||
@MockInvoke(targetMethod = "getColor")
|
||||
private fun get_color_from_color(self: Color): String {
|
||||
return "color_from_color"
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "getColor")
|
||||
@MockInvoke(targetMethod = "getColor")
|
||||
private fun get_color_from_blackbox(self: BlackBox): String {
|
||||
return "color_from_blackbox"
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.demo.basic
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.annotation.MockInvoke
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
@ -11,7 +11,7 @@ import org.junit.jupiter.api.Test
|
||||
internal class DemoInnerClassTest {
|
||||
|
||||
class Mock {
|
||||
@MockMethod(targetClass = DemoInnerClass::class)
|
||||
@MockInvoke(targetClass = DemoInnerClass::class)
|
||||
fun methodToBeMock(): String {
|
||||
return "MockedCall"
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.demo.basic
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.annotation.MockInvoke
|
||||
import com.alibaba.testable.core.error.VerifyFailedError
|
||||
import com.alibaba.testable.core.matcher.InvocationMatcher
|
||||
import com.alibaba.testable.core.matcher.InvocationVerifier
|
||||
@ -17,15 +17,15 @@ internal class DemoMatcherTest {
|
||||
private val demoMatcher = DemoMatcher()
|
||||
|
||||
class Mock {
|
||||
@MockMethod(targetMethod = "methodToBeMocked")
|
||||
@MockInvoke(targetMethod = "methodToBeMocked")
|
||||
private fun methodWithoutArgument(self: DemoMatcher) {
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "methodToBeMocked")
|
||||
@MockInvoke(targetMethod = "methodToBeMocked")
|
||||
private fun methodWithArguments(self: DemoMatcher, a1: Any, a2: Any) {
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "methodToBeMocked")
|
||||
@MockInvoke(targetMethod = "methodToBeMocked")
|
||||
private fun methodWithArrayArgument(self: DemoMatcher, a: Array<Any>) {
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.alibaba.demo.basic
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockConstructor
|
||||
import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.annotation.MockNew
|
||||
import com.alibaba.testable.core.annotation.MockInvoke
|
||||
import com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked
|
||||
import com.alibaba.testable.core.tool.TestableTool.SOURCE_METHOD
|
||||
import com.alibaba.testable.core.tool.TestableTool.MOCK_CONTEXT
|
||||
@ -20,37 +20,37 @@ internal class DemoMockTest {
|
||||
private val demoMock = DemoMock()
|
||||
|
||||
class Mock {
|
||||
@MockConstructor
|
||||
@MockNew
|
||||
private fun createBlackBox(text: String) = BlackBox("mock_$text")
|
||||
|
||||
@MockMethod(targetClass = DemoMock::class)
|
||||
@MockInvoke(targetClass = DemoMock::class)
|
||||
private fun innerFunc(text: String) = "mock_$text"
|
||||
|
||||
@MockMethod(targetClass = DemoMock::class)
|
||||
@MockInvoke(targetClass = DemoMock::class)
|
||||
private fun staticFunc(): String {
|
||||
return "_MOCK_TAIL";
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = BlackBox::class)
|
||||
@MockInvoke(targetClass = BlackBox::class)
|
||||
private fun trim() = "trim_string"
|
||||
|
||||
@MockMethod(targetClass = BlackBox::class, targetMethod = "substring")
|
||||
@MockInvoke(targetClass = BlackBox::class, targetMethod = "substring")
|
||||
private fun sub(i: Int, j: Int) = "sub_string"
|
||||
|
||||
@MockMethod(targetClass = BlackBox::class)
|
||||
@MockInvoke(targetClass = BlackBox::class)
|
||||
private fun startsWith(s: String) = false
|
||||
|
||||
@MockMethod(targetClass = BlackBox::class)
|
||||
@MockInvoke(targetClass = BlackBox::class)
|
||||
private fun secretBox(): BlackBox {
|
||||
return BlackBox("not_secret_box")
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = ColorBox::class)
|
||||
@MockInvoke(targetClass = ColorBox::class)
|
||||
private fun createBox(color: String, box: BlackBox): BlackBox {
|
||||
return BlackBox("White_${box.get()}")
|
||||
}
|
||||
|
||||
@MockMethod(targetClass = DemoMock::class)
|
||||
@MockInvoke(targetClass = DemoMock::class)
|
||||
private fun callFromDifferentMethod(): String {
|
||||
return if (MOCK_CONTEXT["case"] == "special_case") {
|
||||
"mock_special"
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.alibaba.demo.basic
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockConstructor
|
||||
import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.annotation.MockNew
|
||||
import com.alibaba.testable.core.annotation.MockInvoke
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
@ -14,24 +14,24 @@ internal class DemoTemplateTest {
|
||||
private val demoTemplate = DemoTemplate()
|
||||
|
||||
class Mock {
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
private fun <T> getList(self: DemoTemplate, value: T): List<T> {
|
||||
return mutableListOf((value.toString() + "_mock_list") as T)
|
||||
}
|
||||
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
private fun <K, V> getMap(self: DemoTemplate, key: K, value: V): Map<K, V> {
|
||||
return mutableMapOf(key to (value.toString() + "_mock_map") as V)
|
||||
}
|
||||
|
||||
@MockConstructor
|
||||
@MockNew
|
||||
private fun newHashSet(): HashSet<*> {
|
||||
val set = HashSet<Any>()
|
||||
set.add("insert_mock")
|
||||
return set
|
||||
}
|
||||
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
private fun <E> add(s: MutableSet<E>, e: E): Boolean {
|
||||
s.add((e.toString() + "_mocked") as E)
|
||||
return true
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alibaba.demo.java2kotlin
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.annotation.MockInvoke
|
||||
import com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.File
|
||||
@ -8,7 +8,7 @@ import java.io.File
|
||||
class PathDemoTest {
|
||||
|
||||
class Mock {
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
fun exists(f: File): Boolean {
|
||||
return when (f.absolutePath) {
|
||||
"/a/b" -> true
|
||||
@ -17,7 +17,7 @@ class PathDemoTest {
|
||||
}
|
||||
}
|
||||
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
fun isDirectory(f: File): Boolean {
|
||||
return when (f.absolutePath) {
|
||||
"/a/b/c" -> true
|
||||
@ -25,12 +25,12 @@ class PathDemoTest {
|
||||
}
|
||||
}
|
||||
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
fun delete(f: File): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
fun listFiles(f: File): Array<File>? {
|
||||
return when (f.absolutePath) {
|
||||
"/a/b" -> arrayOf(File("/a/b/c"), File("/a/b/d"))
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.alibaba.demo.one2multi
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.annotation.MockInvoke
|
||||
|
||||
class ASvcMock {
|
||||
|
||||
@MockMethod(targetClass = String::class, targetMethod = "format")
|
||||
@MockInvoke(targetClass = String::class, targetMethod = "format")
|
||||
fun a_format(format: String, vararg args: Any?): String {
|
||||
return "a_mock"
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.alibaba.demo.one2multi
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.annotation.MockInvoke
|
||||
|
||||
class BSvcMock {
|
||||
|
||||
@MockMethod(targetClass = String::class, targetMethod = "format")
|
||||
@MockInvoke(targetClass = String::class, targetMethod = "format")
|
||||
fun b_format(format: String, vararg args: Any?): String {
|
||||
return "b_mock"
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.alibaba.demo.one2multi
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.annotation.MockInvoke
|
||||
|
||||
class CSvcMock {
|
||||
|
||||
@MockMethod(targetClass = String::class, targetMethod = "format")
|
||||
@MockInvoke(targetClass = String::class, targetMethod = "format")
|
||||
fun c_format(format: String, vararg args: Any?): String {
|
||||
return "c_mock"
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.github.pbetkier.spockdemo
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockConstructor
|
||||
import com.alibaba.testable.core.annotation.MockMethod
|
||||
import com.alibaba.testable.core.annotation.MockNew
|
||||
import com.alibaba.testable.core.annotation.MockInvoke
|
||||
import com.github.pbetkier.spockdemo.model.SpockBox
|
||||
import spock.lang.Shared
|
||||
import spock.lang.Specification
|
||||
@ -14,14 +14,14 @@ class DemoSpockTest extends Specification {
|
||||
def demoSpock = new DemoSpock()
|
||||
|
||||
static class Mock {
|
||||
@MockConstructor
|
||||
@MockNew
|
||||
SpockBox createBox() {
|
||||
SpockBox box = new SpockBox()
|
||||
box.put("mock zero")
|
||||
return box
|
||||
}
|
||||
|
||||
@MockMethod(targetMethod = "put")
|
||||
@MockInvoke(targetMethod = "put")
|
||||
void putBox(SpockBox self, String data) {
|
||||
self.put("mock " + data)
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ Besides `TestableMock`, there are also several other community mock tools, such
|
||||
|
||||
`JMockit` is a mock tool whose functionality and convenience are between `Mockito` and `PowerMock`, and it makes up for their respective shortcomings. The project tried to launch a rewritten version of JMockit2 in 2017 but failed to complete, and is currently in an inactive maintenance state.
|
||||
|
||||
The functionality of `TestabledMock` is basically the same as that of `PowerMock`, and it is extremely easy to use. You can complete most tasks only by mastering the annotations of `@MockMethod`.
|
||||
The functionality of `TestabledMock` is basically the same as that of `PowerMock`, and it is extremely easy to use. You can complete most tasks only by mastering the annotations of `@MockInvoke`.
|
||||
|
||||
The main disadvantage of the current `TestableMock` is that the IDE cannot promptly prompt whether the method parameters are matched correctly when writing the mock method. If the mocking effect does not meet expectation, it has to be verified during runtime through the method provided in the [self-help troubleshooting](en-us/doc/troubleshooting.md) document. This feature needs to be provided by extending IDE plugins in the future.
|
||||
|
||||
|
@ -1,5 +1,11 @@
|
||||
# Release Note
|
||||
|
||||
## 0.7.0
|
||||
- fix an improper exception issue when mock method with `associated` scope invoked
|
||||
- rename type `InvokeVerifier`/`InvockeMatcher` to `InvocationVerifier`/`InvocationMatcher`
|
||||
- rename method `verify` in `InvocationVerifier` type to `verifyInvoked`
|
||||
- rename annotation `@MockMethod`/`@MockConstructor` to `@MockInvoke`/`@MockNew`
|
||||
|
||||
## 0.6.10
|
||||
- support mock invocation via function reference
|
||||
- support mock method defined in a base interface
|
||||
@ -66,6 +72,13 @@
|
||||
- fix an exception caused by method parameter with ternary operator
|
||||
- fix a bug cause log message lost when `@MockWith` annotation used
|
||||
|
||||
## 0.5.0
|
||||
- split test class and mock class, let the mock class and method reusable
|
||||
- support use mock when package path of the test class is different from the class under test
|
||||
- support limit the mock effective scope to the cases of class under test it bound
|
||||
- use `TransmittableThreadLocal` to store mock context
|
||||
- add annotation `@MockDiagnose` to print debug logs
|
||||
|
||||
## 0.4.12
|
||||
- support verbose diagnose log for better self-troubleshooting
|
||||
- support disable private access target existence check
|
||||
|
@ -78,7 +78,7 @@ class DemoTest {
|
||||
|
||||
public static class Mock {
|
||||
// Intercept `System.out.println` invocation
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
public void println(PrintStream ps, String msg) {
|
||||
// Execute the original call
|
||||
ps.println(msg);
|
||||
|
@ -6,8 +6,8 @@ In unit testing, the main role of the mock method is to replace those methods wi
|
||||
Based on the above information, `TestableMock` has designed a minimalist mock mechanism. Unlike the common mock tools that uses **class** as the definition granularity of mocking, and repeats the description of mock behavior in each test case, `TestableMock` allows each business class (class under test) to be associated with a set of reusable collection of mock methods (carried by the mock container class), following the principle of "convention over configuration", and mock method replacement will automatically happen when the specified method in the test class match an invocation in the class under test.
|
||||
|
||||
> In summary, there are two simple rules:
|
||||
> - Mock non-constructive method, copy the original method definition to the mock class, add a `@MockMethod` annotation
|
||||
> - Mock construction method, copy the original method definition to the mock class, replace the return value with the constructed type, the method name is arbitrary, and add a `@MockContructor` annotation
|
||||
> - Mock non-constructive method, copy the original method definition to the mock class, add a `@MockInvoke` annotation
|
||||
> - Mock construction method, copy the original method definition to the mock class, replace the return value with the constructed type, the method name is arbitrary, and add a `@MockNew` annotation
|
||||
|
||||
The detail mock method definition convention is as follows.
|
||||
|
||||
@ -27,7 +27,7 @@ public class DemoTest {
|
||||
|
||||
### 1.1 Mock method calls of any class
|
||||
|
||||
Define an ordinary method annotated with `@MockMethod` in the mock class with exactly the same signature (name, parameter, and return value type) as the method to be mocked, and then add the type of target object (which the method originally belongs to) as `targetMethod` parameter of `@MockMethod` annotation.
|
||||
Define an ordinary method annotated with `@MockInvoke` in the mock class with exactly the same signature (name, parameter, and return value type) as the method to be mocked, and then add the type of target object (which the method originally belongs to) as `targetMethod` parameter of `@MockInvoke` annotation.
|
||||
|
||||
At this time, all invocations to that original method in the class under test will be automatically replaced with invocations to the above-mentioned mock method when the unit test is running.
|
||||
|
||||
@ -36,33 +36,33 @@ For example, there is a call to `"anything".substring(1, 2)` in the class under
|
||||
```java
|
||||
// The original method signature is `String substring(int, int)`
|
||||
// The object `"anything"` that invokes this method is of type `String`
|
||||
@MockMethod(targetClass = String.class)
|
||||
@MockInvoke(targetClass = String.class)
|
||||
private String substring(int i, int j) {
|
||||
return "sub_string";
|
||||
}
|
||||
```
|
||||
|
||||
When several methods to be mocked have the same name, you can put the name of the method to be mocked in the `targetMethod` parameter of `@MockMethod` annotation, so that the mock method itself can be named at will.
|
||||
When several methods to be mocked have the same name, you can put the name of the method to be mocked in the `targetMethod` parameter of `@MockInvoke` annotation, so that the mock method itself can be named at will.
|
||||
|
||||
The following example shows the usage of the `targetMethod` parameter, and its effect is the same as the above example:
|
||||
|
||||
```java
|
||||
// Use `targetMethod` to specify the name of the method that needs to be mocked
|
||||
// The method itself can now be named arbitrarily, but the method parameters still need to follow the same matching rules
|
||||
@MockMethod(targetClass = String.class, targetMethod = "substring")
|
||||
@MockInvoke(targetClass = String.class, targetMethod = "substring")
|
||||
private String use_any_mock_method_name(int i, int j) {
|
||||
return "sub_string";
|
||||
}
|
||||
```
|
||||
|
||||
Sometimes, the mock method need to access the member variables in the original object that initiated the invocation, or invoke other methods of the original object. At this point, you can remove the `targetClass` parameter in the `@MockMethod` annotation, and then add a extra parameter whose type is the original object type of the method to the first index of the method parameter list.
|
||||
Sometimes, the mock method need to access the member variables in the original object that initiated the invocation, or invoke other methods of the original object. At this point, you can remove the `targetClass` parameter in the `@MockInvoke` annotation, and then add a extra parameter whose type is the original object type of the method to the first index of the method parameter list.
|
||||
|
||||
The `TestableMock` convention is that when the `targetClass` parameter of the `@MockMethod` annotation is not defined, the first parameter of the mock method is the type of the target method, and the parameter name is arbitrary. In order to facilitate code reading, it is recommended to name this parameter as `self` or `src`. Example as follows:
|
||||
The `TestableMock` convention is that when the `targetClass` parameter of the `@MockInvoke` annotation is not defined, the first parameter of the mock method is the type of the target method, and the parameter name is arbitrary. In order to facilitate code reading, it is recommended to name this parameter as `self` or `src`. Example as follows:
|
||||
|
||||
```java
|
||||
// Adds a `String` type parameter to the first position the mock method parameter list (parameter name is arbitrary)
|
||||
// This parameter can be used to get the value and context of the actual invoker at runtime
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
private String substring(String self, int i, int j) {
|
||||
// Call the original method is also allowed
|
||||
return self.substring(i, j);
|
||||
@ -81,7 +81,7 @@ For example, there is a private method with the signature `String innerFunc(Stri
|
||||
|
||||
```java
|
||||
// The type to test is `DemoMock`
|
||||
@MockMethod(targetClass = DemoMock.class)
|
||||
@MockInvoke(targetClass = DemoMock.class)
|
||||
private String innerFunc(String text) {
|
||||
return "mock_" + text;
|
||||
}
|
||||
@ -98,7 +98,7 @@ Mock for static methods is the same as for any ordinary methods.
|
||||
For example, if the static method `secretBox()` of the `BlackBox` type is invoked in the class under test, and the method signature is `BlackBox secretBox()`, then the mock method is as follows:
|
||||
|
||||
```java
|
||||
@MockMethod(targetClass = BlackBox.class)
|
||||
@MockInvoke(targetClass = BlackBox.class)
|
||||
private BlackBox secretBox() {
|
||||
return new BlackBox("not_secret_box");
|
||||
}
|
||||
@ -108,7 +108,7 @@ For complete code examples, see the `should_mock_static_method()` test case in t
|
||||
|
||||
### 1.4 Mock `new` operation of any type
|
||||
|
||||
Define an ordinary method annotated with `@MockContructor` in the mock class, make the return value type of the method the type of the object to be created, and the method parameters are exactly the same as the constructor parameters to be mocked, the method name is arbitrary.
|
||||
Define an ordinary method annotated with `@MockNew` in the mock class, make the return value type of the method the type of the object to be created, and the method parameters are exactly the same as the constructor parameters to be mocked, the method name is arbitrary.
|
||||
|
||||
At this time, all operations in the class under test that use `new` to create the specified class (and use the constructor that is consistent with the mock method parameters) will be replaced with calls to the custom method.
|
||||
|
||||
@ -117,13 +117,13 @@ For example, if there is a call to `new BlackBox("something")` in the class unde
|
||||
```java
|
||||
// The signature of the constructor to be mocked is `BlackBox(String)`
|
||||
// No need to add additional parameters to the mock method parameter list, and the name of the mock method is arbitrary
|
||||
@MockContructor
|
||||
@MockNew
|
||||
private BlackBox createBlackBox(String text) {
|
||||
return new BlackBox("mock_" + text);
|
||||
}
|
||||
```
|
||||
|
||||
> You can still use the `@MockMethod` annotation, and configure the `targetMethod` parameter value to `"<init>"`, and the rest is the same as above. The effect is the same as using the `@MockContructor` annotation
|
||||
> You can still use the `@MockInvoke` annotation, and configure the `targetMethod` parameter value to `"<init>"`, and the rest is the same as above. The effect is the same as using the `@MockNew` annotation
|
||||
|
||||
For complete code examples, see the `should_mock_new_object()` test case in the `java-demo` and `kotlin-demo` sample projects.
|
||||
|
||||
@ -146,7 +146,7 @@ public void testDemo() {
|
||||
Take out the injected parameters in the mock method and return different results according to the situation:
|
||||
|
||||
```java
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
private Data mockDemo() {
|
||||
switch((String)MOCK_CONTEXT.get("case")) {
|
||||
case "data-ready":
|
||||
|
@ -14,7 +14,7 @@
|
||||
| srcClass | Class | 否 | N/A | 当测试类命名不符合约定时,指定实际被测类 |
|
||||
| verifyTargetOnCompile | boolean | 否 | true | 是否启用私有目标的编译期存在性校验 |
|
||||
|
||||
#### @MockMethod
|
||||
#### @MockInvoke
|
||||
|
||||
将当前方法标识为待匹配的Mock成员方法。
|
||||
|
||||
@ -26,7 +26,7 @@
|
||||
| targetMethod | String | 否 | N/A | 指定Mock目标的方法名 |
|
||||
| scope | MockScope | 否 | MockScope.GLOBAL | 指定Mock的生效范围 |
|
||||
|
||||
#### @MockConstructor
|
||||
#### @MockNew
|
||||
|
||||
将当前方法标识为待匹配的Mock构造方法。
|
||||
|
||||
|
@ -19,7 +19,7 @@
|
||||
|
||||
`JMockit`是一款功能性与易用性均居于`Mockito`与`PowerMock`之间的Mock工具,较好的弥补了两者各自的不足。该项目在2017年尝试推出JMockit2重写版本但未能完成,目前处于不活跃的维护状态。
|
||||
|
||||
相比之下,`TestabledMock`的功能与`PowerMock`基本平齐,且极易上手,只需掌握`@MockMethod`注解就可以完成绝大多数任务。
|
||||
相比之下,`TestabledMock`的功能与`PowerMock`基本平齐,且极易上手,只需掌握`@MockInvoke`注解就可以完成绝大多数任务。
|
||||
|
||||
当前`TestableMock`的主要不足在于,编写Mock方法时IDE无法即时提示方法参数是否正确匹配。若发现匹配效果不符合预期,需要通过[自助问题排查](zh-cn/doc/troubleshooting.md)文档提供的方法在运行期进行校验。这个功能理论上能够通过扩展主流IDE插件来补充,但目前暂无相关开发计划,参见[Issue-104](https://github.com/alibaba/testable-mock/issues/104)。
|
||||
|
||||
|
@ -51,7 +51,7 @@ public class BbbServiceTest {
|
||||
}
|
||||
|
||||
public class BasicMock {
|
||||
@MockMethod(targetClass = UserDao.class)
|
||||
@MockInvoke(targetClass = UserDao.class)
|
||||
protected String getById(int id) {
|
||||
...
|
||||
}
|
||||
|
@ -1,5 +1,11 @@
|
||||
# Release Note
|
||||
|
||||
## 0.7.0
|
||||
- 修复当`scope`为`associated`的Mock方法被`null`对象调用,且上下文与测试用例未关联时抛错不合理的问题(issue-163)
|
||||
- 类型`InvokeVerifier`和`InvockeMatcher`更名为`InvocationVerifier`和`InvocationMatcher`
|
||||
- 类型`InvocationVerifier`中的`verify`方法与`com.sun`包中的方法重名,更名为`verifyInvoked`
|
||||
- 注解`@MockMethod`和`@MockConstructor`更名为`@MockInvoke`和`@MockNew`
|
||||
|
||||
## 0.6.10
|
||||
- 支持Mock通过方法引用执行的调用(issue-233 / pr-234)
|
||||
- 支持Mock基类接口中的方法(pr-231)
|
||||
|
@ -1,14 +1,14 @@
|
||||
Mock的生效范围
|
||||
---
|
||||
|
||||
在`@MockMethod`和`@MockConstructor`注解上都有一个`scope`参数,其可选值有两种
|
||||
在`@MockInvoke`和`@MockNew`注解上都有一个`scope`参数,其可选值有两种
|
||||
|
||||
- `MockScope.GLOBAL`:该Mock方法将全局生效
|
||||
- `MockScope.ASSOCIATED`:该Mock方法仅对Mock容器关联测试类中的测试用例生效
|
||||
|
||||
对于常规项目而言,单元测试里需要被Mock的调用都是由于其中包含了不需要或不便于测试的逻辑,譬如“依赖外部系统”、“包含随机结果”、“执行非常耗时”等等,这类调用在整个单元测试的生命周期里都应该被Mock方法置换,不论调用的发起者是谁。因此`TestableMock`默认所有Mock方法都是全局生效的,即`scope`默认值为`MockScope.GLOBAL`。
|
||||
|
||||
> 举例来说,`CookerService`和`SellerService`是两个需要被测试的类,假设`CookerService`的代码里的`hireXxx()`和`cookXxx()`方法都需要依赖外部系统。因此在进行单元测试时,开发者在`CookerService`关联的Mock容器里使用`@MockMethod`注解定义了这些调用的替代方法。
|
||||
> 举例来说,`CookerService`和`SellerService`是两个需要被测试的类,假设`CookerService`的代码里的`hireXxx()`和`cookXxx()`方法都需要依赖外部系统。因此在进行单元测试时,开发者在`CookerService`关联的Mock容器里使用`@MockInvoke`注解定义了这些调用的替代方法。
|
||||
>
|
||||
> 此时,若该Mock方法的`scope`值为`MockScope.GLOBAL`,则不论是在`SellerServiceTest`测试类还是在`CookerServiceTest`测试类的测试用例,只要直接或间接的执行到这行调用,都会被置换为调用Mock方法。若该Mock方法的`scope`值为`MockScope.ASSOCIATED`,则Mock只对`CookerServiceTest`类中的测试用例生效,而`SellerServiceTest`类中的测试用例在运行过程中执行到了`CookerService`类的相关代码,将会执行原本的调用。
|
||||
>
|
||||
@ -24,7 +24,7 @@ Mock的生效范围
|
||||
> ```
|
||||
> 若默认的`scope`参数不是`MockScope.GLOBAL`,则相应Mock方法应当显式的声明`scope`值,例如:
|
||||
> ```java
|
||||
> @MockMethod(targetClass = System.class, scope = MockScope.GLOBAL)
|
||||
> @MockInvoke(targetClass = System.class, scope = MockScope.GLOBAL)
|
||||
> private void loadLibrary(String libname) {
|
||||
> System.err.println("loadLibrary " + libname);
|
||||
> }
|
||||
|
@ -79,7 +79,7 @@ class DemoTest {
|
||||
|
||||
public static class Mock {
|
||||
// 拦截System.out.println调用
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
public void println(PrintStream ps, String msg) {
|
||||
// 执行原调用
|
||||
ps.println(msg);
|
||||
|
@ -6,8 +6,8 @@
|
||||
基于上述特点,`TestableMock`设计了一种极简的Mock机制。与以往Mock工具以**类**作为Mock的定义粒度,在每个测试用例里各自重复描述Mock行为的方式不同,`TestableMock`让每个业务类(被测类)关联一组可复用的Mock方法集合(使用Mock容器类承载),并遵循约定优于配置的原则,按照规则自动在测试运行时替换被测类中的指定方法调用。
|
||||
|
||||
> 实际规则约定归纳起来只有两条:
|
||||
> - Mock非构造方法,拷贝原方法定义到Mock容器类,加`@MockMethod`注解
|
||||
> - Mock构造方法,拷贝原方法定义到Mock容器类,返回值换成构造的类型,方法名随意,加`@MockContructor`注解
|
||||
> - Mock非构造方法,拷贝原方法定义到Mock容器类,加`@MockInvoke`注解
|
||||
> - Mock构造方法,拷贝原方法定义到Mock容器类,返回值换成构造的类型,方法名随意,加`@MockNew`注解
|
||||
|
||||
具体使用方法如下。
|
||||
|
||||
@ -27,7 +27,7 @@ public class DemoTest {
|
||||
|
||||
### 1.1 覆写任意类的方法调用
|
||||
|
||||
在Mock容器类中定义一个有`@MockMethod`注解的普通方法,使它与需覆写的方法名称、参数、返回值类型完全一致,并在注解的`targetClass`参数指定该方法原本所属对象类型。
|
||||
在Mock容器类中定义一个有`@MockInvoke`注解的普通方法,使它与需覆写的方法名称、参数、返回值类型完全一致,并在注解的`targetClass`参数指定该方法原本所属对象类型。
|
||||
|
||||
此时被测类中所有对该需覆写方法的调用,将在单元测试运行时,将自动被替换为对上述自定义Mock方法的调用。
|
||||
|
||||
@ -36,33 +36,33 @@ public class DemoTest {
|
||||
```java
|
||||
// 原方法签名为`String substring(int, int)`
|
||||
// 调用此方法的对象`"something"`类型为`String`
|
||||
@MockMethod(targetClass = String.class)
|
||||
@MockInvoke(targetClass = String.class)
|
||||
private String substring(int i, int j) {
|
||||
return "sub_string";
|
||||
}
|
||||
```
|
||||
|
||||
当遇到待覆写方法有重名时,可以将需覆写的方法名写到`@MockMethod`注解的`targetMethod`参数里,这样Mock方法自身就可以随意命名了。
|
||||
当遇到待覆写方法有重名时,可以将需覆写的方法名写到`@MockInvoke`注解的`targetMethod`参数里,这样Mock方法自身就可以随意命名了。
|
||||
|
||||
下面这个例子展示了`targetMethod`参数的用法,其效果与上述示例相同:
|
||||
|
||||
```java
|
||||
// 使用`targetMethod`指定需Mock的方法名
|
||||
// 此方法本身现在可以随意命名,但方法参数依然需要遵循相同的匹配规则
|
||||
@MockMethod(targetClass = String.class, targetMethod = "substring")
|
||||
@MockInvoke(targetClass = String.class, targetMethod = "substring")
|
||||
private String use_any_mock_method_name(int i, int j) {
|
||||
return "sub_string";
|
||||
}
|
||||
```
|
||||
|
||||
有时,在Mock方法里会需要访问发起调用的原始对象中的成员变量,或是调用原始对象的其他方法。此时,可以将`@MockMethod`注解中的`targetClass`参数去除,然后在方法参数列表首位增加一个类型为该方法原本所属对象类型的参数。
|
||||
有时,在Mock方法里会需要访问发起调用的原始对象中的成员变量,或是调用原始对象的其他方法。此时,可以将`@MockInvoke`注解中的`targetClass`参数去除,然后在方法参数列表首位增加一个类型为该方法原本所属对象类型的参数。
|
||||
|
||||
`TestableMock`约定,当`@MockMethod`注解的`targetClass`参数未定义时,Mock方法的首位参数即为目标方法所属类型,参数名称随意。通常为了便于代码阅读,建议将此参数统一命名为`self`或`src`。举例如下:
|
||||
`TestableMock`约定,当`@MockInvoke`注解的`targetClass`参数未定义时,Mock方法的首位参数即为目标方法所属类型,参数名称随意。通常为了便于代码阅读,建议将此参数统一命名为`self`或`src`。举例如下:
|
||||
|
||||
```java
|
||||
// Mock方法在参数列表首位增加一个类型为`String`的参数(名字随意)
|
||||
// 此参数可用于获得当时的实际调用者的值和上下文
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
private String substring(String self, int i, int j) {
|
||||
// 可以直接调用原方法,此时Mock方法仅用于记录调用,常见于对void方法的测试
|
||||
return self.substring(i, j);
|
||||
@ -81,7 +81,7 @@ private String substring(String self, int i, int j) {
|
||||
|
||||
```java
|
||||
// 被测类型是`DemoMock`
|
||||
@MockMethod(targetClass = DemoMock.class)
|
||||
@MockInvoke(targetClass = DemoMock.class)
|
||||
private String innerFunc(String text) {
|
||||
return "mock_" + text;
|
||||
}
|
||||
@ -98,7 +98,7 @@ private String innerFunc(String text) {
|
||||
例如,在被测类中调用了`BlackBox`类型中的静态方法`secretBox()`,该方法签名为`BlackBox secretBox()`,则Mock方法如下:
|
||||
|
||||
```java
|
||||
@MockMethod(targetClass = BlackBox.class)
|
||||
@MockInvoke(targetClass = BlackBox.class)
|
||||
private BlackBox secretBox() {
|
||||
return new BlackBox("not_secret_box");
|
||||
}
|
||||
@ -110,7 +110,7 @@ private BlackBox secretBox() {
|
||||
|
||||
### 1.4 覆写任意类的new操作
|
||||
|
||||
在Mock容器类里定义一个返回值类型为要被创建的对象类型,且方法参数与要Mock的构造函数参数完全一致的方法,名称随意,然后加上`@MockContructor`注解。
|
||||
在Mock容器类里定义一个返回值类型为要被创建的对象类型,且方法参数与要Mock的构造函数参数完全一致的方法,名称随意,然后加上`@MockNew`注解。
|
||||
|
||||
此时被测类中所有用`new`创建指定类的操作(并使用了与Mock方法参数一致的构造函数)将被替换为对该自定义方法的调用。
|
||||
|
||||
@ -119,7 +119,7 @@ private BlackBox secretBox() {
|
||||
```java
|
||||
// 要覆写的构造函数签名为`BlackBox(String)`
|
||||
// Mock方法返回`BlackBox`类型对象,方法的名称随意起
|
||||
@MockContructor
|
||||
@MockNew
|
||||
private BlackBox createBlackBox(String text) {
|
||||
return new BlackBox("mock_" + text);
|
||||
}
|
||||
@ -146,7 +146,7 @@ public void testDemo() {
|
||||
在Mock方法中取出注入的参数,根据情况返回不同结果:
|
||||
|
||||
```java
|
||||
@MockMethod
|
||||
@MockInvoke
|
||||
private Data mockDemo() {
|
||||
switch((String)MOCK_CONTEXT.get("case")) {
|
||||
case "data-ready":
|
||||
|
@ -16,8 +16,8 @@ public class ConstPool {
|
||||
public static final String MOCK_WITH = "com.alibaba.testable.core.annotation.MockWith";
|
||||
public static final String DUMP_TO = "com.alibaba.testable.core.annotation.DumpTo";
|
||||
public static final String MOCK_DIAGNOSE = "com.alibaba.testable.core.annotation.MockDiagnose";
|
||||
public static final String MOCK_METHOD = "com.alibaba.testable.core.annotation.MockMethod";
|
||||
public static final String MOCK_CONSTRUCTOR = "com.alibaba.testable.core.annotation.MockConstructor";
|
||||
public static final String MOCK_INVOKE = "com.alibaba.testable.core.annotation.MockInvoke";
|
||||
public static final String MOCK_NEW = "com.alibaba.testable.core.annotation.MockNew";
|
||||
|
||||
public static final String CGLIB_CLASS_PATTERN = "$$EnhancerBy";
|
||||
public static final String KOTLIN_POSTFIX_COMPANION = "$Companion";
|
||||
|
@ -93,12 +93,12 @@ public class MockClassHandler extends BaseClassWithContextHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* put targetClass parameter in @MockMethod to first parameter of the mock method
|
||||
* put targetClass parameter in @MockInvoke to first parameter of the mock method
|
||||
*/
|
||||
private void unfoldTargetClass(MethodNode mn) {
|
||||
String targetClassName = null;
|
||||
for (AnnotationNode an : mn.visibleAnnotations) {
|
||||
if (ClassUtil.toByteCodeClassName(ConstPool.MOCK_METHOD).equals(an.desc)) {
|
||||
if (ClassUtil.toByteCodeClassName(ConstPool.MOCK_INVOKE).equals(an.desc)) {
|
||||
Type type = AnnotationUtil.getAnnotationParameter(an, ConstPool.FIELD_TARGET_CLASS, null, Type.class);
|
||||
if (type != null) {
|
||||
targetClassName = ClassUtil.toByteCodeClassName(type.getClassName());
|
||||
@ -212,7 +212,7 @@ public class MockClassHandler extends BaseClassWithContextHandler {
|
||||
if (name != null) {
|
||||
methodName = name;
|
||||
}
|
||||
} else if (isMockConstructorAnnotation(an)) {
|
||||
} else if (isMockNewAnnotation(an)) {
|
||||
methodName = CONSTRUCTOR;
|
||||
}
|
||||
}
|
||||
@ -226,7 +226,7 @@ public class MockClassHandler extends BaseClassWithContextHandler {
|
||||
|
||||
private boolean isGlobalScope(MethodNode mn) {
|
||||
for (AnnotationNode an : mn.visibleAnnotations) {
|
||||
if (isMockMethodAnnotation(an) || isMockConstructorAnnotation(an)) {
|
||||
if (isMockMethodAnnotation(an) || isMockNewAnnotation(an)) {
|
||||
MockScope scope = AnnotationUtil.getAnnotationParameter(an, ConstPool.FIELD_SCOPE,
|
||||
GlobalConfig.defaultMockScope, MockScope.class);
|
||||
if (scope.equals(MockScope.GLOBAL)) {
|
||||
@ -248,7 +248,7 @@ public class MockClassHandler extends BaseClassWithContextHandler {
|
||||
getTargetMethodOwner(mn, an), getTargetMethodName(mn, an), getTargetMethodDesc(mn, an)));
|
||||
}
|
||||
return true;
|
||||
} else if (isMockConstructorAnnotation(an)) {
|
||||
} else if (isMockNewAnnotation(an)) {
|
||||
if (LogUtil.isVerboseEnabled()) {
|
||||
LogUtil.verbose(" Mock constructor \"%s\" as \"%s\"", mn.name, MethodUtil.toJavaMethodDesc(
|
||||
ClassUtil.toJavaStyleClassName(MethodUtil.getReturnType(mn.desc)), mn.desc));
|
||||
@ -277,12 +277,12 @@ public class MockClassHandler extends BaseClassWithContextHandler {
|
||||
return type == null ? MethodUtil.removeFirstParameter(mn.desc) : mn.desc;
|
||||
}
|
||||
|
||||
private boolean isMockConstructorAnnotation(AnnotationNode an) {
|
||||
return ClassUtil.toByteCodeClassName(ConstPool.MOCK_CONSTRUCTOR).equals(an.desc);
|
||||
private boolean isMockNewAnnotation(AnnotationNode an) {
|
||||
return ClassUtil.toByteCodeClassName(ConstPool.MOCK_NEW).equals(an.desc);
|
||||
}
|
||||
|
||||
private boolean isMockMethodAnnotation(AnnotationNode an) {
|
||||
return ClassUtil.toByteCodeClassName(ConstPool.MOCK_METHOD).equals(an.desc);
|
||||
return ClassUtil.toByteCodeClassName(ConstPool.MOCK_INVOKE).equals(an.desc);
|
||||
}
|
||||
|
||||
private void injectInvokeRecorder(MethodNode mn) {
|
||||
@ -324,9 +324,9 @@ public class MockClassHandler extends BaseClassWithContextHandler {
|
||||
private boolean isMockForConstructor(MethodNode mn) {
|
||||
for (AnnotationNode an : mn.visibleAnnotations) {
|
||||
String annotationName = ClassUtil.toJavaStyleClassName(an.desc);
|
||||
if (ConstPool.MOCK_CONSTRUCTOR.equals(annotationName)) {
|
||||
if (ConstPool.MOCK_NEW.equals(annotationName)) {
|
||||
return true;
|
||||
} else if (ConstPool.MOCK_METHOD.equals(annotationName)) {
|
||||
} else if (ConstPool.MOCK_INVOKE.equals(annotationName)) {
|
||||
String method = AnnotationUtil.getAnnotationParameter
|
||||
(an, ConstPool.FIELD_TARGET_METHOD, null, String.class);
|
||||
if (CONSTRUCTOR.equals(method)) {
|
||||
|
@ -308,7 +308,7 @@ public class SourceClassHandler extends BaseClassHandler {
|
||||
mn.instructions.remove(instructions[end - 1]);
|
||||
}
|
||||
}
|
||||
// method with @MockMethod will be modified as public access
|
||||
// method with @MockInvoke will be modified as public access
|
||||
int invokeOpcode = mockMethod.isStatic() ? INVOKESTATIC : INVOKEVIRTUAL;
|
||||
mn.instructions.insertBefore(instructions[end], new MethodInsnNode(invokeOpcode, mockClassName,
|
||||
mockMethod.getMockName(), mockMethod.getMockDesc(), false));
|
||||
|
@ -57,8 +57,8 @@ public class MockClassParser {
|
||||
if (mn.visibleAnnotations != null) {
|
||||
for (AnnotationNode an : mn.visibleAnnotations) {
|
||||
String fullClassName = toJavaStyleClassName(an.desc);
|
||||
if (fullClassName.equals(ConstPool.MOCK_METHOD) ||
|
||||
fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) {
|
||||
if (fullClassName.equals(ConstPool.MOCK_INVOKE) ||
|
||||
fullClassName.equals(ConstPool.MOCK_NEW)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -98,13 +98,13 @@ public class MockClassParser {
|
||||
}
|
||||
for (AnnotationNode an : mn.visibleAnnotations) {
|
||||
String fullClassName = toJavaStyleClassName(an.desc);
|
||||
if (fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) {
|
||||
if (fullClassName.equals(ConstPool.MOCK_NEW)) {
|
||||
if (GlobalConfig.checkMockTargetExistence) {
|
||||
checkTargetConstructorExists(cn, mn);
|
||||
}
|
||||
methodInfos.add(new MethodInfo(ClassUtil.getSourceClassName(cn.name), CONSTRUCTOR, mn.desc, cn.name,
|
||||
mn.name, mn.desc, isStatic(mn)));
|
||||
} else if (fullClassName.equals(ConstPool.MOCK_METHOD) && isValidMockMethod(mn, an)) {
|
||||
} else if (fullClassName.equals(ConstPool.MOCK_INVOKE) && isValidMockMethod(mn, an)) {
|
||||
if (GlobalConfig.checkMockTargetExistence) {
|
||||
checkTargetMethodExists(cn, mn, an);
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import java.lang.annotation.*;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@Documented
|
||||
public @interface MockMethod {
|
||||
public @interface MockInvoke {
|
||||
|
||||
/**
|
||||
* mock specified method instead of method with same name
|
@ -12,7 +12,7 @@ import java.lang.annotation.*;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@Documented
|
||||
public @interface MockConstructor {
|
||||
public @interface MockNew {
|
||||
|
||||
/**
|
||||
* specify the effective scope of the mock method
|
Loading…
Reference in New Issue
Block a user