diff --git a/demo/java-demo/src/test/java/com/alibaba/testable/demo/service/DemoMockServiceTest.java b/demo/java-demo/src/test/java/com/alibaba/testable/demo/service/DemoMockServiceTest.java index 9179c3b..e2eb215 100644 --- a/demo/java-demo/src/test/java/com/alibaba/testable/demo/service/DemoMockServiceTest.java +++ b/demo/java-demo/src/test/java/com/alibaba/testable/demo/service/DemoMockServiceTest.java @@ -75,21 +75,21 @@ class DemoMockServiceTest { @Test void should_able_to_mock_common_method() throws Exception { assertEquals("trim_string__sub_string__false", demoService.commonFunc()); - verify("trim").times(1); - verify("sub").times(1); - verify("startsWith").times(1); + verify("trim").withTimes(1); + verify("sub").withTimes(1); + verify("startsWith").withTimes(1); } @Test void should_able_to_mock_static_method() throws Exception { assertEquals("not_secret_box", demoService.getBox().get()); - verify("secretBox").times(1); + verify("secretBox").withTimes(1); } @Test void should_able_to_mock_override_method() throws Exception { BlackBox box = (BlackBox)demoService.putBox(); - verify("put").times(1); + verify("put").withTimes(1); assertEquals("put_data_mocked", box.get()); } @@ -100,7 +100,7 @@ class DemoMockServiceTest { // asynchronous assertEquals("mock_one_mock_others", Executors.newSingleThreadExecutor().submit(() -> demoService.callerOne() + "_" + demoService.callerTwo()).get()); - verify("callFromDifferentMethod").times(4); + verify("callFromDifferentMethod").withTimes(4); } @Test @@ -109,7 +109,7 @@ class DemoMockServiceTest { assertEquals("mock_special", demoService.callerOne()); // asynchronous assertEquals("mock_special", Executors.newSingleThreadExecutor().submit(() -> demoService.callerOne()).get()); - verify("callFromDifferentMethod").times(2); + verify("callFromDifferentMethod").withTimes(2); } } diff --git a/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/service/DemoMockServiceTest.kt b/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/service/DemoMockServiceTest.kt index a3ffa7e..9e9d0d6 100644 --- a/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/service/DemoMockServiceTest.kt +++ b/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/service/DemoMockServiceTest.kt @@ -71,22 +71,22 @@ internal class DemoMockServiceTest { @Test fun should_able_to_mock_common_method() { assertEquals("trim_string__sub_string__false", demoService.commonFunc()) - verify("trim").times(1) - verify("sub").times(1) - verify("startsWith").times(1) + verify("trim").withTimes(1) + verify("sub").withTimes(1) + verify("startsWith").withTimes(1) } @Test fun should_able_to_mock_static_method() { assertEquals("White_not_secret_box", demoService.getBox().get()) - verify("secretBox").times(1) - verify("createBox").times(1) + verify("secretBox").withTimes(1) + verify("createBox").withTimes(1) } @Test fun should_able_to_mock_override_method() { val box = demoService.putBox() as BlackBox - verify("put").times(1) + verify("put").withTimes(1) assertEquals("put_data_mocked", box.get()) } @@ -98,7 +98,7 @@ internal class DemoMockServiceTest { assertEquals("mock_one_mock_others", Executors.newSingleThreadExecutor().submit { demoService.callerOne() + "_" + demoService.callerTwo() }.get()) - verify("callFromDifferentMethod").times(4) + verify("callFromDifferentMethod").withTimes(4) } @Test @@ -109,6 +109,6 @@ internal class DemoMockServiceTest { assertEquals("mock_special", Executors.newSingleThreadExecutor().submit { demoService.callerOne() }.get()) - verify("callFromDifferentMethod").times(2) + verify("callFromDifferentMethod").withTimes(2) } } diff --git a/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/util/PathUtilTest.kt b/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/util/PathUtilTest.kt index ae60573..85c2310 100644 --- a/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/util/PathUtilTest.kt +++ b/demo/kotlin-demo/src/test/kotlin/com/alibaba/testable/demo/util/PathUtilTest.kt @@ -41,8 +41,8 @@ class PathUtilTest { @Test fun should_able_to_mock_java_method_invoke_in_kotlin() { PathUtil.deleteRecursively(File("/a/b/")) - verify("listFiles").times(2) - verify("delete").times(4) + verify("listFiles").withTimes(2) + verify("delete").withTimes(4) } } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/tool/InvokeVerifier.java b/testable-core/src/main/java/com/alibaba/testable/core/tool/InvokeVerifier.java index 6acc0d6..b414134 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/tool/InvokeVerifier.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/tool/InvokeVerifier.java @@ -16,35 +16,79 @@ public class InvokeVerifier { } public InvokeVerifier with(Object arg1) { - with(new Object[]{arg1}); - return this; + return with(new Object[]{arg1}); } public InvokeVerifier with(Object arg1, Object arg2) { - with(new Object[]{arg1, arg2}); - return this; + return with(new Object[]{arg1, arg2}); } public InvokeVerifier with(Object arg1, Object arg2, Object arg3) { - with(new Object[]{arg1, arg2, arg3}); - return this; + return with(new Object[]{arg1, arg2, arg3}); } public InvokeVerifier with(Object arg1, Object arg2, Object arg3, Object arg4) { - with(new Object[]{arg1, arg2, arg3, arg4}); - return this; + return with(new Object[]{arg1, arg2, arg3, arg4}); } public InvokeVerifier with(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) { - with(new Object[]{arg1, arg2, arg3, arg4, arg5}); - return this; + return with(new Object[]{arg1, arg2, arg3, arg4, arg5}); + } + + public InvokeVerifier withInOrder(Object arg1) { + return withInOrder(new Object[]{arg1}); + } + + public InvokeVerifier withInOrder(Object arg1, Object arg2) { + return withInOrder(new Object[]{arg1, arg2}); + } + + public InvokeVerifier withInOrder(Object arg1, Object arg2, Object arg3) { + return withInOrder(new Object[]{arg1, arg2, arg3}); + } + + public InvokeVerifier withInOrder(Object arg1, Object arg2, Object arg3, Object arg4) { + return withInOrder(new Object[]{arg1, arg2, arg3, arg4}); + } + + public InvokeVerifier withInOrder(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) { + return withInOrder(new Object[]{arg1, arg2, arg3, arg4, arg5}); } public InvokeVerifier with(Object[] args) { + boolean found = false; + for (int i = 0; i < records.size(); i++) { + try { + withInternal(args, i); + found = true; + break; + } catch (AssertionError e) { + // continue + } + } + if (!found) { + throw new VerifyFailedError("has not invoke with " + desc(args)); + } + return this; + } + + public InvokeVerifier withInOrder(Object[] args) { + withInternal(args, 0); + return this; + } + + public InvokeVerifier withTimes(int expectedCount) { + if (expectedCount != records.size()) { + throw new VerifyFailedError("times: " + records.size(), "times: " + expectedCount); + } + return this; + } + + private void withInternal(Object[] args, int order) { if (records.isEmpty()) { throw new VerifyFailedError("has not more invoke"); } - Object[] record = records.get(0); + Object[] record = records.get(order); if (record.length != args.length) { throw new VerifyFailedError(desc(args), desc(record)); } @@ -57,8 +101,7 @@ public class InvokeVerifier { throw new VerifyFailedError("parameter " + (i + 1) + " mismatched", desc(args), desc(record)); } } - records.remove(0); - return this; + records.remove(order); } private String desc(Object[] args) { @@ -72,11 +115,4 @@ public class InvokeVerifier { return sb.toString(); } - public InvokeVerifier times(int expectedCount) { - if (expectedCount != records.size()) { - throw new VerifyFailedError("times: " + records.size(), "times: " + expectedCount); - } - return this; - } - }