mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-10 20:30:11 +08:00
quick check several invocation with same parameters
This commit is contained in:
parent
5b7382e9b0
commit
6454ae547e
@ -0,0 +1,16 @@
|
|||||||
|
package com.alibaba.testable.core.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author flin
|
||||||
|
*/
|
||||||
|
public class Verification {
|
||||||
|
|
||||||
|
public Object[] parameters;
|
||||||
|
|
||||||
|
public boolean inOrder;
|
||||||
|
|
||||||
|
public Verification(Object[] parameters, boolean inOrder) {
|
||||||
|
this.parameters = parameters;
|
||||||
|
this.inOrder = inOrder;
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,9 @@
|
|||||||
package com.alibaba.testable.core.tool;
|
package com.alibaba.testable.core.tool;
|
||||||
|
|
||||||
import com.alibaba.testable.core.error.VerifyFailedError;
|
import com.alibaba.testable.core.error.VerifyFailedError;
|
||||||
|
import com.alibaba.testable.core.model.Verification;
|
||||||
|
|
||||||
|
import java.security.InvalidParameterException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -10,6 +12,7 @@ import java.util.List;
|
|||||||
public class InvokeVerifier {
|
public class InvokeVerifier {
|
||||||
|
|
||||||
private final List<Object[]> records;
|
private final List<Object[]> records;
|
||||||
|
private Verification lastVerification = null;
|
||||||
|
|
||||||
public InvokeVerifier(List<Object[]> records) {
|
public InvokeVerifier(List<Object[]> records) {
|
||||||
this.records = records;
|
this.records = records;
|
||||||
@ -55,6 +58,10 @@ public class InvokeVerifier {
|
|||||||
return withInOrder(new Object[]{arg1, arg2, arg3, arg4, arg5});
|
return withInOrder(new Object[]{arg1, arg2, arg3, arg4, arg5});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expect mock method invoked with specified parameters
|
||||||
|
* @param args parameters to compare
|
||||||
|
*/
|
||||||
public InvokeVerifier with(Object[] args) {
|
public InvokeVerifier with(Object[] args) {
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
for (int i = 0; i < records.size(); i++) {
|
for (int i = 0; i < records.size(); i++) {
|
||||||
@ -69,18 +76,50 @@ public class InvokeVerifier {
|
|||||||
if (!found) {
|
if (!found) {
|
||||||
throw new VerifyFailedError("has not invoke with " + desc(args));
|
throw new VerifyFailedError("has not invoke with " + desc(args));
|
||||||
}
|
}
|
||||||
|
lastVerification = new Verification(args, false);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expect next mock method call was invoked with specified parameters
|
||||||
|
* @param args parameters to compare
|
||||||
|
*/
|
||||||
public InvokeVerifier withInOrder(Object[] args) {
|
public InvokeVerifier withInOrder(Object[] args) {
|
||||||
withInternal(args, 0);
|
withInternal(args, 0);
|
||||||
|
lastVerification = new Verification(args, true);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expect mock method have been invoked specified times
|
||||||
|
* @param expectedCount times to compare
|
||||||
|
*/
|
||||||
public InvokeVerifier withTimes(int expectedCount) {
|
public InvokeVerifier withTimes(int expectedCount) {
|
||||||
if (expectedCount != records.size()) {
|
if (expectedCount != records.size()) {
|
||||||
throw new VerifyFailedError("times: " + records.size(), "times: " + expectedCount);
|
throw new VerifyFailedError("times: " + records.size(), "times: " + expectedCount);
|
||||||
}
|
}
|
||||||
|
lastVerification = null;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expect several consecutive invocations with the same parameters
|
||||||
|
* @param count number of invocations
|
||||||
|
*/
|
||||||
|
public InvokeVerifier times(int count) {
|
||||||
|
if (count < 2) {
|
||||||
|
throw new InvalidParameterException("should only use times() method with count equal or larger than 2.");
|
||||||
|
} else if (lastVerification == null) {
|
||||||
|
throw new InvalidParameterException("should only use times() after with() or withInOrder() method.");
|
||||||
|
}
|
||||||
|
for (int i = 0; i < count - 1; i++) {
|
||||||
|
if (lastVerification.inOrder) {
|
||||||
|
withInOrder(lastVerification.parameters);
|
||||||
|
} else {
|
||||||
|
with(lastVerification.parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lastVerification = null;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user