Compare commits

...

20 Commits
v0.6.8 ... 0.7

Author SHA1 Message Date
金戟
c2d456da97 use MockInvoke and MockNew 2021-11-03 11:44:44 +08:00
金戟
28500558fc rename verify() to verifyInvoked(), avoid name conflict with system method 2021-11-02 23:39:48 +08:00
金戟
990f407ebe should throw NullPointerException when invoke mock method on null object (issue-163) 2021-11-02 23:09:57 +08:00
金戟
f6206ee294 add release note of 0.6.10 2021-10-31 23:36:32 +08:00
金戟
8cfba333ff release v0.6.10 2021-10-31 14:24:10 +08:00
Fan Lin
397fc3d2fa Merge pull request #234 from zcbbpo/FIXED-LAMBDA-MR
[Lambda] Fixed method reference external var
2021-10-31 14:19:02 +08:00
jim cao
b7a076dfaf fixed external var 2021-10-30 19:40:21 +08:00
金戟
aee2ec2767 fix typo and format code 2021-10-27 07:33:08 +08:00
Fan Lin
682d822249 Merge pull request #231 from HankDevelop/0.6
接口继承支持
2021-10-27 07:27:46 +08:00
Fan Lin
6cf3f993ca Merge branch 'master' into 0.6 2021-10-27 07:27:23 +08:00
HankDevelop
0cce5a6ec0 接口继承支持 2021-10-26 19:57:10 +08:00
金戟
0917406c44 release v0.6.9 2021-10-17 22:56:33 +08:00
金戟
3bf19ee6d0 fix NullPointerException when passing null as parameter of private accessor 2021-10-17 22:56:33 +08:00
Fan Lin
e1afb1cd54 Merge pull request #223 from Augustine-C/fix_flaky
Fixed a flaky test in core.tool.OmniAccessorTest
2021-09-30 10:47:59 +08:00
Augustine Cui
10953ff02a Fixed a flaky test in core.tool.OmniAccessorTest
This test is flaky since the method `java.lang.Class.getDeclaredFields` is non-deterministic, and `GetDeclaredFilelds`  is ultimately used in the `com.alibaba.testable.core.util.TypeUtil.getAllFields`
I converted the result to a `HashSet` to avoid ordering issues, so the specific ordering of the fields would not affect the result. I think this change can prevent test failure due to future change in the JVM.
2021-09-29 17:10:37 -07:00
金戟
80b0f3b878 do not introduce sun package dependence 2021-09-28 21:26:16 +08:00
Fan Lin
8fbd459280 Merge pull request #208 from zcbbpo/FIXED-LAMBDA-METHOD-REFERENCE-ISSUES
Fixed method reference
2021-09-28 20:46:51 +08:00
jimcao
f3309f933e fixed method reference 2021-09-24 15:20:13 +08:00
金戟
b47204e329 hold images in cdn 2021-09-15 21:11:29 +08:00
金戟
6616ff1174 add description about modify static final member 2021-08-09 08:56:40 +08:00
94 changed files with 1738 additions and 449 deletions

View File

@@ -2,7 +2,7 @@
换种思路写Mock让单元测试更简单。
无需初始化不挑服务框架甭管要换的是私有方法、静态方法、构造方法还是其他任何类的任何方法也甭管要换的对象是怎么创建的。写好Mock定义加个`@MockMethod`注解,一切统统搞定。
无需初始化不挑服务框架甭管要换的是私有方法、静态方法、构造方法还是其他任何类的任何方法也甭管要换的对象是怎么创建的。写好Mock定义加个`@MockInvoke`注解,一切统统搞定。
- 文档https://alibaba.github.io/testable-mock/
- 国内文档镜像http://freyrlin.gitee.io/testable-mock/

View File

@@ -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/

View File

@@ -49,7 +49,7 @@ dependencies {
testImplementation 'androidx.test:runner:1.4.0-alpha05'
testImplementation 'junit:junit:4.+'
testImplementation 'org.robolectric:robolectric:4.5.1'
testImplementation 'com.alibaba.testable:testable-all:0.6.8'
testImplementation 'com.alibaba.testable:testable-all:0.6.10'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

View File

@@ -1,15 +1,15 @@
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;
import java.util.concurrent.Executors;
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
import static com.alibaba.testable.core.tool.TestableTool.MOCK_CONTEXT;
import static com.alibaba.testable.core.tool.TestableTool.SOURCE_METHOD;
import static org.junit.Assert.assertEquals;
@@ -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";
@@ -75,28 +75,28 @@ public class DemoBasicTest {
@Test
public void should_mock_new_object() {
assertEquals("mock_something", demoBasic.newFunc());
verify("createBlackBox").with("something");
verifyInvoked("createBlackBox").with("something");
}
@Test
public void should_mock_member_method() throws Exception {
assertEquals("{ \"res\": \"mock_hello_MOCK_TAIL\"}", demoBasic.outerFunc("hello"));
verify("innerFunc").with("hello");
verify("staticFunc").with();
verifyInvoked("innerFunc").with("hello");
verifyInvoked("staticFunc").with();
}
@Test
public void should_mock_common_method() {
assertEquals("trim_string__sub_string__false", demoBasic.commonFunc());
verify("trim").withTimes(1);
verify("sub").withTimes(1);
verify("startsWith").withTimes(1);
verifyInvoked("trim").withTimes(1);
verifyInvoked("sub").withTimes(1);
verifyInvoked("startsWith").withTimes(1);
}
@Test
public void should_mock_static_method() {
assertEquals("not_secret_box", demoBasic.getBox().get());
verify("secretBox").withTimes(1);
verifyInvoked("secretBox").withTimes(1);
}
@Test
@@ -106,7 +106,7 @@ public class DemoBasicTest {
// asynchronous
assertEquals("mock_one_mock_others",
Executors.newSingleThreadExecutor().submit(() -> demoBasic.callerOne() + "_" + demoBasic.callerTwo()).get());
verify("callFromDifferentMethod").withTimes(4);
verifyInvoked("callFromDifferentMethod").withTimes(4);
}
@Test
@@ -116,7 +116,7 @@ public class DemoBasicTest {
assertEquals("mock_special", demoBasic.callerOne());
// asynchronous
assertEquals("mock_special", Executors.newSingleThreadExecutor().submit(() -> demoBasic.callerOne()).get());
verify("callFromDifferentMethod").withTimes(2);
verifyInvoked("callFromDifferentMethod").withTimes(2);
}
}

View File

@@ -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;
@@ -12,7 +12,7 @@ import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
@RunWith(RobolectricTestRunner.class)
@Config(sdk = 30)
@@ -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;
}
@@ -38,10 +38,10 @@ public class DemoServiceTest {
intent.setAction("start_foreground");
demoService.onStartCommand(intent, 0, 1);
verify("log").with("DemoService", "start service.");
verifyInvoked("log").with("DemoService", "start service.");
intent.setAction("stop_foreground");
demoService.onStartCommand(intent, 0, 1);
verify("log").with("DemoService", "stop service.");
verifyInvoked("log").with("DemoService", "stop service.");
}
}

View File

@@ -13,8 +13,8 @@ repositories {
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
testImplementation('com.alibaba.testable:testable-all:0.6.8')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.8')
testImplementation('com.alibaba.testable:testable-all:0.6.10')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.10')
}
tasks.withType(JavaCompile) {

View File

@@ -12,7 +12,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>5.6.2</junit.version>
<testable.version>0.6.8</testable.version>
<testable.version>0.6.10</testable.version>
</properties>
<dependencies>

View File

@@ -11,7 +11,7 @@ import com.alibaba.demo.basic.model.mock.Color;
public class DemoInherit {
/**
* call method overridden by sub class via parent class variable
* call method overridden by subclass via parent class variable
*/
public Box putIntoBox() {
Box box = new BlackBox("");
@@ -20,7 +20,7 @@ public class DemoInherit {
}
/**
* call method overridden by sub class via sub class variable
* call method overridden by subclass via subclass variable
*/
public BlackBox putIntoBlackBox() {
BlackBox box = new BlackBox("");
@@ -37,7 +37,7 @@ public class DemoInherit {
}
/**
* call method defined in parent class via sub class variable
* call method defined in parent class via subclass variable
*/
public String getFromBlackBox() {
BlackBox box = new BlackBox("data");
@@ -53,10 +53,18 @@ public class DemoInherit {
}
/**
* call method defined in interface via sub class variable
* call method defined in interface via subclass variable
*/
public String getColorViaBox() {
BlackBox box = new BlackBox("");
return box.getColor();
}
/**
* call method defined in interface via subclass variable
*/
public String getColorIdxViaColor() {
Color color = new BlackBox("");
return color.getColorIndex();
}
}

View File

@@ -46,7 +46,9 @@ public class DemoPrivateAccess {
* private member method with arguments
*/
private String privateFuncWithArgs(List<String> list, String str, int i) {
return list.stream().reduce((a, s) -> a + s).orElse("") + " + " + str + " + " + i;
return list.stream().reduce((a, s) -> a + s).orElse("")
+ " + " + (str == null ? "null" : str)
+ " + " + i;
}
}

View File

@@ -0,0 +1,7 @@
package com.alibaba.demo.basic.model.mock;
public interface BasicColor {
String getColorIndex();
}

View File

@@ -20,4 +20,8 @@ public class BlackBox extends Box implements Color {
return "black";
}
@Override
public String getColorIndex() {
return "idx";
}
}

View File

@@ -1,6 +1,6 @@
package com.alibaba.demo.basic.model.mock;
public interface Color {
public interface Color extends BasicColor {
String getColor();

View File

@@ -0,0 +1,243 @@
package com.alibaba.demo.lambda;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @author jimca
*/
@SuppressWarnings({"WrapperTypeMayBePrimitive", "ResultOfMethodCallIgnored", "MismatchedReadAndWriteOfArray", "unused"})
public class ExternalLambdaDemo {
public void string1() {
String s = "";
consumesFunction2(s::contains);
}
public void string2() {
String s = "";
consumesFunction2(s::charAt);
}
public void string3() {
String s = "";
consumesFunction0(s::notify);
}
public void byte1() {
Byte s = 1;
consumesSupplier(s::floatValue);
}
public void byte2() {
Byte s = 1;
consumesFunction2(s::compareTo);
}
public void byte3() {
Byte s = 1;
consumesFunction0(s::notify);
}
public void char1() {
Character s = 1;
consumesFunction0(s::toString);
}
public void char2() {
Character s = 1;
consumesFunction2(s::compareTo);
}
public void char3() {
Character s = 1;
consumesFunction0(s::notify);
}
public void short1() {
Short s = 1;
consumesFunction0(s::toString);
}
public void short2() {
Short s = 1;
consumesFunction2(s::compareTo);
}
public void short3() {
Short s = 1;
consumesFunction0(s::notify);
}
public void int1() {
Integer s = 1;
consumesFunction0(s::toString);
}
public void int2() {
Integer s = 1;
consumesFunction2(s::compareTo);
}
public void int3() {
Integer s = 1;
consumesFunction0(s::notify);
}
public void long1() {
Long s = 1L;
consumesFunction0(s::toString);
}
public void long2() {
Long s = 1L;
consumesFunction2(s::compareTo);
}
public void long3() {
Long s = 1L;
consumesFunction0(s::notify);
}
public void float1() {
Float s = 1f;
consumesFunction0(s::toString);
}
public void float2() {
Float s = 1f;
consumesFunction2(s::compareTo);
}
public void float3() {
Float s = 1f;
consumesFunction0(s::notify);
}
public void double1() {
Double s = 1d;
consumesFunction0(s::toString);
}
public void double2() {
Double s = 1d;
consumesFunction2(s::compareTo);
}
public void double3() {
Double s = 1d;
consumesFunction0(s::notify);
}
public void bool1() {
Boolean s = true;
consumesFunction0(s::toString);
}
public void bool2() {
Boolean s = true;
consumesFunction2(s::compareTo);
}
public void bool3() {
Boolean s = true;
consumesFunction0(s::notify);
}
public void stringArray1() {
String[] array = new String[]{""};
consumesFunction0(array::toString);
}
public void stringArray2() {
String[] array = new String[]{""};
consumesFunction2(array::equals);
}
public void stringArray3() {
String[] array = new String[]{""};
consumesFunction0(array::notify);
}
public void intArray1() {
int[] array = new int[]{1};
consumesFunction0(array::toString);
}
public void intArray2() {
int[] array = new int[]{1};
consumesFunction2(array::equals);
}
public void intArray3() {
int[] array = new int[]{1};
consumesFunction0(array::notify);
}
public void mul() {
String s = "";
consumesTwoFunction2(s::contains, s::contains);
}
public void externalClass() {
LambdaDemo lambdaDemo = new LambdaDemo();
consumesFunction0(lambdaDemo::methodReference0);
}
public void interClass() {
A a = new A();
consumesFunction2(a::m1);
consumesFunction2(a::m2);
}
public void function3() {
ExternalLambdaDemo externalLambdaDemo = new ExternalLambdaDemo();
consumesFunction3(externalLambdaDemo::f3);
}
public Boolean f3(String s1, Long l) {
return false;
}
private void consumesFunction0(Runnable f) {
f.run();
}
private <T> void consumesFunction1(Consumer<T> f) {
f.accept(null);
}
private <T, R> void consumesFunction2(Function<T, R> f) {
f.apply(null);
}
private <T1, T2, R> void consumesFunction3(BiFunction<T1, T2, R> f) {
f.apply(null, null);
}
private <T> void consumesSupplier(Supplier<T> supplier) {
supplier.get();
}
private <T, R> void consumesTwoFunction2(Function<T, R> f1, Function<T, R> f2) {
f1.apply(null);
f2.apply(null);
}
public static class A {
public String m1(int i) {
return "";
}
public String m2(Integer i) {
return "";
}
}
}

View File

@@ -0,0 +1,9 @@
package com.alibaba.demo.lambda;
/**
* @author jim
*/
@FunctionalInterface
public interface Function1Throwable<T, R> {
R apply(T t) throws Throwable;
}

View File

@@ -0,0 +1,218 @@
package com.alibaba.demo.lambda;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* @author jim
*/
@SuppressWarnings("unused")
public class LambdaDemo {
public void methodReference() {
consumesRun(this::run);
}
private void consumesRun(Runnable r) {
r.run();
}
private void run() {
blackHole();
}
public String methodReference0() {
return consumes0(this::function0);
}
private String consumes0(Supplier<String> function0) {
return function0.get();
}
private String function0() {
return "Hello";
}
public String methodReference1() {
return consumes1(this::function1);
}
private String consumes1(Function<Integer, String> function) {
return function.apply(1);
}
private String function1(Integer i) {
return String.valueOf(i);
}
public String methodReferenceThrows() {
return consumesThrows(this::function1Throwable);
}
private String consumesThrows(Function1Throwable<Integer, String> function) {
try {
return function.apply(1);
}catch (Throwable e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("RedundantThrows")
private String function1Throwable(Integer i) throws Throwable{
return String.valueOf(i);
}
public String methodReference2() {
return consumes2(this::function2);
}
private String consumes2(BiFunction<Integer, Double, String> function) {
return function.apply(1, .2);
}
private String function2(Integer i, Double d) {
return i + String.valueOf(d);
}
public String staticMethodReference1() {
return consumes1(StaticMethod::function1);
}
public String staticMethodReference2() {
return consumes2(StaticMethod::function2);
}
public void lambdaRun() {
consumes(() -> System.out.println("lambdaRun"));
}
private void consumes(Runnable o) {
o.run();
}
public void methodReferenceNew() {
Object o = consumes(Object::new);
blackHole(o);
}
private <T> T consumes(Supplier<T> s) {
return s.get();
}
private void blackHole(Object... ignore) {}
public void array() {
Function<Boolean[], Boolean[]> arrayBooleanFunction = this::arrayBooleanFunction;
Function<boolean[], boolean[]> arrayBooleanFunction1 = this::arrayBoolFunction;
Function<Byte[], Byte[]> byteFunction = this::arrayByteFunction;
Function<byte[], byte[]> byteFunction1 = this::arrayByteFunction;
Function<Character[], Character[]> charFunction = this::arrayCharFunction;
Function<char[], char[]> charFunction1 = this::arrayCharFunction;
Function<Short[], Short[]> shortFunction = this::arrayShortFunction;
Function<short[], short[]> shortFunction1 = this::arrayShortFunction;
Function<int[], int[]> intFunction = this::arrayIntFunction;
Function<Integer[], Integer[]> intFunction1 = this::arrayIntegerFunction;
Function<long[], long[]> longFunction = this::arrayLongFunction;
Function<Long[], Long[]> longFunction1 = this::arrayLongFunction;
Function<Float[], Float[]> floatFunction = this::arrayFloatFunction;
Function<float[], float[]> floatFunction1 = this::arrayFloatFunction;
Function<Double[], Double[]> doubleFunction = this::arrayDoubleFunction;
Function<double[], double[]> doubleFunction1 = this::arrayDoubleFunction;
blackHole(arrayBooleanFunction, arrayBooleanFunction1,
byteFunction, byteFunction1, charFunction, charFunction1, shortFunction, shortFunction1,
intFunction, intFunction1, longFunction, longFunction1, floatFunction, floatFunction1, doubleFunction,
doubleFunction1
);
}
private int[] arrayIntFunction(int[] arg) {
return arg;
}
private Integer[] arrayIntegerFunction(Integer[] arg) {
return arg;
}
private boolean[] arrayBoolFunction(boolean[] arg) {
return arg;
}
private Boolean[] arrayBooleanFunction(Boolean[] arg) {
return arg;
}
private byte[] arrayByteFunction(byte[] arg) {
return arg;
}
private Byte[] arrayByteFunction(Byte[] arg) {
return arg;
}
private char[] arrayCharFunction(char[] arg) {
return arg;
}
private Character[] arrayCharFunction(Character[] arg) {
return arg;
}
private short[] arrayShortFunction(short[] arg) {
return arg;
}
private Short[] arrayShortFunction(Short[] arg) {
return arg;
}
private long[] arrayLongFunction(long[] arg) {
return arg;
}
private Long[] arrayLongFunction(Long[] arg) {
return arg;
}
private float[] arrayFloatFunction(float[] arg) {
return arg;
}
private Float[] arrayFloatFunction(Float[] arg) {
return arg;
}
private double[] arrayDoubleFunction(double[] arg) {
return arg;
}
private Double[] arrayDoubleFunction(Double[] arg) {
return arg;
}
public void generic() {
Function<?, ?> genericFunction = this::genericFunction;
blackHole(genericFunction);
}
public <T, R> R genericFunction(T arg) {
//noinspection unchecked
return (R)arg;
}
private void collects() {
long l = Stream.of("1", "2", "3")
.filter(v -> !"2".equals(v))
.map(Long::parseLong)
.peek(this::blackHole)
.map(v -> new ArrayList<Long>(){{add(v);}})
.flatMap(Collection::stream)
.mapToLong(Long::valueOf)
.sum();
blackHole(l);
}
}

View File

@@ -0,0 +1,15 @@
package com.alibaba.demo.lambda;
/**
* @author jim
*/
public class StaticMethod {
public static String function1(Integer i) {
return "static" + i;
}
public static String function2(Integer i, Double d) {
return "static" + i + d;
}
}

View File

@@ -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";
}

View File

@@ -3,10 +3,10 @@ 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.InvokeVerifier.verify;
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
@@ -18,77 +18,88 @@ 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";
}
@MockInvoke(targetMethod = "getColorIndex")
private String get_colorIdx_from_color(Color self) {
return "colorIdx_from_color";
}
}
@Test
void should_mock_call_sub_object_method_by_parent_object() {
BlackBox box = (BlackBox)demoInherit.putIntoBox();
verify("put_into_box").withTimes(1);
verifyInvoked("put_into_box").withTimes(1);
assertEquals("put_data_into_box", box.get());
}
@Test
void should_mock_call_sub_object_method_by_sub_object() {
BlackBox box = demoInherit.putIntoBlackBox();
verify("put_into_blackbox").withTimes(1);
verifyInvoked("put_into_blackbox").withTimes(1);
assertEquals("put_data_into_blackbox", box.get());
}
@Test
void should_mock_call_parent_object_method_by_parent_object() {
String content = demoInherit.getFromBox();
verify("get_from_box").withTimes(1);
verifyInvoked("get_from_box").withTimes(1);
assertEquals("get_from_box", content);
}
@Test
void should_mock_call_parent_object_method_by_sub_object() {
String content = demoInherit.getFromBlackBox();
verify("get_from_blackbox").withTimes(1);
verifyInvoked("get_from_blackbox").withTimes(1);
assertEquals("get_from_blackbox", content);
}
@Test
void should_mock_call_interface_method_by_interface_object() {
String color = demoInherit.getColorViaColor();
verify("get_color_from_color").withTimes(1);
verifyInvoked("get_color_from_color").withTimes(1);
assertEquals("color_from_color", color);
}
@Test
void should_mock_call_interface_method_by_sub_class_object() {
String color = demoInherit.getColorViaBox();
verify("get_color_from_blackbox").withTimes(1);
verifyInvoked("get_color_from_blackbox").withTimes(1);
assertEquals("color_from_blackbox", color);
}
@Test
void should_mock_call_interface_method_by_sub_interface_object() {
String colorIdx = demoInherit.getColorIdxViaColor();
verifyInvoked("get_colorIdx_from_color").withTimes(1);
assertEquals("colorIdx_from_color", colorIdx);
}
}

View File

@@ -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";
}

View File

@@ -1,12 +1,12 @@
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;
import static com.alibaba.testable.core.matcher.InvokeMatcher.*;
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
import static com.alibaba.testable.core.matcher.InvocationMatcher.*;
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
import static org.junit.jupiter.api.Assertions.fail;
/**
@@ -18,62 +18,62 @@ 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) {}
}
@Test
void should_match_no_argument() {
demoMatcher.callMethodWithoutArgument();
verify("methodWithoutArgument").withTimes(1);
verifyInvoked("methodWithoutArgument").withTimes(1);
demoMatcher.callMethodWithoutArgument();
verify("methodWithoutArgument").withTimes(2);
verifyInvoked("methodWithoutArgument").withTimes(2);
}
@Test
void should_match_number_arguments() {
demoMatcher.callMethodWithNumberArguments();
verify("methodWithArguments").without(anyString(), 2);
verify("methodWithArguments").withInOrder(anyInt(), 2);
verify("methodWithArguments").withInOrder(anyLong(), anyNumber());
verify("methodWithArguments").with(1.0, anyMapOf(Integer.class, Float.class));
verify("methodWithArguments").with(anyList(), anySetOf(Float.class));
verify("methodWithArguments").with(anyList(), anyListOf(Float.class));
verify("methodWithArrayArgument").with(anyArrayOf(Long.class));
verify("methodWithArrayArgument").with(anyArray());
verifyInvoked("methodWithArguments").without(anyString(), 2);
verifyInvoked("methodWithArguments").withInOrder(anyInt(), 2);
verifyInvoked("methodWithArguments").withInOrder(anyLong(), anyNumber());
verifyInvoked("methodWithArguments").with(1.0, anyMapOf(Integer.class, Float.class));
verifyInvoked("methodWithArguments").with(anyList(), anySetOf(Float.class));
verifyInvoked("methodWithArguments").with(anyList(), anyListOf(Float.class));
verifyInvoked("methodWithArrayArgument").with(anyArrayOf(Long.class));
verifyInvoked("methodWithArrayArgument").with(anyArray());
}
@Test
void should_match_string_arguments() {
demoMatcher.callMethodWithStringArgument();
verify("methodWithArguments").with(startsWith("he"), endsWith("ld"));
verify("methodWithArguments").with(contains("stab"), matches("m.[cd]k"));
verify("methodWithArrayArgument").with(anyArrayOf(String.class));
verifyInvoked("methodWithArguments").with(startsWith("he"), endsWith("ld"));
verifyInvoked("methodWithArguments").with(contains("stab"), matches("m.[cd]k"));
verifyInvoked("methodWithArrayArgument").with(anyArrayOf(String.class));
}
@Test
void should_match_object_arguments() {
demoMatcher.callMethodWithObjectArgument();
verify("methodWithArguments").withInOrder(any(BlackBox.class), any(BlackBox.class));
verify("methodWithArguments").withInOrder(nullable(BlackBox.class), nullable(BlackBox.class));
verify("methodWithArguments").withInOrder(isNull(), notNull());
verifyInvoked("methodWithArguments").withInOrder(any(BlackBox.class), any(BlackBox.class));
verifyInvoked("methodWithArguments").withInOrder(nullable(BlackBox.class), nullable(BlackBox.class));
verifyInvoked("methodWithArguments").withInOrder(isNull(), notNull());
}
@Test
void should_match_with_times() {
demoMatcher.callMethodWithNumberArguments();
verify("methodWithArguments").with(anyNumber(), any()).times(3);
verifyInvoked("methodWithArguments").with(anyNumber(), any()).times(3);
demoMatcher.callMethodWithNumberArguments();
boolean gotError = false;
try {
verify("methodWithArguments").with(anyNumber(), any()).times(4);
verifyInvoked("methodWithArguments").with(anyNumber(), any()).times(4);
} catch (VerifyFailedError e) {
gotError = true;
}

View File

@@ -1,13 +1,13 @@
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;
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
import static com.alibaba.testable.core.tool.TestableTool.MOCK_CONTEXT;
import static com.alibaba.testable.core.tool.TestableTool.SOURCE_METHOD;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -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";
@@ -73,28 +73,28 @@ class DemoMockTest {
@Test
void should_mock_new_object() {
assertEquals("mock_something", demoMock.newFunc());
verify("createBlackBox").with("something");
verifyInvoked("createBlackBox").with("something");
}
@Test
void should_mock_member_method() throws Exception {
assertEquals("{ \"res\": \"mock_hello_MOCK_TAIL\"}", demoMock.outerFunc("hello"));
verify("innerFunc").with("hello");
verify("staticFunc").with();
verifyInvoked("innerFunc").with("hello");
verifyInvoked("staticFunc").with();
}
@Test
void should_mock_common_method() {
assertEquals("trim_string__sub_string__false", demoMock.commonFunc());
verify("trim").withTimes(1);
verify("sub").withTimes(1);
verify("startsWith").withTimes(1);
verifyInvoked("trim").withTimes(1);
verifyInvoked("sub").withTimes(1);
verifyInvoked("startsWith").withTimes(1);
}
@Test
void should_mock_static_method() {
assertEquals("not_secret_box", demoMock.getBox().get());
verify("secretBox").withTimes(1);
verifyInvoked("secretBox").withTimes(1);
}
@Test
@@ -104,7 +104,7 @@ class DemoMockTest {
// asynchronous
assertEquals("mock_one_mock_others",
Executors.newSingleThreadExecutor().submit(() -> demoMock.callerOne() + "_" + demoMock.callerTwo()).get());
verify("callFromDifferentMethod").withTimes(4);
verifyInvoked("callFromDifferentMethod").withTimes(4);
}
@Test
@@ -114,7 +114,7 @@ class DemoMockTest {
assertEquals("mock_special", demoMock.callerOne());
// asynchronous
assertEquals("mock_special", Executors.newSingleThreadExecutor().submit(() -> demoMock.callerOne()).get());
verify("callFromDifferentMethod").withTimes(2);
verifyInvoked("callFromDifferentMethod").withTimes(2);
}
}

View File

@@ -52,7 +52,13 @@ class DemoPrivateAccessorTest {
void should_use_null_parameter() {
set(demoPrivateAccess, "pi", null);
assertNull(get(demoPrivateAccess, "pi"));
assertEquals("null + 1", invokeStatic(DemoPrivateAccess.class, "privateStaticFuncWithArgs", null, 1));
List<String> list = new ArrayList<String>() {{ add("a"); add("b"); add("c"); }};
String value = invoke(demoPrivateAccess, "privateFuncWithArgs", list, null, 0);
assertEquals("abc + null + 0", value);
value = invokeStatic(DemoPrivateAccess.class, "privateStaticFuncWithArgs", null, 1);
assertEquals("null + 1", value);
}
}

View File

@@ -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;

View File

@@ -0,0 +1,80 @@
package com.alibaba.demo.lambda;
import com.alibaba.testable.core.annotation.MockInvoke;
import org.junit.jupiter.api.Test;
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
/**
* @author zcbbpo
*/
public class ExternalLambdaDemoTest {
private final ExternalLambdaDemo lambdaDemo = new ExternalLambdaDemo();
@SuppressWarnings("unused")
public static class Mock {
@MockInvoke(targetClass = String.class, targetMethod = "contains")
public boolean mockContains(CharSequence s) {
return false;
}
@MockInvoke(targetClass = Byte.class, targetMethod = "floatValue")
public float mockFloatValue() {
return 0.1f;
}
@MockInvoke(targetClass = Double.class, targetMethod = "compareTo")
public int mockCompareTo(Double anotherDouble) {
return 1;
}
@MockInvoke(targetClass = LambdaDemo.class, targetMethod = "methodReference0")
public String mockMethodReference0() {
return "";
}
@MockInvoke(targetClass = ExternalLambdaDemo.class, targetMethod = "f3")
public Boolean mockF3(String s1, Long l) {
return true;
}
}
@Test
public void shouldMockString1() {
lambdaDemo.string1();
verifyInvoked("mockContains").withTimes(1);
}
@Test
public void shouldMockByte1() {
lambdaDemo.byte1();
verifyInvoked("mockFloatValue").withTimes(1);
}
@Test
public void shouldMockDouble2() {
lambdaDemo.double2();
verifyInvoked("mockCompareTo").withTimes(1);
}
@Test
public void testMul() {
lambdaDemo.mul();
verifyInvoked("mockContains").withTimes(2);
}
@Test
public void testExternalClass() {
lambdaDemo.externalClass();
verifyInvoked("mockMethodReference0").withTimes(1);
}
@Test
public void testFunction3() {
lambdaDemo.function3();
verifyInvoked("mockF3").withTimes(1);
}
}

View File

@@ -0,0 +1,86 @@
package com.alibaba.demo.lambda;
import com.alibaba.testable.core.annotation.MockInvoke;
import org.junit.jupiter.api.Test;
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author zcbbpo
*/
public class LambdaDemoTest {
private final LambdaDemo lambdaDemo = new LambdaDemo();
@SuppressWarnings("unused")
public static class Mock {
@MockInvoke(targetClass = LambdaDemo.class, targetMethod = "run")
private void mockRun() {
}
@MockInvoke(targetClass = LambdaDemo.class)
private String function0() {
return "mock_function0";
}
@MockInvoke(targetClass = LambdaDemo.class)
private String function1(Integer i) {
return "mock_function1";
}
@MockInvoke(targetClass = LambdaDemo.class)
private String function2(Integer i, Double d) {
return "mock_function2";
}
@SuppressWarnings("RedundantThrows")
@MockInvoke(targetClass = LambdaDemo.class)
private String function1Throwable(Integer i) throws Throwable{
return "mock_function1Throwable";
}
@MockInvoke(targetClass = StaticMethod.class, targetMethod = "function1")
public static String staticFunction1(Integer i) {
return "mock_staticFunction1";
}
}
@Test
public void shouldMockRun() {
lambdaDemo.methodReference();
verifyInvoked("mockRun").withTimes(1);
}
@Test
public void shouldMockFunction0() {
String s = lambdaDemo.methodReference0();
assertEquals(s, "mock_function0");
}
@Test
public void shouldMockFunction1() {
String s = lambdaDemo.methodReference1();
assertEquals(s, "mock_function1");
}
@Test
public void shouldMockFunction2() {
String s = lambdaDemo.methodReference2();
assertEquals(s, "mock_function2");
}
@Test
public void shouldMockFunction1Throws() {
String s = lambdaDemo.methodReferenceThrows();
assertEquals(s, "mock_function1Throwable");
}
@Test
public void shouldMockStaticFunction1() {
String s = lambdaDemo.staticMethodReference1();
assertEquals(s, "mock_staticFunction1");
}
}

View File

@@ -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";
}

View File

@@ -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";
}

