How to use UnorderedBehavior method of org.easymock.internal.UnorderedBehavior class

Best Easymock code snippet using org.easymock.internal.UnorderedBehavior.UnorderedBehavior

Source:MocksBehavior.java Github

copy

Full Screen

...25public class MocksBehavior implements IMocksBehavior, Serializable {2627 private static final long serialVersionUID = 3265727009370529027L;2829 private final List<UnorderedBehavior> behaviorLists = new ArrayList<UnorderedBehavior>();3031 private final List<ExpectedInvocationAndResult> stubResults = new ArrayList<ExpectedInvocationAndResult>();3233 private final boolean nice;3435 private boolean checkOrder;3637 private boolean isThreadSafe;38 39 private boolean shouldBeUsedInOneThread;4041 private int position = 0;4243 private transient volatile Thread lastThread;44 45 private LegacyMatcherProvider legacyMatcherProvider;4647 public MocksBehavior(boolean nice) {48 this.nice = nice;49 this.isThreadSafe = !Boolean.valueOf(EasyMockProperties.getInstance()50 .getProperty(EasyMock.NOT_THREAD_SAFE_BY_DEFAULT));51 this.shouldBeUsedInOneThread = Boolean.valueOf(EasyMockProperties52 .getInstance().getProperty(53 EasyMock.ENABLE_THREAD_SAFETY_CHECK_BY_DEFAULT));54 }5556 public final void addStub(ExpectedInvocation expected, Result result) {57 stubResults.add(new ExpectedInvocationAndResult(expected, result));58 }5960 public void addExpected(ExpectedInvocation expected, Result result,61 Range count) {62 if (legacyMatcherProvider != null) {63 expected = expected.withMatcher(legacyMatcherProvider64 .getMatcher(expected.getMethod()));65 }66 addBehaviorListIfNecessary(expected);67 lastBehaviorList().addExpected(expected, result, count);68 }6970 private final Result getStubResult(Invocation actual) {71 for (ExpectedInvocationAndResult each : stubResults) {72 if (each.getExpectedInvocation().matches(actual)) {73 return each.getResult();74 }75 }76 return null;77 }7879 private void addBehaviorListIfNecessary(ExpectedInvocation expected) {80 if (behaviorLists.isEmpty()81 || !lastBehaviorList().allowsExpectedInvocation(expected,82 checkOrder)) {83 behaviorLists.add(new UnorderedBehavior(checkOrder));84 }85 }8687 private UnorderedBehavior lastBehaviorList() {88 return behaviorLists.get(behaviorLists.size() - 1);89 }9091 @SuppressWarnings("deprecation")92 public final Result addActual(Invocation actual) {93 int initialPosition = position;94 95 while (position < behaviorLists.size()) {96 Result result = behaviorLists.get(position).addActual(actual);97 if (result != null) {98 return result;99 } 100 if (!behaviorLists.get(position).verify()) {101 break;102 }103 position++;104 }105 Result stubOrNice = getStubResult(actual);106 if (stubOrNice == null && nice) {107 stubOrNice = Result.createReturnResult(RecordState108 .emptyReturnValueFor(actual.getMethod().getReturnType()));109 }110111 int endPosition = position;112 113 // Do not move the cursor in case of stub, nice or error114 position = initialPosition;115116 if (stubOrNice != null) {117 actual.validateCaptures();118 actual.clearCaptures();119 return stubOrNice;120 }121 122 // Case where the loop was exited at the end of the behaviorLists123 if (endPosition == behaviorLists.size()) {124 endPosition--;125 } 126 127 // Loop all around the behaviors left to generate the message128 StringBuilder errorMessage = new StringBuilder(70 * (endPosition129 - initialPosition + 1)); // rough approximation of the length130 errorMessage.append("\n Unexpected method call ").append(131 actual.toString(org.easymock.MockControl.EQUALS_MATCHER));132 133 List<ErrorMessage> messages = new ArrayList<ErrorMessage>();134 135 int matches = 0;136 137 // First find how many match we have138 for (int i = initialPosition; i <= endPosition; i++) {139 List<ErrorMessage> thisListMessages = behaviorLists.get(i)140 .getMessages(actual);141 messages.addAll(thisListMessages);142 for (ErrorMessage m : thisListMessages) {143 if (m.isMatching()) {144 matches++;145 }146 }147 }148 149 if (matches > 1) {150 errorMessage151 .append(". Possible matches are marked with (+1):");152 } else {153 errorMessage.append(":");154 }155156 for (ErrorMessage m : messages) {157 m.appendTo(errorMessage, matches);158 }159 160 // And finally throw the error161 throw new AssertionErrorWrapper(new AssertionError(errorMessage));162 }163164 public void verify() {165 boolean verified = true;166 167 for (UnorderedBehavior behaviorList : behaviorLists.subList(position,168 behaviorLists.size())) {169 if (!behaviorList.verify()) {170 verified = false;171 }172 }173 if (verified) {174 return;175 }176 177 StringBuilder errorMessage = new StringBuilder(70 * (behaviorLists178 .size()179 - position + 1));180181 errorMessage.append("\n Expectation failure on verify:");182 for (UnorderedBehavior behaviorList : behaviorLists.subList(position,183 behaviorLists.size())) {184 for (ErrorMessage m : behaviorList.getMessages(null)) {185 m.appendTo(errorMessage, 0);186 }187 }188 189 throw new AssertionErrorWrapper(new AssertionError(errorMessage190 .toString()));191 }192193 public void checkOrder(boolean value) {194 this.checkOrder = value;195 }196 ...

Full Screen

Full Screen

UnorderedBehavior

Using AI Code Generation

copy

Full Screen

1public class UnorderedBehavior {2 private final List<Method> methods = new ArrayList<Method>();3 private final List<Object[]> arguments = new ArrayList<Object[]>();4 private final List<Method> replayedMethods = new ArrayList<Method>();5 private final List<Object[]> replayedArguments = new ArrayList<Object[]>();6 private final List<Throwable> errors = new ArrayList<Throwable>();7 private int nextIndex;8 private boolean replaying;9 public UnorderedBehavior() {10 this.nextIndex = 0;11 }12 public synchronized void addExpected(Method method, Object[] arguments) {13 methods.add(method);14 this.arguments.add(arguments);15 }16 public synchronized void addActual(Method method, Object[] arguments) {17 if (replaying) {18 replayedMethods.add(method);19 replayedArguments.add(arguments);20 } else {21 if (nextIndex < methods.size()) {22 Method expectedMethod = methods.get(nextIndex);23 Object[] expectedArguments = this.arguments.get(nextIndex);24 if (expectedMethod.equals(method)25 && Arrays.equals(expectedArguments, arguments)) {26 nextIndex++;27 return;28 }29 }30 errors.add(new UnexpectedInvocation(method, arguments));31 }32 }33 public synchronized void verify() {34 if (nextIndex < methods.size()) {35 Method expectedMethod = methods.get(nextIndex);36 Object[] expectedArguments = arguments.get(nextIndex);37 errors.add(new MissingInvocation(expectedMethod, expectedArguments));38 }39 if (errors.size() > 0) {40 throw new AssertionError(createErrorMessage());41 }42 }43 public synchronized void replay() {44 replaying = true;45 }46 private String createErrorMessage() {47 StringBuffer buffer = new StringBuffer();48 for (Throwable error : errors) {49 buffer.append(error.getMessage());50 buffer.append("51");52 }53 return buffer.toString();54 }55 public void verifyState() {56 if (replaying) {57 replayedMethods.clear();58 replayedArguments.clear();59 } else {60 if (nextIndex < methods.size()) {61 Method expectedMethod = methods.get(nextIndex);62 Object[] expectedArguments = arguments.get(nextIndex);63 errors.add(new MissingInvocation(expectedMethod, expectedArguments));64 }65 if (errors.size() > 0) {66 throw new AssertionError(createErrorMessage());67 }68 }69 }70}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Easymock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful