rename verify() to verifyInvoked(), avoid name conflict with system method

This commit is contained in:
金戟 2021-11-02 23:39:48 +08:00
parent 990f407ebe
commit 28500558fc
26 changed files with 179 additions and 182 deletions

View File

@ -9,7 +9,7 @@ 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;
@ -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

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

@ -6,7 +6,7 @@ import com.alibaba.demo.basic.model.mock.Color;
import com.alibaba.testable.core.annotation.MockMethod;
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;
/**
@ -57,49 +57,49 @@ class DemoInheritTest {
@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();
verify("get_colorIdx_from_color").withTimes(1);
verifyInvoked("get_colorIdx_from_color").withTimes(1);
assertEquals("colorIdx_from_color", colorIdx);
}
}

View File

@ -5,8 +5,8 @@ import com.alibaba.testable.core.annotation.MockMethod;
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;
/**
@ -31,49 +31,49 @@ class DemoMatcherTest {
@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

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

@ -3,7 +3,7 @@ package com.alibaba.demo.lambda;
import com.alibaba.testable.core.annotation.MockMethod;
import org.junit.jupiter.api.Test;
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
/**
* @author zcbbpo
@ -44,37 +44,37 @@ public class ExternalLambdaDemoTest {
@Test
public void shouldMockString1() {
lambdaDemo.string1();
verify("mockContains").withTimes(1);
verifyInvoked("mockContains").withTimes(1);
}
@Test
public void shouldMockByte1() {
lambdaDemo.byte1();
verify("mockFloatValue").withTimes(1);
verifyInvoked("mockFloatValue").withTimes(1);
}
@Test
public void shouldMockDouble2() {
lambdaDemo.double2();
verify("mockCompareTo").withTimes(1);
verifyInvoked("mockCompareTo").withTimes(1);
}
@Test
public void testMul() {
lambdaDemo.mul();
verify("mockContains").withTimes(2);
verifyInvoked("mockContains").withTimes(2);
}
@Test
public void testExternalClass() {
lambdaDemo.externalClass();
verify("mockMethodReference0").withTimes(1);
verifyInvoked("mockMethodReference0").withTimes(1);
}
@Test
public void testFunction3() {
lambdaDemo.function3();
verify("mockF3").withTimes(1);
verifyInvoked("mockF3").withTimes(1);
}
}

View File

@ -1,11 +1,9 @@
package com.alibaba.demo.lambda;
import com.alibaba.testable.core.annotation.MockDiagnose;
import com.alibaba.testable.core.annotation.MockMethod;
import com.alibaba.testable.core.model.LogLevel;
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;
/**
@ -15,7 +13,6 @@ public class LambdaDemoTest {
private final LambdaDemo lambdaDemo = new LambdaDemo();
@SuppressWarnings("unused")
@MockDiagnose(LogLevel.VERBOSE)
public static class Mock {
@MockMethod(targetClass = LambdaDemo.class, targetMethod = "run")
private void mockRun() {
@ -53,7 +50,7 @@ public class LambdaDemoTest {
@Test
public void shouldMockRun() {
lambdaDemo.methodReference();
verify("mockRun").withTimes(1);
verifyInvoked("mockRun").withTimes(1);
}
@Test

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

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

@ -2,8 +2,8 @@ package com.alibaba.demo.basic
import com.alibaba.testable.core.annotation.MockMethod
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
@ -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

@ -2,7 +2,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.matcher.InvokeVerifier.verify
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
@ -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.java2kotlin
import com.alibaba.testable.core.annotation.MockMethod
import com.alibaba.testable.core.matcher.InvokeVerifier.verify
import com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked
import org.junit.jupiter.api.Test
import java.io.File
@ -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,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

@ -6,7 +6,7 @@ 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 {
@ -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

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

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

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

@ -71,7 +71,7 @@ class Demo {
若要测试此方法,可以利用`TestableMock`快速Mock掉`System.out.println`方法。在Mock方法体里可以继续执行原调用相当于并不影响本来方法功能仅用于做调用记录也可以直接留空相当于去除了原方法的副作用
在执行完被测的void类型方法以后用`InvokeVerifier.verify()`校验传入的打印内容是否符合预期:
在执行完被测的void类型方法以后用`InvocationVerifier.verifyInvoked()`校验传入的打印内容是否符合预期:
```java
class DemoTest {
@ -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

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

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