View File

@@ -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";
}

View File

@@ -4,7 +4,7 @@ import com.alibaba.testable.core.annotation.MockWith;
import org.junit.jupiter.api.Test;
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
import static org.junit.jupiter.api.Assertions.assertEquals;
@MockWith
@@ -19,9 +19,9 @@ public class OneToMultiSvcTest {
assertEquals("a_mock", aSvc.demo("test"));
assertEquals("b_mock", bSvc.demo("test"));
assertEquals("c_mock", cSvc.demo("test"));
verify("a_format").withTimes(1);
verify("b_format").withTimes(1);
verify("c_format").withTimes(1);
verifyInvoked("a_format").withTimes(1);
verifyInvoked("b_format").withTimes(1);
verifyInvoked("c_format").withTimes(1);
}
}

View File

@@ -17,8 +17,8 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.junit.jupiter:junit-jupiter:5.6.2")
testImplementation("com.alibaba.testable:testable-all:0.6.8")
testAnnotationProcessor("com.alibaba.testable:testable-processor:0.6.8")
testImplementation("com.alibaba.testable:testable-all:0.6.10")
testAnnotationProcessor("com.alibaba.testable:testable-processor:0.6.10")
}
tasks.withType<KotlinCompile> {

View File

@@ -14,7 +14,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>5.6.2</junit.version>
<testable.version>0.6.8</testable.version>
<testable.version>0.6.10</testable.version>
</properties>
<dependencies>

View File

@@ -11,7 +11,7 @@ import com.alibaba.demo.basic.model.mock.Color
class DemoInherit {
/**
* call method overridden by sub class via parent class variable
* call method overridden by subclass via parent class variable
*/
fun putIntoBox(): Box {
val box: Box = BlackBox("")
@@ -20,7 +20,7 @@ class DemoInherit {
}
/**
* call method overridden by sub class via sub class variable
* call method overridden by subclass via subclass variable
*/
fun putIntoBlackBox(): BlackBox {
val box = BlackBox("")
@@ -38,7 +38,7 @@ class DemoInherit {
}
/**
* call method defined in parent class via sub class variable
* call method defined in parent class via subclass variable
*/
val fromBlackBox: String?
get() {
@@ -56,7 +56,7 @@ class DemoInherit {
}
/**
* call method defined in interface via sub class variable
* call method defined in interface via subclass variable
*/
val colorViaBox: String
get() {

View File

@@ -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"
}

View File

@@ -1,7 +1,7 @@
package com.alibaba.demo.basic
import com.alibaba.testable.core.annotation.MockMethod
import com.alibaba.testable.core.matcher.InvokeVerifier
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
import com.alibaba.demo.basic.model.mock.Color
@@ -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"
}
@@ -51,42 +51,42 @@ internal class DemoInheritTest {
@Test
fun should_mock_call_sub_object_method_by_parent_object() {
val box = demoInherit.putIntoBox() as BlackBox
InvokeVerifier.verify("put_into_box").withTimes(1)
InvocationVerifier.verifyInvoked("put_into_box").withTimes(1)
assertEquals("put_data_into_box", box.get())
}
@Test
fun should_mock_call_sub_object_method_by_sub_object() {
val box = demoInherit.putIntoBlackBox()
InvokeVerifier.verify("put_into_blackbox").withTimes(1)
InvocationVerifier.verifyInvoked("put_into_blackbox").withTimes(1)
assertEquals("put_data_into_blackbox", box.get())
}
@Test
fun should_mock_call_parent_object_method_by_parent_object() {
val content = demoInherit.fromBox
InvokeVerifier.verify("get_from_box").withTimes(1)
InvocationVerifier.verifyInvoked("get_from_box").withTimes(1)
assertEquals("get_from_box", content)
}
@Test
fun should_mock_call_parent_object_method_by_sub_object() {
val content = demoInherit.fromBlackBox
InvokeVerifier.verify("get_from_blackbox").withTimes(1)
InvocationVerifier.verifyInvoked("get_from_blackbox").withTimes(1)
assertEquals("get_from_blackbox", content)
}
@Test
fun should_mock_call_interface_method_by_interface_object() {
val color = demoInherit.colorViaColor
InvokeVerifier.verify("get_color_from_color").withTimes(1)
InvocationVerifier.verifyInvoked("get_color_from_color").withTimes(1)
assertEquals("color_from_color", color)
}
@Test
fun should_mock_call_interface_method_by_sub_class_object() {
val color = demoInherit.colorViaBox
InvokeVerifier.verify("get_color_from_blackbox").withTimes(1)
InvocationVerifier.verifyInvoked("get_color_from_blackbox").withTimes(1)
assertEquals("color_from_blackbox", color)
}
}

View File

@@ -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"
}

View File

@@ -1,9 +1,9 @@
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.InvokeMatcher
import com.alibaba.testable.core.matcher.InvokeVerifier
import com.alibaba.testable.core.matcher.InvocationMatcher
import com.alibaba.testable.core.matcher.InvocationVerifier
import com.alibaba.demo.basic.model.mock.BlackBox
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
@@ -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>) {
}
}
@@ -33,50 +33,50 @@ internal class DemoMatcherTest {
@Test
fun should_match_no_argument() {
demoMatcher.callMethodWithoutArgument()
InvokeVerifier.verify("methodWithoutArgument").withTimes(1)
InvocationVerifier.verifyInvoked("methodWithoutArgument").withTimes(1)
demoMatcher.callMethodWithoutArgument()
InvokeVerifier.verify("methodWithoutArgument").withTimes(2)
InvocationVerifier.verifyInvoked("methodWithoutArgument").withTimes(2)
}
@Test
fun should_match_number_arguments() {
demoMatcher.callMethodWithNumberArguments()
InvokeVerifier.verify("methodWithArguments").without(InvokeMatcher.anyString(), 2)
InvokeVerifier.verify("methodWithArguments").withInOrder(InvokeMatcher.anyInt(), 2)
InvokeVerifier.verify("methodWithArguments").withInOrder(InvokeMatcher.anyLong(), InvokeMatcher.anyNumber())
InvocationVerifier.verifyInvoked("methodWithArguments").without(InvocationMatcher.anyString(), 2)
InvocationVerifier.verifyInvoked("methodWithArguments").withInOrder(InvocationMatcher.anyInt(), 2)
InvocationVerifier.verifyInvoked("methodWithArguments").withInOrder(InvocationMatcher.anyLong(), InvocationMatcher.anyNumber())
// Note: Must use `::class.javaObjectType` for primary types check in Kotlin
InvokeVerifier.verify("methodWithArguments").with(1.0, InvokeMatcher.anyMapOf(Int::class.javaObjectType, Float::class.javaObjectType)).times(2)
InvokeVerifier.verify("methodWithArguments").with(InvokeMatcher.anyList(), InvokeMatcher.anySetOf(Float::class.javaObjectType)).times(2)
InvokeVerifier.verify("methodWithArguments").with(InvokeMatcher.anyList(), InvokeMatcher.anyListOf(Float::class.javaObjectType))
InvokeVerifier.verify("methodWithArrayArgument").with(InvokeMatcher.anyArrayOf(Long::class.javaObjectType))
InvokeVerifier.verify("methodWithArrayArgument").with(InvokeMatcher.anyArray())
InvocationVerifier.verifyInvoked("methodWithArguments").with(1.0, InvocationMatcher.anyMapOf(Int::class.javaObjectType, Float::class.javaObjectType)).times(2)
InvocationVerifier.verifyInvoked("methodWithArguments").with(InvocationMatcher.anyList(), InvocationMatcher.anySetOf(Float::class.javaObjectType)).times(2)
InvocationVerifier.verifyInvoked("methodWithArguments").with(InvocationMatcher.anyList(), InvocationMatcher.anyListOf(Float::class.javaObjectType))
InvocationVerifier.verifyInvoked("methodWithArrayArgument").with(InvocationMatcher.anyArrayOf(Long::class.javaObjectType))
InvocationVerifier.verifyInvoked("methodWithArrayArgument").with(InvocationMatcher.anyArray())
}
@Test
fun should_match_string_arguments() {
demoMatcher.callMethodWithStringArgument()
InvokeVerifier.verify("methodWithArguments").with(InvokeMatcher.startsWith("he"), InvokeMatcher.endsWith("ld"))
InvokeVerifier.verify("methodWithArguments").with(InvokeMatcher.contains("stab"), InvokeMatcher.matches("m.[cd]k"))
InvokeVerifier.verify("methodWithArrayArgument").with(InvokeMatcher.anyArrayOf(String::class.java))
InvocationVerifier.verifyInvoked("methodWithArguments").with(InvocationMatcher.startsWith("he"), InvocationMatcher.endsWith("ld"))
InvocationVerifier.verifyInvoked("methodWithArguments").with(InvocationMatcher.contains("stab"), InvocationMatcher.matches("m.[cd]k"))
InvocationVerifier.verifyInvoked("methodWithArrayArgument").with(InvocationMatcher.anyArrayOf(String::class.java))
}
@Test
fun should_match_object_arguments() {
demoMatcher.callMethodWithObjectArgument()
InvokeVerifier.verify("methodWithArguments").withInOrder(InvokeMatcher.any(BlackBox::class.java), InvokeMatcher.any(BlackBox::class.java))
InvokeVerifier.verify("methodWithArguments").withInOrder(InvokeMatcher.nullable(BlackBox::class.java), InvokeMatcher.nullable(BlackBox::class.java))
InvokeVerifier.verify("methodWithArguments").withInOrder(InvokeMatcher.isNull(), InvokeMatcher.notNull())
InvocationVerifier.verifyInvoked("methodWithArguments").withInOrder(InvocationMatcher.any(BlackBox::class.java), InvocationMatcher.any(BlackBox::class.java))
InvocationVerifier.verifyInvoked("methodWithArguments").withInOrder(InvocationMatcher.nullable(BlackBox::class.java), InvocationMatcher.nullable(BlackBox::class.java))
InvocationVerifier.verifyInvoked("methodWithArguments").withInOrder(InvocationMatcher.isNull(), InvocationMatcher.notNull())
}
@Test
fun should_match_with_times() {
demoMatcher.callMethodWithNumberArguments()
InvokeVerifier.verify("methodWithArguments").with(InvokeMatcher.anyNumber(), InvokeMatcher.any()).times(4)
InvocationVerifier.verifyInvoked("methodWithArguments").with(InvocationMatcher.anyNumber(), InvocationMatcher.any()).times(4)
demoMatcher.callMethodWithNumberArguments()
var gotError = false
try {
InvokeVerifier.verify("methodWithArguments").with(InvokeMatcher.anyNumber(), InvokeMatcher.any()).times(5)
InvocationVerifier.verifyInvoked("methodWithArguments").with(InvocationMatcher.anyNumber(), InvocationMatcher.any()).times(5)
} catch (e: VerifyFailedError) {
gotError = true
}

View File

@@ -1,8 +1,8 @@
package com.alibaba.demo.basic
import com.alibaba.testable.core.annotation.MockConstructor
import com.alibaba.testable.core.annotation.MockMethod
import com.alibaba.testable.core.matcher.InvokeVerifier.verify
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
import com.alibaba.demo.basic.model.mock.BlackBox
@@ -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"
@@ -66,35 +66,35 @@ internal class DemoMockTest {
@Test
fun should_mock_new_object() {
assertEquals("mock_something", demoMock.newFunc())
verify("createBlackBox").with("something")
verifyInvoked("createBlackBox").with("something")
}
@Test
fun should_mock_member_method() {
assertEquals("{ \"res\": \"mock_hello_MOCK_TAIL\"}", demoMock.outerFunc("hello"))
verify("innerFunc").with("hello")
verify("staticFunc").with()
verifyInvoked("innerFunc").with("hello")
verifyInvoked("staticFunc").with()
}
// @Test
// fun should_mock_method_in_companion_object() {
// assertEquals("CALL_MOCK_TAIL", DemoMock.callStaticFunc())
// verify("staticFunc").with()
// verifyInvoked("staticFunc").with()
// }
@Test
fun should_mock_common_method() {
assertEquals("trim_string__sub_string__false", demoMock.commonFunc())
verify("trim").withTimes(1)
verify("sub").withTimes(1)
verify("startsWith").withTimes(1)
verifyInvoked("trim").withTimes(1)
verifyInvoked("sub").withTimes(1)
verifyInvoked("startsWith").withTimes(1)
}
@Test
fun should_mock_static_method() {
assertEquals("White_not_secret_box", demoMock.getBox().get())
verify("secretBox").withTimes(1)
verify("createBox").withTimes(1)
verifyInvoked("secretBox").withTimes(1)
verifyInvoked("createBox").withTimes(1)
}
@Test
@@ -105,7 +105,7 @@ internal class DemoMockTest {
assertEquals("mock_one_mock_others", Executors.newSingleThreadExecutor().submit<String> {
demoMock.callerOne() + "_" + demoMock.callerTwo()
}.get())
verify("callFromDifferentMethod").withTimes(4)
verifyInvoked("callFromDifferentMethod").withTimes(4)
}
@Test
@@ -117,7 +117,7 @@ internal class DemoMockTest {
assertEquals("mock_special", Executors.newSingleThreadExecutor().submit<String> {
demoMock.callerOne()
}.get())
verify("callFromDifferentMethod").withTimes(2)
verifyInvoked("callFromDifferentMethod").withTimes(2)
MOCK_CONTEXT.clear()
}
}

View File

@@ -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

View File

@@ -1,14 +1,14 @@
package com.alibaba.demo.java2kotlin
import com.alibaba.testable.core.annotation.MockMethod
import com.alibaba.testable.core.matcher.InvokeVerifier.verify
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
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"))
@@ -43,8 +43,8 @@ class PathDemoTest {
@Test
fun should_mock_java_method_invoke_in_kotlin() {
PathDemo.deleteRecursively(File("/a/b/"))
verify("listFiles").withTimes(2)
verify("delete").withTimes(4)
verifyInvoked("listFiles").withTimes(2)
verifyInvoked("delete").withTimes(4)
}
}

View File

@@ -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"
}

View File

@@ -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"
}

View File

@@ -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"
}

