mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-02-05 01:01:02 +08:00
rename verify method names
This commit is contained in:
parent
6f74ba771c
commit
dc904496fc
@ -75,21 +75,21 @@ class DemoMockServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_common_method() throws Exception {
|
void should_able_to_mock_common_method() throws Exception {
|
||||||
assertEquals("trim_string__sub_string__false", demoService.commonFunc());
|
assertEquals("trim_string__sub_string__false", demoService.commonFunc());
|
||||||
verify("trim").times(1);
|
verify("trim").withTimes(1);
|
||||||
verify("sub").times(1);
|
verify("sub").withTimes(1);
|
||||||
verify("startsWith").times(1);
|
verify("startsWith").withTimes(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_static_method() throws Exception {
|
void should_able_to_mock_static_method() throws Exception {
|
||||||
assertEquals("not_secret_box", demoService.getBox().get());
|
assertEquals("not_secret_box", demoService.getBox().get());
|
||||||
verify("secretBox").times(1);
|
verify("secretBox").withTimes(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_override_method() throws Exception {
|
void should_able_to_mock_override_method() throws Exception {
|
||||||
BlackBox box = (BlackBox)demoService.putBox();
|
BlackBox box = (BlackBox)demoService.putBox();
|
||||||
verify("put").times(1);
|
verify("put").withTimes(1);
|
||||||
assertEquals("put_data_mocked", box.get());
|
assertEquals("put_data_mocked", box.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,7 +100,7 @@ class DemoMockServiceTest {
|
|||||||
// asynchronous
|
// asynchronous
|
||||||
assertEquals("mock_one_mock_others",
|
assertEquals("mock_one_mock_others",
|
||||||
Executors.newSingleThreadExecutor().submit(() -> demoService.callerOne() + "_" + demoService.callerTwo()).get());
|
Executors.newSingleThreadExecutor().submit(() -> demoService.callerOne() + "_" + demoService.callerTwo()).get());
|
||||||
verify("callFromDifferentMethod").times(4);
|
verify("callFromDifferentMethod").withTimes(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -109,7 +109,7 @@ class DemoMockServiceTest {
|
|||||||
assertEquals("mock_special", demoService.callerOne());
|
assertEquals("mock_special", demoService.callerOne());
|
||||||
// asynchronous
|
// asynchronous
|
||||||
assertEquals("mock_special", Executors.newSingleThreadExecutor().submit(() -> demoService.callerOne()).get());
|
assertEquals("mock_special", Executors.newSingleThreadExecutor().submit(() -> demoService.callerOne()).get());
|
||||||
verify("callFromDifferentMethod").times(2);
|
verify("callFromDifferentMethod").withTimes(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -71,22 +71,22 @@ internal class DemoMockServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
fun should_able_to_mock_common_method() {
|
fun should_able_to_mock_common_method() {
|
||||||
assertEquals("trim_string__sub_string__false", demoService.commonFunc())
|
assertEquals("trim_string__sub_string__false", demoService.commonFunc())
|
||||||
verify("trim").times(1)
|
verify("trim").withTimes(1)
|
||||||
verify("sub").times(1)
|
verify("sub").withTimes(1)
|
||||||
verify("startsWith").times(1)
|
verify("startsWith").withTimes(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun should_able_to_mock_static_method() {
|
fun should_able_to_mock_static_method() {
|
||||||
assertEquals("White_not_secret_box", demoService.getBox().get())
|
assertEquals("White_not_secret_box", demoService.getBox().get())
|
||||||
verify("secretBox").times(1)
|
verify("secretBox").withTimes(1)
|
||||||
verify("createBox").times(1)
|
verify("createBox").withTimes(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun should_able_to_mock_override_method() {
|
fun should_able_to_mock_override_method() {
|
||||||
val box = demoService.putBox() as BlackBox
|
val box = demoService.putBox() as BlackBox
|
||||||
verify("put").times(1)
|
verify("put").withTimes(1)
|
||||||
assertEquals("put_data_mocked", box.get())
|
assertEquals("put_data_mocked", box.get())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ internal class DemoMockServiceTest {
|
|||||||
assertEquals("mock_one_mock_others", Executors.newSingleThreadExecutor().submit<String> {
|
assertEquals("mock_one_mock_others", Executors.newSingleThreadExecutor().submit<String> {
|
||||||
demoService.callerOne() + "_" + demoService.callerTwo()
|
demoService.callerOne() + "_" + demoService.callerTwo()
|
||||||
}.get())
|
}.get())
|
||||||
verify("callFromDifferentMethod").times(4)
|
verify("callFromDifferentMethod").withTimes(4)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -109,6 +109,6 @@ internal class DemoMockServiceTest {
|
|||||||
assertEquals("mock_special", Executors.newSingleThreadExecutor().submit<String> {
|
assertEquals("mock_special", Executors.newSingleThreadExecutor().submit<String> {
|
||||||
demoService.callerOne()
|
demoService.callerOne()
|
||||||
}.get())
|
}.get())
|
||||||
verify("callFromDifferentMethod").times(2)
|
verify("callFromDifferentMethod").withTimes(2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,8 +41,8 @@ class PathUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
fun should_able_to_mock_java_method_invoke_in_kotlin() {
|
fun should_able_to_mock_java_method_invoke_in_kotlin() {
|
||||||
PathUtil.deleteRecursively(File("/a/b/"))
|
PathUtil.deleteRecursively(File("/a/b/"))
|
||||||
verify("listFiles").times(2)
|
verify("listFiles").withTimes(2)
|
||||||
verify("delete").times(4)
|
verify("delete").withTimes(4)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,35 +16,79 @@ public class InvokeVerifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public InvokeVerifier with(Object arg1) {
|
public InvokeVerifier with(Object arg1) {
|
||||||
with(new Object[]{arg1});
|
return with(new Object[]{arg1});
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public InvokeVerifier with(Object arg1, Object arg2) {
|
public InvokeVerifier with(Object arg1, Object arg2) {
|
||||||
with(new Object[]{arg1, arg2});
|
return with(new Object[]{arg1, arg2});
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public InvokeVerifier with(Object arg1, Object arg2, Object arg3) {
|
public InvokeVerifier with(Object arg1, Object arg2, Object arg3) {
|
||||||
with(new Object[]{arg1, arg2, arg3});
|
return with(new Object[]{arg1, arg2, arg3});
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public InvokeVerifier with(Object arg1, Object arg2, Object arg3, Object arg4) {
|
public InvokeVerifier with(Object arg1, Object arg2, Object arg3, Object arg4) {
|
||||||
with(new Object[]{arg1, arg2, arg3, arg4});
|
return with(new Object[]{arg1, arg2, arg3, arg4});
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public InvokeVerifier with(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) {
|
public InvokeVerifier with(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) {
|
||||||
with(new Object[]{arg1, arg2, arg3, arg4, arg5});
|
return with(new Object[]{arg1, arg2, arg3, arg4, arg5});
|
||||||
return this;
|
}
|
||||||
|
|
||||||
|
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) {
|
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()) {
|
if (records.isEmpty()) {
|
||||||
throw new VerifyFailedError("has not more invoke");
|
throw new VerifyFailedError("has not more invoke");
|
||||||
}
|
}
|
||||||
Object[] record = records.get(0);
|
Object[] record = records.get(order);
|
||||||
if (record.length != args.length) {
|
if (record.length != args.length) {
|
||||||
throw new VerifyFailedError(desc(args), desc(record));
|
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));
|
throw new VerifyFailedError("parameter " + (i + 1) + " mismatched", desc(args), desc(record));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
records.remove(0);
|
records.remove(order);
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String desc(Object[] args) {
|
private String desc(Object[] args) {
|
||||||
@ -72,11 +115,4 @@ public class InvokeVerifier {
|
|||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public InvokeVerifier times(int expectedCount) {
|
|
||||||
if (expectedCount != records.size()) {
|
|
||||||
throw new VerifyFailedError("times: " + records.size(), "times: " + expectedCount);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user