View File

@@ -1,7 +1,7 @@
package com.alibaba.demo.one2multi
import com.alibaba.testable.core.annotation.MockWith
import com.alibaba.testable.core.matcher.InvokeVerifier.verify
import com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
@@ -17,9 +17,9 @@ class OneToMultiSvcTest {
assertEquals("a_mock", aSvc.demo("test"))
assertEquals("b_mock", bSvc.demo("test"))
assertEquals("c_mock", cSvc.demo("test"))
verify("a_format").withTimes(1)
verify("b_format").withTimes(1)
verify("c_format").withTimes(1)
verifyInvoked("a_format").withTimes(1)
verifyInvoked("b_format").withTimes(1)
verifyInvoked("c_format").withTimes(1)
}
}

View File

@@ -14,8 +14,8 @@ repositories {
dependencies {
testImplementation 'org.codehaus.groovy:groovy-all:3.0.7'
testImplementation 'org.spockframework:spock-core:2.0-M5-groovy-3.0'
testImplementation('com.alibaba.testable:testable-all:0.6.8')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.8')
testImplementation('com.alibaba.testable:testable-all:0.6.10')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.10')
}
tasks.withType(JavaCompile) {

View File

@@ -12,7 +12,7 @@
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<testable.version>0.6.8</testable.version>
<testable.version>0.6.10</testable.version>
</properties>
<dependencyManagement>

View File

@@ -1,12 +1,12 @@
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
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
class DemoSpockTest extends 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)
}
@@ -37,8 +37,8 @@ class DemoSpockTest extends Specification {
box.pop() == "mock 2"
box.pop() == "mock 1"
box.pop() == "mock zero"
verify("createBox").withTimes(1)
verify("putBox").withInOrder("1").withInOrder("2").withInOrder("3")
verifyInvoked("createBox").withTimes(1)
verifyInvoked("putBox").withInOrder("1").withInOrder("2").withInOrder("3")
}
}

View File

@@ -10,4 +10,4 @@ However, when the current mainstream mock framework implements the mock function
Therefore, we developed `TestableMock`, **a maverick and lightweight mock tool**.
![mock](https://testable-code.oss-cn-beijing.aliyuncs.com/en-us/mock-simpson.png)
![mock-simpson-en-us.png](https://img.alicdn.com/imgextra/i2/O1CN01CdAfqR1tP2iqFC14g_!!6000000005893-2-tps-500-761.png)

View File

@@ -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.

View File

@@ -61,4 +61,4 @@ This framework will run Android unit tests on a standard JVM virtual machine, wh
This problem is caused by the system `Class Path` content is too long, and has nothing to do with `TestableMock`. However, it should be noted that IntelliJ provides two auxiliary solutions: `JAR manifest` and `classpath file`. If `TestableMock` is used in the test, please select `JAR manifest`.
![jar-manifest](https://testable-code.oss-cn-beijing.aliyuncs.com/jar-manifest.png)
![jar-manifest.png](https://img.alicdn.com/imgextra/i2/O1CN01hfC5YE1Kw0gBIlB2x_!!6000000001227-2-tps-752-171.png)

View File

@@ -9,7 +9,7 @@ The **verifiers** and **matchers** are provided in `TestableMock` to achieve thi
@Test
public test_case() {
int res = insToTest.methodToTest();
verify("mockMethod").with(123, "abc");
verifyInvoked("mockMethod").with(123, "abc");
}
```

View File

@@ -1,5 +1,19 @@
# 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
## 0.6.9
- support mock invocation in lambda method
- fix a `NullPointerException` issue when `PrivateAccessor.invoke()` has `null` parameter
## 0.6.8
- support `@DumpTo` annotation to dump bytecode of any transformed class
- `PrivateAccessor.setStatic()` method now able to update static final members
@@ -58,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

View File

@@ -16,7 +16,7 @@ It is recommended to add a `property` field that identifies the TestableMock ver
```xml
<properties>
<testable.version>0.6.8</testable.version>
<testable.version>0.6.10</testable.version>
</properties>
```
@@ -63,8 +63,8 @@ Add dependence of `TestableMock` in `build.gradle` file:
```groovy
dependencies {
testImplementation('com.alibaba.testable:testable-all:0.6.8')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.8')
testImplementation('com.alibaba.testable:testable-all:0.6.10')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.10')
}
```

View File

@@ -70,7 +70,7 @@ class Demo {
To test this method, you can use `TestableMock` to quickly mock out the `System.out.println` method. In the mock method body, you can simply call the original method (equivalent to not affecting the original method function, only used for call recording), or leave it blank (equivalent to removing the side effects of the original method).
After executing the void type method under test, use `InvokeVerifier.verify()` to verify whether the incoming print content meets expectations:
After executing the void type method under test, use `InvocationVerifier.verifyInvoked()` to verify whether the incoming print content meets expectations:
```java
class DemoTest {
@@ -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);
@@ -90,7 +90,7 @@ class DemoTest {
Action action = new Action("click", ":download");
demo.recordAction();
// Verify mock method `println` is invoked, and passing parameters in line with expectations
verify("println").with(matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} \\[click\\] :download"));
verifyInvoked("println").with(matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} \\[click\\] :download"));
}
}
```

View File

@@ -39,7 +39,7 @@ public class DemoMockTest {
@Test
void should_mock_member_method() throws Exception {
assertEquals("hello_world", demoMock.outerFunc());
verify("innerFunc").with("world");
verifyInvoked("innerFunc").with("world");
}
}
```
@@ -59,7 +59,7 @@ public class DemoMockTest {
@Test
void should_mock_member_method() throws Exception {
assertEquals("hello_world", demoMock.outerFunc());
verify("innerFunc").with("world");
verifyInvoked("innerFunc").with("world");
}
}
```

View File

@@ -15,4 +15,4 @@ At the same time, because the built-in unit test executor of `Eclipse` completel
Take the use of `JUnit` as an example. You need to pull down from the small triangle next to the run button on the IDE toolbar, select "Run Configurations...", select the task to run the unit test on the left side, and switch to "arguments" Tab on the right side, append a `-javaagent:` parameter in the "VM Options", the following figure is an example, note that the `testable-agent` package should be modified to match the actual situation of the local Maven repository path.
![eclipse-junit-configuration](https://testable-code.oss-cn-beijing.aliyuncs.com/eclipse-junit-configuration.png)
![eclipse-junit-configuration.png](https://img.alicdn.com/imgextra/i3/O1CN01C7DwGs1dHgVRAhh3y_!!6000000003711-2-tps-1430-1004.png)

View File

@@ -29,13 +29,13 @@ This is because the IDE usually only runs the `maven-surefire-plugin` plugin whe
This problem can be bypassed by configuring the test parameters of the IDE additionally. Take IntelliJ as an example, open the "Edit Configuration..." option of the run menu, as shown in the position ①
![modify-run-configuration](https://testable-code.oss-cn-beijing.aliyuncs.com/modify-run-configuration.png)
![modify-run-configuration.png](https://img.alicdn.com/imgextra/i3/O1CN01HLlNyZ1gezVe4AOiE_!!6000000004168-2-tps-1036-184.png)
Add JavaAgent startup parameters at the end of the "virtual machine parameters" attribute value: `-javaagent:${HOME}/.m2/repository/com/alibaba/testable/testable-agent/xyz/testable-agent-xyzjar`, as shown in the figure position ②
> PS: Please replace `x.y.z` in the path with the actual version number
![add-testable-javaagent](https://testable-code.oss-cn-beijing.aliyuncs.com/add-testable-javaagent.png)
![add-testable-javaagent.png](https://img.alicdn.com/imgextra/i4/O1CN01pdxC8S1R2JpXX8aOJ_!!6000000002053-2-tps-2446-486.png)
Finally, click to run the unit test, as shown in the position ③

View File

@@ -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":
@@ -163,7 +163,7 @@ For complete code examples, see the `should_get_source_method_name()` and `shoul
### 3. Verify the sequence and parameters of the mock method being invoked
In test cases, you can use the `InvokeVerifier.verify()` method, and cooperate with `with()`, `withInOrder()`, `without()`, `withTimes()` and other methods to verify the mock call situation.
In test cases, you can use the `InvocationVerifier.verifyInvoked()` method, and cooperate with `with()`, `withInOrder()`, `without()`, `withTimes()` and other methods to verify the mock call situation.
For details, please refer to the [Check Mock Call](en-us/doc/matcher.md) document.

View File

@@ -9,4 +9,4 @@ TestableMock简介
于是,我们开发了`TestableMock`**一款特立独行的轻量Mock工具**。
![mock](https://testable-code.oss-cn-beijing.aliyuncs.com/mock-simpson.png)
![mock-simpson-zh-cn.png](https://img.alicdn.com/imgextra/i2/O1CN01uPzi441cxzTZzhUWT_!!6000000003668-2-tps-500-761.png)

View File

@@ -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构造方法。

View File

@@ -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)。

View File

@@ -63,4 +63,4 @@ Kotlin语言中的`String`类型实际上是`kotlin.String`,而非`java.lang.S
这个问题是由于系统ClassPath包含太多路径所致与是否使用`TestableMock`无关。但需要注意的是IntelliJ提供了两种辅助解决机制`JAR manifest``classpath file`,若测试中使用了`TestableMock`,请选择`JAR manifest`
![jar-manifest](https://testable-code.oss-cn-beijing.aliyuncs.com/jar-manifest.png)
![jar-manifest.png](https://img.alicdn.com/imgextra/i2/O1CN01hfC5YE1Kw0gBIlB2x_!!6000000001227-2-tps-752-171.png)

View File

@@ -9,7 +9,7 @@
@Test
public test_case() {
int res = insToTest.methodToTest();
verify("mockMethod").with(123, "abc");
verifyInvoked("mockMethod").with(123, "abc");
}
```

View File

@@ -59,4 +59,4 @@
若项目测试中既包含真实的单元测试又包含了使用单元测试框架编写的集成测试时。为了让集成测试的执行过程不受Mock影响可能需要使用`mock.scope.default`将默认的Mock方法范围限制为仅对所属类型的单元测试用例生效。
若需Mock的调用发生在线程池中且遇到`verify()`结果或`MOCK_CONTEXT`内容不正确的时候,则需考虑开启`thread.pool.enhance.enable`配置,详见[Mock线程池内的调用](zh-cn/doc/with-thread-pool.md)。
若需Mock的调用发生在线程池中且遇到`verifyInvoked()`结果或`MOCK_CONTEXT`内容不正确的时候,则需考虑开启`thread.pool.enhance.enable`配置,详见[Mock线程池内的调用](zh-cn/doc/with-thread-pool.md)。

View File

@@ -51,7 +51,7 @@ public class BbbServiceTest {
}
public class BasicMock {
@MockMethod(targetClass = UserDao.class)
@MockInvoke(targetClass = UserDao.class)
protected String getById(int id) {
...
}

View File

@@ -17,6 +17,9 @@
- `PrivateAccessor.invokeStatic(任意类型, "私有静态方法名", 调用参数...)` ➜ 调用任意类的**静态**私有方法
- `PrivateAccessor.construct(任意类型, 构造方法参数...)` ➜ 调用任意类的私有构造方法
> 特别说明:默认情况下,`setStatic()`方法不支持修改`static final`修饰的成员变量。在Java中此类变量通常代表业务意义上的恒定常量值不应当在单元测试中更改。
> 在特殊场景下,如确实需要修改`static final`成员,请开启配置项`private.access.enhance.enable = true`,详见[全局运行参数](zh-cn/doc/javaagent-args.md)文档。
详见`java-demo``kotlin-demo`示例项目`DemoPrivateAccessorTest`测试类中的用例。
### 1.2 防代码重构机制

View File

@@ -1,5 +1,19 @@
# 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
## 0.6.9
- 支持Mock匿名方法体内部的方法调用issue-36 / pr-208
- 修复`PrivateAccessor.invoke()`调用参数为`null`时的空指针异常issue-226
## 0.6.8
- 支持使用`@DumpTo`注解导出任意类处理后的字节码
- 支持使用`PrivateAccessor.setStatic()`方法修改静态常量成员

View File

@@ -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);
> }

View File

@@ -16,7 +16,7 @@
```xml
<properties>
<testable.version>0.6.8</testable.version>
<testable.version>0.6.10</testable.version>
</properties>
```
@@ -63,8 +63,8 @@
```groovy
dependencies {
testImplementation('com.alibaba.testable:testable-all:0.6.8')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.8')
testImplementation('com.alibaba.testable:testable-all:0.6.10')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.10')
}
```

View File

@@ -71,7 +71,7 @@ class Demo {
若要测试此方法,可以利用`TestableMock`快速Mock掉`System.out.println`方法。在Mock方法体里可以继续执行原调用相当于并不影响本来方法功能仅用于做调用记录也可以直接留空相当于去除了原方法的副作用
在执行完被测的void类型方法以后`InvokeVerifier.verify()`校验传入的打印内容是否符合预期:
在执行完被测的void类型方法以后`InvocationVerifier.verifyInvoked()`校验传入的打印内容是否符合预期:
```java
class DemoTest {
@@ -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);
@@ -91,7 +91,7 @@ class DemoTest {
Action action = new Action("click", ":download");
demo.recordAction();
// 验证Mock方法println被调用且传入参数格式符合预期
verify("println").with(matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} \\[click\\] :download"));
verifyInvoked("println").with(matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} \\[click\\] :download"));
}
}
```

View File

@@ -39,7 +39,7 @@ public class DemoMockTest {
@Test
void should_mock_member_method() throws Exception {
assertEquals("hello_world", demoMock.outerFunc());
verify("innerFunc").with("world");
verifyInvoked("innerFunc").with("world");
}
}
```
@@ -59,7 +59,7 @@ public class DemoMockTest {
@Test
void should_mock_member_method() throws Exception {
assertEquals("hello_world", demoMock.outerFunc());
verify("innerFunc").with("world");
verifyInvoked("innerFunc").with("world");
}
}
```

View File

@@ -15,4 +15,4 @@ IntelliJ IDE对`TestableMock`所用到的`JSR-269`注释处理器以及`maven-su
以使用`JUnit`为例方法为从IDE工具栏的运行按钮旁边的小三角处下拉选择"Run Configurations...",左侧选择要运行单元测试的任务,在右侧切换到"arguments"标签页,在"VM Options"里添加`-javaagent:`参数,下图为示例,注意应修改`testable-agent`包为与实际情况匹配的本地Maven仓库路径。
![eclipse-junit-configuration](https://testable-code.oss-cn-beijing.aliyuncs.com/eclipse-junit-configuration.png)
![eclipse-junit-configuration.png](https://img.alicdn.com/imgextra/i3/O1CN01C7DwGs1dHgVRAhh3y_!!6000000003711-2-tps-1430-1004.png)

View File

@@ -31,13 +31,13 @@
该问题可以通过额外配置IDE的测试参数绕过。以IntelliJ为例打开运行菜单的"编辑配置..."选型,如图中位置①
![modify-run-configuration](https://testable-code.oss-cn-beijing.aliyuncs.com/modify-run-configuration.png)
![modify-run-configuration.png](https://img.alicdn.com/imgextra/i3/O1CN01HLlNyZ1gezVe4AOiE_!!6000000004168-2-tps-1036-184.png)
在"虚拟机参数"属性值末尾添加JavaAgent启动参数`-javaagent:${HOME}/.m2/repository/com/alibaba/testable/testable-agent/x.y.z/testable-agent-x.y.z.jar`,如图中位置②
> PS请将路径中的`x.y.z`替换成实际使用的版本号
![add-testable-javaagent](https://testable-code.oss-cn-beijing.aliyuncs.com/add-testable-javaagent.png)
![add-testable-javaagent.png](https://img.alicdn.com/imgextra/i4/O1CN01pdxC8S1R2JpXX8aOJ_!!6000000002053-2-tps-2446-486.png)
最后点击运行单元测试,如图中位置③

View File

@@ -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":
@@ -163,7 +163,7 @@ private Data mockDemo() {
### 3. 验证Mock方法被调用的顺序和参数
在测试用例中可用通过`InvokeVerifier.verify()`方法,配合`with()``withInOrder()``without()``withTimes()`等方法实现对Mock调用情况的验证。
在测试用例中可用通过`InvocationVerifier.verifyInvoked()`方法,配合`with()``withInOrder()``without()``withTimes()`等方法实现对Mock调用情况的验证。
详见[校验Mock调用](zh-cn/doc/matcher.md)文档。

View File

@@ -3,7 +3,7 @@ Mock线程池内的调用
`TestableMock`采用来自[transmittable-thread-local](https://github.com/alibaba/transmittable-thread-local)项目的`TransmittableThreadLocal`类型存储测试用例运行期的`MOCK_CONTEXT`内容和Mock方法调用过程。
当线程池中的执行对象未经过`TtlRunnable``TtlCallable`处理时,`TransmittableThreadLocal`将自动降级为与`InheritableThreadLocal`等效的类型,即只对父子线程有效,无法在线程池上下文中正常传递存储数据。因而会导致`MOCK_CONTEXT`内容丢失和`verify()`方法校验结果不正确的情况。
当线程池中的执行对象未经过`TtlRunnable``TtlCallable`处理时,`TransmittableThreadLocal`将自动降级为与`InheritableThreadLocal`等效的类型,即只对父子线程有效,无法在线程池上下文中正常传递存储数据。因而会导致`MOCK_CONTEXT`内容丢失和`verifyInvoked()`方法校验结果不正确的情况。
为此,可以启用[Testable全局配置](zh-cn/doc/javaagent-args.md)`thread.pool.enhance.enable=true`,来自动在测试启动时自动封装程序中的普通`Runnable``Callable`对象,使`TransmittableThreadLocal`恢复跨线程池存储数据的能力。

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-parent</artifactId>
<version>0.6.8</version>
<version>0.6.10</version>
<relativePath>../testable-parent</relativePath>
</parent>
<artifactId>testable-agent</artifactId>

View File

@@ -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";

View File

@@ -19,7 +19,9 @@ abstract public class BaseClassHandler implements Opcodes {
ClassNode cn = new ClassNode();
cr.accept(cn, 0);
transform(cn);
ClassWriter cw = new ClassWriter( 0);
// flag 1 was auto compute max
ClassWriter cw = new ClassWriter( ClassWriter.COMPUTE_MAXS);
cn.accept(cw);
return cw.toByteArray();
}

View File

@@ -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)) {

View File

@@ -1,18 +1,25 @@
package com.alibaba.testable.agent.handler;
import com.alibaba.testable.agent.model.BasicType;
import com.alibaba.testable.agent.model.MethodInfo;
import com.alibaba.testable.agent.model.TravelStatus;
import com.alibaba.testable.agent.util.BytecodeUtil;
import com.alibaba.testable.agent.util.ClassUtil;
import com.alibaba.testable.agent.util.MethodUtil;
import com.alibaba.testable.core.util.LogUtil;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import static com.alibaba.testable.core.constant.ConstPool.CONSTRUCTOR;
@@ -21,6 +28,7 @@ import static com.alibaba.testable.core.constant.ConstPool.CONSTRUCTOR;
*/
public class SourceClassHandler extends BaseClassHandler {
private final AtomicInteger atomicInteger = new AtomicInteger();
private final String mockClassName;
private final List<MethodInfo> injectMethods;
private final Set<Integer> invokeOps = new HashSet<Integer>() {{
@@ -37,6 +45,7 @@ public class SourceClassHandler extends BaseClassHandler {
/**
* Handle bytecode of source class
*
* @param cn original class node
*/
@Override
@@ -51,13 +60,16 @@ public class SourceClassHandler extends BaseClassHandler {
memberInjectMethods.add(im);
}
}
resolveMethodReference(cn);
for (MethodNode m : cn.methods) {
transformMethod(m, memberInjectMethods, newOperatorInjectMethods);
transformMethod(m, memberInjectMethods, newOperatorInjectMethods, cn);
}
}
private void transformMethod(MethodNode mn, Set<MethodInfo> memberInjectMethods,
Set<MethodInfo> newOperatorInjectMethods) {
Set<MethodInfo> newOperatorInjectMethods, ClassNode cn) {
LogUtil.verbose(" Found method %s", mn.name);
if (mn.name.startsWith("$")) {
// skip methods e.g. "$jacocoInit"
@@ -71,11 +83,11 @@ public class SourceClassHandler extends BaseClassHandler {
int i = 0;
do {
if (invokeOps.contains(instructions[i].getOpcode())) {
MethodInsnNode node = (MethodInsnNode)instructions[i];
MethodInsnNode node = (MethodInsnNode) instructions[i];
if (CONSTRUCTOR.equals(node.name)) {
if (LogUtil.isVerboseEnabled()) {
LogUtil.verbose(" Line %d, constructing \"%s\"", getLineNum(instructions, i),
MethodUtil.toJavaMethodDesc(node.owner, node.desc));
MethodUtil.toJavaMethodDesc(node.owner, node.desc));
}
MethodInfo newOperatorInjectMethod = getNewOperatorInjectMethod(newOperatorInjectMethods, node);
if (newOperatorInjectMethod != null) {
@@ -92,7 +104,7 @@ public class SourceClassHandler extends BaseClassHandler {
} else {
if (LogUtil.isVerboseEnabled()) {
LogUtil.verbose(" Line %d, invoking \"%s\"", getLineNum(instructions, i),
MethodUtil.toJavaMethodDesc(node.owner, node.name, node.desc));
MethodUtil.toJavaMethodDesc(node.owner, node.name, node.desc));
}
MethodInfo mockMethod = getMemberInjectMethodName(memberInjectMethods, node);
if (mockMethod != null) {
@@ -103,7 +115,7 @@ public class SourceClassHandler extends BaseClassHandler {
handleFrameStackChange(mn, mockMethod, rangeStart, i);
}
instructions = replaceMemberCallOps(mn, mockMethod,
instructions, node.owner, node.getOpcode(), rangeStart, i);
instructions, node.owner, node.getOpcode(), rangeStart, i);
i = rangeStart;
} else {
LogUtil.warn("Potential missed mocking at %s:%s", mn.name, getLineNum(instructions, i));
@@ -111,21 +123,23 @@ public class SourceClassHandler extends BaseClassHandler {
}
}
}
i++;
} while (i < instructions.length);
}
/**
* find the mock method fit for specified method node
*
* @param memberInjectMethods mock methods available
* @param node method node to match for
* @param node method node to match for
* @return mock method info
*/
private MethodInfo getMemberInjectMethodName(Set<MethodInfo> memberInjectMethods, MethodInsnNode node) {
for (MethodInfo m : memberInjectMethods) {
String nodeOwner = ClassUtil.fitCompanionClassName(node.owner);
String nodeName = ClassUtil.fitKotlinAccessorName(node.name);
// Kotlin accessor method will append a extra type parameter
// Kotlin accessor method will append an extra type parameter
String nodeDesc = nodeName.equals(node.name) ? node.desc : MethodUtil.removeFirstParameter(node.desc);
if (m.getClazz().equals(nodeOwner) && m.getName().equals(nodeName) && m.getDesc().equals(nodeDesc)) {
return m;
@@ -145,12 +159,12 @@ public class SourceClassHandler extends BaseClassHandler {
private String getConstructorInjectDesc(MethodInsnNode constructorNode) {
return constructorNode.desc.substring(0, constructorNode.desc.length() - 1) +
ClassUtil.toByteCodeClassName(constructorNode.owner);
ClassUtil.toByteCodeClassName(constructorNode.owner);
}
private int getConstructorStart(AbstractInsnNode[] instructions, String target, int rangeEnd) {
for (int i = rangeEnd - 1; i >= 0; i--) {
if (instructions[i].getOpcode() == Opcodes.NEW && ((TypeInsnNode)instructions[i]).desc.equals(target)) {
if (instructions[i].getOpcode() == Opcodes.NEW && ((TypeInsnNode) instructions[i]).desc.equals(target)) {
return i;
}
}
@@ -158,7 +172,7 @@ public class SourceClassHandler extends BaseClassHandler {
}
private int getMemberMethodStart(AbstractInsnNode[] instructions, int rangeEnd) {
int stackLevel = getInitialStackLevel((MethodInsnNode)instructions[rangeEnd]);
int stackLevel = getInitialStackLevel((MethodInsnNode) instructions[rangeEnd]);
if (stackLevel < 0) {
return rangeEnd;
}
@@ -178,13 +192,13 @@ public class SourceClassHandler extends BaseClassHandler {
break;
case LookingForLabel:
if (instructions[i] instanceof LabelNode) {
labelToJump = ((LabelNode)instructions[i]).getLabel();
labelToJump = ((LabelNode) instructions[i]).getLabel();
status = TravelStatus.LookingForJump;
}
break;
case LookingForJump:
if (instructions[i] instanceof JumpInsnNode &&
((JumpInsnNode)instructions[i]).label.getLabel().equals(labelToJump)) {
((JumpInsnNode) instructions[i]).label.getLabel().equals(labelToJump)) {
stackLevel += getStackLevelChange(instructions[i]);
labelToJump = null;
status = TravelStatus.Normal;
@@ -217,11 +231,11 @@ public class SourceClassHandler extends BaseClassHandler {
case Opcodes.INVOKESPECIAL:
case Opcodes.INVOKEVIRTUAL:
case Opcodes.INVOKEINTERFACE:
return stackEffectOfInvocation(((MethodInsnNode)instruction).desc) + 1;
return stackEffectOfInvocation(((MethodInsnNode) instruction).desc) + 1;
case Opcodes.INVOKESTATIC:
return stackEffectOfInvocation(((MethodInsnNode)instruction).desc);
return stackEffectOfInvocation(((MethodInsnNode) instruction).desc);
case Opcodes.INVOKEDYNAMIC:
return stackEffectOfInvocation(((InvokeDynamicInsnNode)instruction).desc);
return stackEffectOfInvocation(((InvokeDynamicInsnNode) instruction).desc);
case -1:
// either LabelNode or LineNumberNode
return 0;
@@ -235,7 +249,7 @@ public class SourceClassHandler extends BaseClassHandler {
}
private AbstractInsnNode[] replaceNewOps(MethodNode mn, MethodInfo newOperatorInjectMethod,
AbstractInsnNode[] instructions, int start, int end) {
AbstractInsnNode[] instructions, int start, int end) {
String mockMethodName = newOperatorInjectMethod.getMockName();
int invokeOpcode = newOperatorInjectMethod.isStatic() ? INVOKESTATIC : INVOKEVIRTUAL;
String log = String.format("Line %d, mock method \"%s\" used", getLineNum(instructions, start), mockMethodName);
@@ -244,14 +258,14 @@ public class SourceClassHandler extends BaseClassHandler {
} else {
LogUtil.diagnose(2, log);
}
String classType = ((TypeInsnNode)instructions[start]).desc;
String constructorDesc = ((MethodInsnNode)instructions[end]).desc;
String classType = ((TypeInsnNode) instructions[start]).desc;
String constructorDesc = ((MethodInsnNode) instructions[end]).desc;
if (!newOperatorInjectMethod.isStatic()) {
mn.instructions.insertBefore(instructions[start], new MethodInsnNode(INVOKESTATIC, mockClassName,
GET_TESTABLE_REF, VOID_ARGS + ClassUtil.toByteCodeClassName(mockClassName), false));
GET_TESTABLE_REF, VOID_ARGS + ClassUtil.toByteCodeClassName(mockClassName), false));
}
mn.instructions.insertBefore(instructions[end], new MethodInsnNode(invokeOpcode, mockClassName,
mockMethodName, getConstructorInjectDesc(constructorDesc, classType), false));
mockMethodName, getConstructorInjectDesc(constructorDesc, classType), false));
mn.instructions.remove(instructions[start]);
mn.instructions.remove(instructions[start + 1]);
mn.instructions.remove(instructions[end]);
@@ -261,7 +275,7 @@ public class SourceClassHandler extends BaseClassHandler {
private int getLineNum(AbstractInsnNode[] instructions, int start) {
for (int i = start - 1; i >= 0; i--) {
if (instructions[i] instanceof LineNumberNode) {
return ((LineNumberNode)instructions[i]).line;
return ((LineNumberNode) instructions[i]).line;
}
}
return 0;
@@ -269,13 +283,13 @@ public class SourceClassHandler extends BaseClassHandler {
private String getConstructorInjectDesc(String constructorDesc, String classType) {
return constructorDesc.substring(0, constructorDesc.length() - 1) +
ClassUtil.toByteCodeClassName(classType);
ClassUtil.toByteCodeClassName(classType);
}
private AbstractInsnNode[] replaceMemberCallOps(MethodNode mn, MethodInfo mockMethod, AbstractInsnNode[] instructions,
String ownerClass, int opcode, int start, int end) {
String ownerClass, int opcode, int start, int end) {
String log = String.format("Line %d, mock method \"%s\" used", getLineNum(instructions, start),
mockMethod.getMockName());
mockMethod.getMockName());
if (LogUtil.isVerboseEnabled()) {
LogUtil.verbose(5, log);
} else {
@@ -283,7 +297,7 @@ public class SourceClassHandler extends BaseClassHandler {
}
if (!mockMethod.isStatic()) {
mn.instructions.insertBefore(instructions[start], new MethodInsnNode(INVOKESTATIC, mockClassName,
GET_TESTABLE_REF, VOID_ARGS + ClassUtil.toByteCodeClassName(mockClassName), false));
GET_TESTABLE_REF, VOID_ARGS + ClassUtil.toByteCodeClassName(mockClassName), false));
}
if (Opcodes.INVOKESTATIC == opcode || isCompanionMethod(ownerClass, opcode)) {
// append a null value if it was a static invoke or in kotlin companion class
@@ -294,10 +308,10 @@ 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));
mockMethod.getMockName(), mockMethod.getMockDesc(), false));
mn.instructions.remove(instructions[end]);
mn.maxStack++;
return mn.instructions.toArray();
@@ -308,7 +322,7 @@ public class SourceClassHandler extends BaseClassHandler {
AbstractInsnNode endInsn = mn.instructions.get(end);
do {
if (curInsn instanceof FrameNode) {
FrameNode fn = (FrameNode)curInsn;
FrameNode fn = (FrameNode) curInsn;
if (fn.type == F_FULL) {
fn.stack.add(0, mockMethod.getMockClass());
// remove label reference in stack of frame node
@@ -327,4 +341,196 @@ public class SourceClassHandler extends BaseClassHandler {
return Opcodes.INVOKEVIRTUAL == opcode && ClassUtil.isCompanionClassName(ownerClass);
}
private void setFinalValue(Field ownerField, Object obj, Object value) throws Exception {
ownerField.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(ownerField, ownerField.getModifiers() & ~Modifier.FINAL);
ownerField.set(obj, value);
}
private List<Handle> fetchInvokeDynamicHandle(MethodNode mn) {
List<Handle> handleList = new ArrayList<Handle>();
for (AbstractInsnNode instruction : mn.instructions) {
if (instruction.getOpcode() == Opcodes.INVOKEDYNAMIC) {
InvokeDynamicInsnNode invokeDynamicInsnNode = (InvokeDynamicInsnNode) instruction;
handleList.add((Handle) invokeDynamicInsnNode.bsmArgs[1]);
}
}
return handleList;
}
private void resolveMethodReference(ClassNode cn) {
List<Handle> invokeDynamicList = new ArrayList<Handle>();
for (MethodNode method : cn.methods) {
List<Handle> handleList = fetchInvokeDynamicHandle(method);
invokeDynamicList.addAll(handleList);
}
// process for method reference
for (Handle handle : invokeDynamicList) {
// the jdk auto generation method
if (handle.getName().startsWith("lambda$")) {
continue;
}
int tag = handle.getTag();
if (tag == Opcodes.H_NEWINVOKESPECIAL) {
// lambda new method reference
continue;
}
// external mean:
// public void foo() {
// String s = "";
// consumes(s::contains);
//}
boolean external = tag == Opcodes.H_INVOKEVIRTUAL;
boolean isStatic = tag == Opcodes.H_INVOKESTATIC || external;
String desc = handle.getDesc();
String parameters = desc.substring(desc.indexOf("(") + 1, desc.lastIndexOf(")"));
String returnType = desc.substring(desc.indexOf(")") + 1);
String[] parameterArray = parameters.split(";");
int len = parameterArray.length;
for (String s : parameterArray) {
if (s.isEmpty()) {
len--;
}
}
len = external ? len + 1 : len;
String[] refineParameterArray = new String[len];
int index = external ? 1 : 0;
if (external) {
// The type should was reference type
refineParameterArray[0] = "L" + handle.getOwner();
}
for (String s : parameterArray) {
if (!s.isEmpty()) {
refineParameterArray[index] = s;
index++;
}
}
String externalDesc = buildDesc(refineParameterArray, returnType);
String lambdaName = String.format("Lambda$_%s_%d", handle.getName(), atomicInteger.incrementAndGet());
MethodVisitor mv = cn.visitMethod(isStatic ? ACC_PUBLIC + ACC_STATIC : ACC_PUBLIC, lambdaName, external ? externalDesc : desc, null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
if (!isStatic) {
// add this
mv.visitVarInsn(ALOAD, 0);
}
for (int i = 0; i < refineParameterArray.length; i++) {
String arg = refineParameterArray[i];
mv.visitVarInsn(getLoadType(arg), isStatic ? i : i + 1);
}
mv.visitMethodInsn(isStatic ? INVOKESTATIC : INVOKEVIRTUAL, handle.getOwner(), handle.getName(), desc, false);
mv.visitInsn(getReturnType(returnType));
Label l1 = new Label();
mv.visitLabel(l1);
// static function was not required add this to first parameter
if (isStatic) {
for (int i = 0; i < refineParameterArray.length; i++) {
String localVar = refineParameterArray[i];
if (!isPrimitive(localVar)) {
// primitive type and reference type difference
localVar = localVar.endsWith(";") ? localVar : localVar + ";";
}
if (localVar.isEmpty()) {
continue;
}
// add local var
mv.visitLocalVariable(String.format("o%d", i), localVar, null, l0, l1, i);
}
} else {
mv.visitLocalVariable("this", "L" + handle.getOwner() + ";", null, l0, l1, 0);
for (int i = 0; i < refineParameterArray.length; i++) {
String localVar = refineParameterArray[i];
if (!isPrimitive(localVar) && !isPrimitiveArray(localVar)) {
localVar = localVar.endsWith(";") ? localVar : localVar + ";";
}
if (localVar.isEmpty()) {
continue;
}
mv.visitLocalVariable(String.format("o%d", i), localVar, null, l0, l1, i + 1);
}
}
// auto compute max
mv.visitMaxs(-1, -1);
mv.visitEnd();
try {
// modify handle to the generation method
setFinalValue(handle.getClass().getDeclaredField("name"), handle, lambdaName);
// mark: should merge the below two if.
if (!handle.getOwner().equals(cn.name) && isStatic) {
setFinalValue(handle.getClass().getDeclaredField("owner"), handle, cn.name);
}
if (external) {
setFinalValue(handle.getClass().getDeclaredField("owner"), handle, cn.name);
setFinalValue(handle.getClass().getDeclaredField("descriptor"), handle, externalDesc);
setFinalValue(handle.getClass().getDeclaredField("tag"), handle, H_INVOKESTATIC);
}
} catch (Exception ignore) {
}
}
}
private String buildDesc(String[] refineParameterArray, String returnType) {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (String s : refineParameterArray) {
sb.append(s);
if (!isPrimitive(s)) {
sb.append(";");
}
}
sb.append(")");
sb.append(returnType);
return sb.toString();
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean isPrimitive(String type) {
if (type.endsWith(";")) {
type = type.substring(0, type.length() - 1);
}
return BasicType.basicType(type.charAt(0)).isPrimitive();
}
private boolean isPrimitiveArray(String type) {
if (!type.startsWith("[")) {
return false;
}
if (type.endsWith(";")) {
type = type.substring(0, type.length() - 1);
}
type = type.replace("[", "");
return BasicType.basicType(type.charAt(0)).isPrimitive();
}
private int getReturnType(String returnType) {
return BasicType.basicType(returnType.charAt(0)).returnInsn;
}
private int getLoadType(String arg) {
return BasicType.basicType(arg.charAt(0)).loadVarInsn;
}
}

View File

@@ -0,0 +1,61 @@
package com.alibaba.testable.agent.model;
import static org.objectweb.asm.Opcodes.*;
/**
* simplified of java.lang.invoke.LambdaForm.BasicType
*/
public enum BasicType {
/**
* all reference types
*/
L_TYPE('L', Object.class, WrapperType.OBJECT, ALOAD, ARETURN),
/**
* all primitive types
*/
I_TYPE('I', int.class, WrapperType.INT, ILOAD, IRETURN),
J_TYPE('J', long.class, WrapperType.LONG, LLOAD, LRETURN),
F_TYPE('F', float.class, WrapperType.FLOAT, FLOAD, FRETURN),
D_TYPE('D', double.class, WrapperType.DOUBLE, DLOAD, DRETURN),
V_TYPE('V', void.class, WrapperType.VOID, null, RETURN),
A_TYPE('[', Object[].class, WrapperType.OBJECT, ALOAD, ARETURN);
public final char btChar;
public final Class<?> btClass;
public final WrapperType btWrapper;
public final Integer loadVarInsn;
public final Integer returnInsn;
BasicType(char btChar, Class<?> btClass, WrapperType btWrapper, Integer loadVarInsn, Integer returnInsn) {
this.btChar = btChar;
this.btClass = btClass;
this.btWrapper = btWrapper;
this.loadVarInsn = loadVarInsn;
this.returnInsn = returnInsn;
}
public boolean isPrimitive() {
return this != L_TYPE && this != A_TYPE;
}
public static BasicType basicType(char type) {
switch (type) {
case 'L': return L_TYPE;
case 'I': return I_TYPE;
case 'J': return J_TYPE;
case 'F': return F_TYPE;
case 'D': return D_TYPE;
case 'V': return V_TYPE;
case '[': return A_TYPE;
// all subword types are represented as ints
case 'Z':
case 'B':
case 'S':
case 'C':
return I_TYPE;
default:
throw new InternalError("Unknown type char: '"+type+"'");
}
}
}

View File

@@ -0,0 +1,276 @@
package com.alibaba.testable.agent.model;
/**
* simplified of sun.invoke.util.Wrapper
*/
public enum WrapperType {
// wrapperType primitiveType char format
BOOLEAN( Boolean.class, boolean.class, 'Z', Format.unsigned( 1)),
// These must be in the order defined for widening primitive conversions in JLS 5.1.2
BYTE ( Byte.class, byte.class, 'B', Format.signed( 8)),
SHORT ( Short.class, short.class, 'S', Format.signed( 16)),
CHAR (Character.class, char.class, 'C', Format.unsigned(16)),
INT ( Integer.class, int.class, 'I', Format.signed( 32)),
LONG ( Long.class, long.class, 'J', Format.signed( 64)),
FLOAT ( Float.class, float.class, 'F', Format.floating(32)),
DOUBLE ( Double.class, double.class, 'D', Format.floating(64)),
OBJECT ( Object.class, Object.class, 'L', Format.other( 1)),
// VOID must be the last type, since it is "assignable" from any other type:
VOID ( Void.class, void.class, 'V', Format.other( 0)),
;
private final Class<?> wrapperType;
private final Class<?> primitiveType;
private final char basicTypeChar;
private final int format;
WrapperType(Class<?> wtype, Class<?> ptype, char tchar, int format) {
this.wrapperType = wtype;
this.primitiveType = ptype;
this.basicTypeChar = tchar;
this.format = format;
}
private static abstract class Format {
static final int SLOT_SHIFT = 0, SIZE_SHIFT = 2, KIND_SHIFT = 12;
static final int
SIGNED = (-1) << KIND_SHIFT,
UNSIGNED = 0 << KIND_SHIFT,
FLOATING = 1 << KIND_SHIFT;
static final int
SLOT_MASK = ((1<<(SIZE_SHIFT-SLOT_SHIFT))-1),
SIZE_MASK = ((1<<(KIND_SHIFT-SIZE_SHIFT))-1);
static int format(int kind, int size, int slots) {
assert(((kind >> KIND_SHIFT) << KIND_SHIFT) == kind);
assert((size & (size-1)) == 0); // power of two
assert((kind == SIGNED) ? (size > 0) : (kind == UNSIGNED) ? (size > 0) : (kind == FLOATING) ? (size == 32 || size == 64) : false);
assert((slots == 2) ? (size == 64) : (slots == 1) ? (size <= 32) : false);
return kind | (size << SIZE_SHIFT) | (slots << SLOT_SHIFT);
}
static final int
INT = SIGNED | (32 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
SHORT = SIGNED | (16 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
BOOLEAN = UNSIGNED | (1 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
CHAR = UNSIGNED | (16 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
FLOAT = FLOATING | (32 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
VOID = UNSIGNED | (0 << SIZE_SHIFT) | (0 << SLOT_SHIFT),
NUM_MASK = (-1) << SIZE_SHIFT;
static int signed(int size) { return format(SIGNED, size, (size > 32 ? 2 : 1)); }
static int unsigned(int size) { return format(UNSIGNED, size, (size > 32 ? 2 : 1)); }
static int floating(int size) { return format(FLOATING, size, (size > 32 ? 2 : 1)); }
static int other(int slots) { return slots << SLOT_SHIFT; }
}
/// format queries:
/** How many bits are in the wrapped value? Returns 0 for OBJECT or VOID. */
public int bitWidth() { return (format >> Format.SIZE_SHIFT) & Format.SIZE_MASK; }
/** How many JVM stack slots occupied by the wrapped value? Returns 0 for VOID. */
public int stackSlots() { return (format >> Format.SLOT_SHIFT) & Format.SLOT_MASK; }
/** Does the wrapped value occupy a single JVM stack slot? */
public boolean isSingleWord() { return (format & (1 << Format.SLOT_SHIFT)) != 0; }
/** Does the wrapped value occupy two JVM stack slots? */
public boolean isDoubleWord() { return (format & (2 << Format.SLOT_SHIFT)) != 0; }
/** Is the wrapped type numeric (not void or object)? */
public boolean isNumeric() { return (format & Format.NUM_MASK) != 0; }
/** Is the wrapped type a primitive other than float, double, or void? */
public boolean isIntegral() { return isNumeric() && format < Format.FLOAT; }
/** Is the wrapped type one of int, boolean, byte, char, or short? */
public boolean isSubwordOrInt() { return isIntegral() && isSingleWord(); }
/* Is the wrapped value a signed integral type (one of byte, short, int, or long)? */
public boolean isSigned() { return format < Format.VOID; }
/* Is the wrapped value an unsigned integral type (one of boolean or char)? */
public boolean isUnsigned() { return format >= Format.BOOLEAN && format < Format.FLOAT; }
/** Is the wrapped type either float or double? */
public boolean isFloating() { return format >= Format.FLOAT; }
/** Is the wrapped type either void or a reference? */
public boolean isOther() { return (format & ~Format.SLOT_MASK) == 0; }
/** Does the JLS 5.1.2 allow a variable of this wrapper's
* primitive type to be assigned from a value of the given wrapper's primitive type?
* Cases:
* <ul>
* <li>unboxing followed by widening primitive conversion
* <li>any type converted to {@code void} (i.e., dropping a method call's value)
* <li>boxing conversion followed by widening reference conversion to {@code Object}
* </ul>
* These are the cases allowed by MethodHandle.asType.
*/
public boolean isConvertibleFrom(WrapperType source) {
if (this == source) return true;
if (this.compareTo(source) < 0) {
// At best, this is a narrowing conversion.
return false;
}
// All conversions are allowed in the enum order between floats and signed ints.
// First detect non-signed non-float types (boolean, char, Object, void).
boolean floatOrSigned = (((this.format & source.format) & Format.SIGNED) != 0);
if (!floatOrSigned) {
if (this.isOther()) return true;
// can convert char to int or wider, but nothing else
return source.format == Format.CHAR;
// no other conversions are classified as widening
}
// All signed and float conversions in the enum order are widening.
assert(this.isFloating() || this.isSigned());
assert(source.isFloating() || source.isSigned());
return true;
}
static { assert(checkConvertibleFrom()); }
private static boolean checkConvertibleFrom() {
// Check the matrix for correct classification of widening conversions.
for (WrapperType w : values()) {
assert(w.isConvertibleFrom(w));
assert(VOID.isConvertibleFrom(w));
if (w != VOID) {
assert(OBJECT.isConvertibleFrom(w));
assert(!w.isConvertibleFrom(VOID));
}
// check relations with unsigned integral types:
if (w != CHAR) {
assert(!CHAR.isConvertibleFrom(w));
assert w.isConvertibleFrom(INT) || (!w.isConvertibleFrom(CHAR));
}
if (w != BOOLEAN) {
assert(!BOOLEAN.isConvertibleFrom(w));
assert w == VOID || w == OBJECT || (!w.isConvertibleFrom(BOOLEAN));
}
// check relations with signed integral types:
if (w.isSigned()) {
for (WrapperType x : values()) {
if (w == x) continue;
if (x.isFloating())
assert(!w.isConvertibleFrom(x));
else if (x.isSigned()) {
if (w.compareTo(x) < 0)
assert(!w.isConvertibleFrom(x));
else
assert(w.isConvertibleFrom(x));
}
}
}
// check relations with floating types:
if (w.isFloating()) {
for (WrapperType x : values()) {
if (w == x) continue;
if (x.isSigned())
assert(w.isConvertibleFrom(x));
else if (x.isFloating()) {
if (w.compareTo(x) < 0)
assert(!w.isConvertibleFrom(x));
else
assert(w.isConvertibleFrom(x));
}
}
}
}
return true; // i.e., assert(true)
}
// Note on perfect hashes:
// for signature chars c, do (c + (c >> 1)) % 16
// for primitive type names n, do (n[0] + n[2]) % 16
// The type name hash works for both primitive and wrapper names.
// You can add "java/lang/Object" to the primitive names.
// But you add the wrapper name Object, use (n[2] + (3*n[1])) % 16.
private static final WrapperType[] FROM_PRIM = new WrapperType[16];
private static final WrapperType[] FROM_WRAP = new WrapperType[16];
private static final WrapperType[] FROM_CHAR = new WrapperType[16];
private static int hashPrim(Class<?> x) {
String xn = x.getName();
if (xn.length() < 3) return 0;
return (xn.charAt(0) + xn.charAt(2)) % 16;
}
private static int hashWrap(Class<?> x) {
String xn = x.getName();
final int offset = 10;
if (xn.length() < offset+3) return 0;
return (3*xn.charAt(offset+1) + xn.charAt(offset+2)) % 16;
}
private static int hashChar(char x) {
return (x + (x >> 1)) % 16;
}
static {
for (WrapperType w : values()) {
int pi = hashPrim(w.primitiveType);
int wi = hashWrap(w.wrapperType);
int ci = hashChar(w.basicTypeChar);
assert(FROM_PRIM[pi] == null);
assert(FROM_WRAP[wi] == null);
assert(FROM_CHAR[ci] == null);
FROM_PRIM[pi] = w;
FROM_WRAP[wi] = w;
FROM_CHAR[ci] = w;
}
}
/** Wrap a value in this wrapper's type.
* Performs standard primitive conversions, including truncation and float conversions.
* Performs returns the unchanged reference for {@code OBJECT}.
* Returns null for {@code VOID}.
* Returns a zero value for a null input.
* @throws ClassCastException if this wrapper is numeric and the operand
* is not a number, character, boolean, or null
*/
public Object wrap(Object x) {
// do non-numeric wrappers first
switch (basicTypeChar) {
case 'L': return x;
case 'V': return null;
}
Number xn = numberValue(x);
switch (basicTypeChar) {
case 'I': return xn.intValue();
case 'J': return xn.longValue();
case 'F': return xn.floatValue();
case 'D': return xn.doubleValue();
case 'S': return (short) xn.intValue();
case 'B': return (byte) xn.intValue();
case 'C': return (char) xn.intValue();
case 'Z': return boolValue(xn.byteValue());
}
throw new InternalError("bad wrapper");
}
/** Wrap a value (an int or smaller value) in this wrapper's type.
* Performs standard primitive conversions, including truncation and float conversions.
* Produces an {@code Integer} for {@code OBJECT}, although the exact type
* of the operand is not known.
* Returns null for {@code VOID}.
*/
public Object wrap(int x) {
if (basicTypeChar == 'L') return x;
switch (basicTypeChar) {
case 'L': throw new IllegalArgumentException("cannot wrap to object type");
case 'V': return null;
case 'I': return x;
case 'J': return (long) x;
case 'F': return (float) x;
case 'D': return (double) x;
case 'S': return (short) x;
case 'B': return (byte) x;
case 'C': return (char) x;
case 'Z': return boolValue((byte) x);
}
throw new InternalError("bad wrapper");
}
private static Number numberValue(Object x) {
if (x instanceof Number) return (Number)x;
if (x instanceof Character) return (int)(Character)x;
if (x instanceof Boolean) return (Boolean)x ? 1 : 0;
// Remaining allowed case of void: Must be a null reference.
return (Number)x;
}
// Parameter type of boolValue must be byte, because
// MethodHandles.explicitCastArguments defines boolean
// conversion as first converting to byte.
private static boolean boolValue(byte bits) {
bits &= 1; // simple 31-bit zero extension
return (bits != 0);
}
}

View File

@@ -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;
}
}
@@ -75,6 +75,12 @@ public class MockClassParser {
mns.addAll(getAllMethods(scn));
}
}
for(String interfaceClass : cn.interfaces) {
ClassNode scn = ClassUtil.getClassNode(interfaceClass);
if (scn != null) {
mns.addAll(getAllMethods(scn));
}
}
for (InnerClassNode innerClass : cn.innerClasses) {
if (innerClass.name.equals(cn.name + KOTLIN_POSTFIX_COMPANION)) {
ClassNode scn = ClassUtil.getClassNode(innerClass.name);
@@ -92,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);
}

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-parent</artifactId>
<version>0.6.8</version>
<version>0.6.10</version>
<relativePath>../testable-parent</relativePath>
</parent>
<artifactId>testable-all</artifactId>

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-parent</artifactId>
<version>0.6.8</version>
<version>0.6.10</version>
<relativePath>../testable-parent</relativePath>
</parent>
<artifactId>testable-core</artifactId>

View File

@@ -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

View File

@@ -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

View File

@@ -10,19 +10,19 @@ import java.util.Set;
/**
* @author flin
*/
public class InvokeMatcher {
public class InvocationMatcher {
public MatchFunction matchFunction;
private InvokeMatcher(MatchFunction matchFunction) {
private InvocationMatcher(MatchFunction matchFunction) {
this.matchFunction = matchFunction;
}
public static InvokeMatcher any(MatchFunction matcher) {
return new InvokeMatcher(matcher);
public static InvocationMatcher any(MatchFunction matcher) {
return new InvocationMatcher(matcher);
}
public static InvokeMatcher any() {
public static InvocationMatcher any() {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -31,47 +31,47 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher anyString() {
public static InvocationMatcher anyString() {
return any(String.class);
}
public static InvokeMatcher anyNumber() {
public static InvocationMatcher anyNumber() {
return anyTypeOf(Short.class, Integer.class, Long.class, Float.class, Double.class);
}
public static InvokeMatcher anyBoolean() {
public static InvocationMatcher anyBoolean() {
return any(Boolean.class);
}
public static InvokeMatcher anyByte() {
public static InvocationMatcher anyByte() {
return any(Byte.class);
}
public static InvokeMatcher anyChar() {
public static InvocationMatcher anyChar() {
return any(Character.class);
}
public static InvokeMatcher anyInt() {
public static InvocationMatcher anyInt() {
return any(Integer.class);
}
public static InvokeMatcher anyLong() {
public static InvocationMatcher anyLong() {
return any(Long.class);
}
public static InvokeMatcher anyFloat() {
public static InvocationMatcher anyFloat() {
return any(Float.class);
}
public static InvokeMatcher anyDouble() {
public static InvocationMatcher anyDouble() {
return any(Double.class);
}
public static InvokeMatcher anyShort() {
public static InvocationMatcher anyShort() {
return any(Short.class);
}
public static InvokeMatcher anyArray() {
public static InvocationMatcher anyArray() {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -81,7 +81,7 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher anyArrayOf(final Class<?> clazz) {
public static InvocationMatcher anyArrayOf(final Class<?> clazz) {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -92,47 +92,47 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher anyList() {
public static InvocationMatcher anyList() {
return any(List.class);
}
public static InvokeMatcher anyListOf(final Class<?> clazz) {
public static InvocationMatcher anyListOf(final Class<?> clazz) {
return anyClassWithCollectionOf(List.class, clazz);
}
public static InvokeMatcher anySet() {
public static InvocationMatcher anySet() {
return any(Set.class);
}
public static InvokeMatcher anySetOf(final Class<?> clazz) {
public static InvocationMatcher anySetOf(final Class<?> clazz) {
return anyClassWithCollectionOf(Set.class, clazz);
}
public static InvokeMatcher anyMap() {
public static InvocationMatcher anyMap() {
return any(Map.class);
}
public static InvokeMatcher anyMapOf(final Class<?> keyClass, final Class<?> valueClass) {
public static InvocationMatcher anyMapOf(final Class<?> keyClass, final Class<?> valueClass) {
return anyClassWithMapOf(keyClass, valueClass);
}
public static InvokeMatcher anyCollection() {
public static InvocationMatcher anyCollection() {
return any(Collection.class);
}
public static InvokeMatcher anyCollectionOf(final Class<?> clazz) {
public static InvocationMatcher anyCollectionOf(final Class<?> clazz) {
return anyClassWithCollectionOf(Collection.class, clazz);
}
public static InvokeMatcher anyIterable() {
public static InvocationMatcher anyIterable() {
return any(Iterable.class);
}
public static InvokeMatcher anyIterableOf(final Class<?> clazz) {
public static InvocationMatcher anyIterableOf(final Class<?> clazz) {
return anyClassWithCollectionOf(Iterable.class, clazz);
}
public static InvokeMatcher any(final Class<?> clazz) {
public static InvocationMatcher any(final Class<?> clazz) {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -141,7 +141,7 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher anyTypeOf(final Class<?>... classes) {
public static InvocationMatcher anyTypeOf(final Class<?>... classes) {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -158,7 +158,7 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher eq(final Object obj) {
public static InvocationMatcher eq(final Object obj) {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -167,7 +167,7 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher refEq(final Object obj) {
public static InvocationMatcher refEq(final Object obj) {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -176,7 +176,7 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher isNull() {
public static InvocationMatcher isNull() {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -185,7 +185,7 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher notNull() {
public static InvocationMatcher notNull() {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -194,7 +194,7 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher nullable(final Class<?> clazz) {
public static InvocationMatcher nullable(final Class<?> clazz) {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -203,7 +203,7 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher contains(final String substring) {
public static InvocationMatcher contains(final String substring) {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -212,7 +212,7 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher matches(final String regex) {
public static InvocationMatcher matches(final String regex) {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -221,7 +221,7 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher endsWith(final String suffix) {
public static InvocationMatcher endsWith(final String suffix) {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -230,7 +230,7 @@ public class InvokeMatcher {
});
}
public static InvokeMatcher startsWith(final String prefix) {
public static InvocationMatcher startsWith(final String prefix) {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -239,7 +239,7 @@ public class InvokeMatcher {
});
}
private static InvokeMatcher anyClassWithCollectionOf(final Class<?> collectionClass, final Class<?> clazz) {
private static InvocationMatcher anyClassWithCollectionOf(final Class<?> collectionClass, final Class<?> clazz) {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {
@@ -250,7 +250,7 @@ public class InvokeMatcher {
});
}
private static InvokeMatcher anyClassWithMapOf(final Class<?> keyClass, final Class<?> valueClass) {
private static InvocationMatcher anyClassWithMapOf(final Class<?> keyClass, final Class<?> valueClass) {
return any(new MatchFunction() {
@Override
public boolean check(Object value) {

View File

@@ -11,12 +11,12 @@ import java.util.List;
/**
* @author flin
*/
public class InvokeVerifier {
public class InvocationVerifier {
private final List<Object[]> records;
private Verification lastVerification = null;
private InvokeVerifier(List<Object[]> records) {
private InvocationVerifier(List<Object[]> records) {
this.records = records;
}
@@ -25,8 +25,8 @@ public class InvokeVerifier {
* @param mockMethodName name of a mock method
* @return the verifier object
*/
public static InvokeVerifier verify(String mockMethodName) {
return new InvokeVerifier(MockContextUtil.context.get().invokeRecord.get(mockMethodName));
public static InvocationVerifier verifyInvoked(String mockMethodName) {
return new InvocationVerifier(MockContextUtil.context.get().invokeRecord.get(mockMethodName));
}
/**
@@ -34,7 +34,7 @@ public class InvokeVerifier {
* @param args parameters to compare
* @return the verifier object
*/
public InvokeVerifier with(Object... args) {
public InvocationVerifier with(Object... args) {
boolean found = false;
for (int i = 0; i < records.size(); i++) {
try {
@@ -57,7 +57,7 @@ public class InvokeVerifier {
* @param args parameters to compare
* @return the verifier object
*/
public InvokeVerifier withInOrder(Object... args) {
public InvocationVerifier withInOrder(Object... args) {
withInternal(args, 0);
lastVerification = new Verification(args, true);
return this;
@@ -68,7 +68,7 @@ public class InvokeVerifier {
* @param args parameters to compare
* @return the verifier object
*/
public InvokeVerifier without(Object... args) {
public InvocationVerifier without(Object... args) {
for (Object[] r : records) {
if (r.length == args.length) {
for (int i = 0; i < r.length; i++) {
@@ -90,7 +90,7 @@ public class InvokeVerifier {
* @param expectedCount times to compare
* @return the verifier object
*/
public InvokeVerifier withTimes(int expectedCount) {
public InvocationVerifier withTimes(int expectedCount) {
if (expectedCount != records.size()) {
throw new VerifyFailedError("times: " + expectedCount, "times: " + records.size());
}
@@ -103,7 +103,7 @@ public class InvokeVerifier {
* @param count number of invocations
* @return the verifier object
*/
public InvokeVerifier times(int count) {
public InvocationVerifier times(int count) {
if (lastVerification == null) {
// when used independently, equals to `withTimes()`
System.out.println("Warning: [" + TestableUtil.previousStackLocation() + "] using \"times()\" method "
@@ -133,7 +133,7 @@ public class InvokeVerifier {
throw new VerifyFailedError(desc(args), desc(record));
}
for (int i = 0; i < args.length; i++) {
if (!(args[i] instanceof InvokeMatcher || args[i].getClass().equals(record[i].getClass()))) {
if (!(args[i] instanceof InvocationMatcher || args[i].getClass().equals(record[i].getClass()))) {
throw new VerifyFailedError("parameter " + (i + 1) + " type mismatch",
": " + args[i].getClass(), ": " + record[i].getClass());
}
@@ -145,8 +145,8 @@ public class InvokeVerifier {
}
private boolean matches(Object expectValue, Object realValue) {
return expectValue instanceof InvokeMatcher ?
((InvokeMatcher) expectValue).matchFunction.check(realValue) :
return expectValue instanceof InvocationMatcher ?
((InvocationMatcher) expectValue).matchFunction.check(realValue) :
expectValue.equals(realValue);
}

View File

@@ -109,10 +109,10 @@ public class PrivateAccessor {
}
Class<?> commonClass = cls[0];
for (int i = 1; i < cls.length; i++) {
if (cls[i].isPrimitive()) {
return null;
} else if (cls[i] == null) {
if (cls[i] == null) {
continue;
} else if (cls[i].isPrimitive()) {
return null;
}
commonClass = getCommonClassOf(commonClass, cls[i]);
}
@@ -189,13 +189,13 @@ public class PrivateAccessor {
} catch (IllegalAccessException e) {
throw new MemberAccessException("Failed to access private method \"" + method + "\"", e);
} catch (NoSuchFieldException e) {
throw new MemberAccessException("Private method \"" + method + "\" not exist");
throw new MemberAccessException("Private method \"" + method + "\" not exist", e);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof RuntimeException) {
throw (RuntimeException)e.getTargetException();
}
throw new MemberAccessException("Invoke private method \"" + method + "\" failed with exception", e);
}
throw new MemberAccessException("Private method \"" + method + "\" not exist");
throw new MemberAccessException("Private method \"" + method + "\" not found");
}
}

View File

@@ -1,5 +1,6 @@
package com.alibaba.testable.core.util;
import com.alibaba.testable.core.exception.MemberAccessException;
import com.alibaba.testable.core.model.MockContext;
import java.util.HashSet;
@@ -69,7 +70,14 @@ public class MockAssociationUtil {
if (originMethod.equals(CONSTRUCTOR)) {
return construct(originClass, args);
} else if (args[0] == null) {
return invokeStatic(originClass, originMethod, CollectionUtil.slice(args, 1));
try {
return invokeStatic(originClass, originMethod, CollectionUtil.slice(args, 1));
} catch (RuntimeException e) {
if (e instanceof MemberAccessException && e.getCause() instanceof NoSuchFieldException) {
throw new NullPointerException("Invoking method \"" + originMethod + "\" of null object");
}
throw e;
}
} else {
return invoke(args[0], originMethod, CollectionUtil.slice(args, 1));
}

View File

@@ -2,6 +2,7 @@ package com.alibaba.testable.core.tool;
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import java.util.List;
import static com.alibaba.testable.core.tool.PrivateAccessor.*;
@@ -13,40 +14,43 @@ class OmniAccessorTest {
void should_generate_member_index() {
List<String> index = invokeStatic(OmniAccessor.class, "generateMemberIndex", DemoParent.class);
assertEquals(34, index.size());
assertEquals("/c{DemoChild}", index.get(0));
assertEquals("/c{DemoChild}/gc{DemoGrandChild}", index.get(1));
assertEquals("/c{DemoChild}/gc{DemoGrandChild}/i{int}", index.get(2));
assertEquals("/c{DemoChild}/gc{DemoGrandChild}/l{long}", index.get(3));
assertEquals("/c{DemoChild}/gc{DemoGrandChild}/si{Integer}", index.get(4));
assertEquals("/c{DemoChild}/gc{DemoGrandChild}/sl{Long}", index.get(5));
assertEquals("/c{DemoChild}/gcs{DemoGrandChild[]}", index.get(6));
assertEquals("/c{DemoChild}/gcs{DemoGrandChild[]}/i{int}", index.get(7));
assertEquals("/c{DemoChild}/gcs{DemoGrandChild[]}/l{long}", index.get(8));
assertEquals("/c{DemoChild}/gcs{DemoGrandChild[]}/si{Integer}", index.get(9));
assertEquals("/c{DemoChild}/gcs{DemoGrandChild[]}/sl{Long}", index.get(10));
assertEquals("/cs{DemoChild[]}", index.get(11));
assertEquals("/cs{DemoChild[]}/gc{DemoGrandChild}", index.get(12));
assertEquals("/cs{DemoChild[]}/gc{DemoGrandChild}/i{int}", index.get(13));
assertEquals("/cs{DemoChild[]}/gc{DemoGrandChild}/l{long}", index.get(14));
assertEquals("/cs{DemoChild[]}/gc{DemoGrandChild}/si{Integer}", index.get(15));
assertEquals("/cs{DemoChild[]}/gc{DemoGrandChild}/sl{Long}", index.get(16));
assertEquals("/cs{DemoChild[]}/gcs{DemoGrandChild[]}", index.get(17));
assertEquals("/cs{DemoChild[]}/gcs{DemoGrandChild[]}/i{int}", index.get(18));
assertEquals("/cs{DemoChild[]}/gcs{DemoGrandChild[]}/l{long}", index.get(19));
assertEquals("/cs{DemoChild[]}/gcs{DemoGrandChild[]}/si{Integer}", index.get(20));
assertEquals("/cs{DemoChild[]}/gcs{DemoGrandChild[]}/sl{Long}", index.get(21));
assertEquals("/sc{SubChild}", index.get(22));
assertEquals("/sc{SubChild}/gc{DemoGrandChild}", index.get(23));
assertEquals("/sc{SubChild}/gc{DemoGrandChild}/i{int}", index.get(24));
assertEquals("/sc{SubChild}/gc{DemoGrandChild}/l{long}", index.get(25));
assertEquals("/sc{SubChild}/gc{DemoGrandChild}/si{Integer}", index.get(26));
assertEquals("/sc{SubChild}/gc{DemoGrandChild}/sl{Long}", index.get(27));
assertEquals("/ssc{StaticSubChild}", index.get(28));
assertEquals("/ssc{StaticSubChild}/gc{DemoGrandChild}", index.get(29));
assertEquals("/ssc{StaticSubChild}/gc{DemoGrandChild}/i{int}", index.get(30));
assertEquals("/ssc{StaticSubChild}/gc{DemoGrandChild}/l{long}", index.get(31));
assertEquals("/ssc{StaticSubChild}/gc{DemoGrandChild}/si{Integer}", index.get(32));
assertEquals("/ssc{StaticSubChild}/gc{DemoGrandChild}/sl{Long}", index.get(33));
HashSet<String> expected = new HashSet<String>(){{
add("/c{DemoChild}");
add("/c{DemoChild}/gc{DemoGrandChild}");
add("/c{DemoChild}/gc{DemoGrandChild}/i{int}");
add("/c{DemoChild}/gc{DemoGrandChild}/l{long}");
add("/c{DemoChild}/gc{DemoGrandChild}/si{Integer}");
add("/c{DemoChild}/gc{DemoGrandChild}/sl{Long}");
add("/c{DemoChild}/gcs{DemoGrandChild[]}");
add("/c{DemoChild}/gcs{DemoGrandChild[]}/i{int}");
add("/c{DemoChild}/gcs{DemoGrandChild[]}/l{long}");
add("/c{DemoChild}/gcs{DemoGrandChild[]}/si{Integer}");
add("/c{DemoChild}/gcs{DemoGrandChild[]}/sl{Long}");
add("/cs{DemoChild[]}");
add("/cs{DemoChild[]}/gc{DemoGrandChild}");
add("/cs{DemoChild[]}/gc{DemoGrandChild}/i{int}");
add("/cs{DemoChild[]}/gc{DemoGrandChild}/l{long}");
add("/cs{DemoChild[]}/gc{DemoGrandChild}/si{Integer}");
add("/cs{DemoChild[]}/gc{DemoGrandChild}/sl{Long}");
add("/cs{DemoChild[]}/gcs{DemoGrandChild[]}");
add("/cs{DemoChild[]}/gcs{DemoGrandChild[]}/i{int}");
add("/cs{DemoChild[]}/gcs{DemoGrandChild[]}/l{long}");
add("/cs{DemoChild[]}/gcs{DemoGrandChild[]}/si{Integer}");
add("/cs{DemoChild[]}/gcs{DemoGrandChild[]}/sl{Long}");
add("/sc{SubChild}");
add("/sc{SubChild}/gc{DemoGrandChild}");
add("/sc{SubChild}/gc{DemoGrandChild}/i{int}");
add("/sc{SubChild}/gc{DemoGrandChild}/l{long}");
add("/sc{SubChild}/gc{DemoGrandChild}/si{Integer}");
add("/sc{SubChild}/gc{DemoGrandChild}/sl{Long}");
add("/ssc{StaticSubChild}");
add("/ssc{StaticSubChild}/gc{DemoGrandChild}");
add("/ssc{StaticSubChild}/gc{DemoGrandChild}/i{int}");
add("/ssc{StaticSubChild}/gc{DemoGrandChild}/l{long}");
add("/ssc{StaticSubChild}/gc{DemoGrandChild}/si{Integer}");
add("/ssc{StaticSubChild}/gc{DemoGrandChild}/sl{Long}");
}};
assertEquals(expected, new HashSet<String>(index));
}
@Test

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-parent</artifactId>
<version>0.6.8</version>
<version>0.6.10</version>
<relativePath>../testable-parent</relativePath>
</parent>
<artifactId>testable-maven-plugin</artifactId>

View File

@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-parent</artifactId>
<version>0.6.8</version>
<version>0.6.10</version>
<packaging>pom</packaging>
<name>testable-parent</name>
<description>Unit test enhancement toolkit</description>
@@ -42,7 +42,7 @@
<plugin.gpg.version>1.6</plugin.gpg.version>
<plugin.staging.version>1.6.8</plugin.staging.version>
<plugin.maven.version>3.6.0</plugin.maven.version>
<testable.version>0.6.8</testable.version>
<testable.version>0.6.10</testable.version>
</properties>
<profiles>

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-parent</artifactId>
<version>0.6.8</version>
<version>0.6.10</version>
<relativePath>../testable-parent</relativePath>
</parent>
<artifactId>testable-processor</artifactId>