How to use Same class of org.easymock.internal.matchers package

Best Easymock code snippet using org.easymock.internal.matchers.Same

Source:LenientMocksControl.java Github

copy

Full Screen

1/*2 * Copyright 2008, Unitils.org3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package org.unitils.easymock.util;17import java.util.List;18import org.easymock.IAnswer;19import org.easymock.IArgumentMatcher;20import org.easymock.internal.IMocksControlState;21import org.easymock.internal.Invocation;22import org.easymock.internal.LastControl;23import org.easymock.internal.MocksControl;24import org.easymock.internal.Range;25import org.easymock.internal.RecordState;26import org.unitils.reflectionassert.ReflectionComparatorMode;27/**28 * An EasyMock mock control that uses the reflection argument matcher for all arguments of a method invocation.29 * <p/>30 * No explicit argument matcher setting is needed (or allowed). This control will automatically report31 * lenient reflection argument matchers. These matchers can apply some leniency when comparing expected and actual32 * argument values.33 * <p/>34 * Setting the {@link ReflectionComparatorMode#IGNORE_DEFAULTS} mode will for example ignore all fields that35 * have default values as expected values. E.g. if a null value is recorded as argument it will not be checked when36 * the actual invocation occurs. The same applies for inner-fields of object arguments that contain default java values.37 * <p/>38 * Setting the {@link ReflectionComparatorMode#LENIENT_DATES} mode will ignore the actual date values of arguments and39 * inner fields of arguments. It will only check whether both dates are null or both dates are not null. The actual40 * date and hour do not matter.41 * <p/>42 * Setting the {@link ReflectionComparatorMode#LENIENT_ORDER} mode will ignore the actual order of collections and43 * arrays arguments and inner fields of arguments. It will only check whether they both contain the same elements.44 *45 * @author Tim Ducheyne46 * @author Filip Neven47 * @see ReflectionComparatorMode48 * @see org.unitils.reflectionassert.ReflectionComparator49 */50public class LenientMocksControl extends MocksControl {51 /***/52 private static final long serialVersionUID = -4612378998272988410L;53 /* The interceptor that wraps the record state */54 private InvocationInterceptor invocationInterceptor;55 /**56 * Creates a default (no default returns and no order checking) mock control.57 *58 * @param modes the modes for the reflection argument matcher59 */60 public LenientMocksControl(ReflectionComparatorMode... modes) {61 this(org.easymock.MockType.DEFAULT, modes);62 }63 /**64 * Creates a mock control.<ul>65 * <li>Default mock type: no default return values and no order checking</li>66 * <li>Nice mock type: returns default values if no return value set, no order checking</li>67 * <li>Strict mock type: no default return values and strict order checking</li>68 * </ul>69 *70 * @param type the EasyMock mock type71 * @param modes the modes for the reflection argument matcher72 */73 public LenientMocksControl(org.easymock.MockType type, ReflectionComparatorMode... modes) {74 super(type);75 this.invocationInterceptor = new InvocationInterceptor(modes);76 }77 /**78 * Overriden to be able to replace the record behavior that going to record all method invocations.79 * The interceptor will make sure that reflection argument matchers will be reported for the80 * arguments of all recorded method invocations.81 *82 * @return the state, wrapped in case of a RecordState83 */84 @Override85 public IMocksControlState getState() {86 IMocksControlState mocksControlState = super.getState();87 if (mocksControlState instanceof RecordState) {88 invocationInterceptor.setRecordState((RecordState) mocksControlState);89 return invocationInterceptor;90 }91 return mocksControlState;92 }93 /**94 * A wrapper for the record state in easy mock that will intercept the invoke method95 * so that it can install reflection argument matchers for all arguments of the recorded method invocation.96 * <p/>97 * The old easy mock way of having a single argument matcher for all arguments has been deprecated. Since98 * EasyMock 2 each argument should have its own matcher. We however want to avoid having to set all99 * matchers to the reflection argument matcher explicitly.100 * Because some of the methods are declared final and some classes explicitly cast to subtypes, creating a wrapper101 * seems to be the only way to be able to intercept the matcher behavior.102 */103 private class InvocationInterceptor implements IMocksControlState {104 /* The wrapped record state */105 private RecordState recordState;106 /* The modes for the reflection argument matchers */107 private ReflectionComparatorMode[] modes;108 /**109 * Creates an interceptor that will create reflection argument matchers for all arguments of all recorded110 * method invocations.111 *112 * @param modes the modes for the reflection argument matchers113 */114 public InvocationInterceptor(ReflectionComparatorMode... modes) {115 this.modes = modes;116 }117 /**118 * Sets the current wrapped record state.119 *120 * @param recordState the state, not null121 */122 public void setRecordState(RecordState recordState) {123 this.recordState = recordState;124 }125 /**126 * Overriden to report reflection argument matchers for all arguments of the given method invocation.127 *128 * @param invocation the method invocation, not null129 * @return the result of the invocation130 */131 @Override132 public Object invoke(Invocation invocation) {133 LastControl.reportLastControl(LenientMocksControl.this);134 createMatchers(invocation);135 return recordState.invoke(invocation);136 }137 /**138 * Reports report reflection argument matchers for all arguments of the given method invocation.139 * An exception will be thrown if there were already matchers reported for the invocation.140 *141 * @param invocation the method invocation, not null142 */143 private void createMatchers(Invocation invocation) {144 List<IArgumentMatcher> matchers = LastControl.pullMatchers();145 if (matchers != null && !matchers.isEmpty()) {146 if (matchers.size() != invocation.getArguments().length) {147 throw new IllegalStateException("This mock control does not support mixing of no-argument matchers and per-argument matchers. " +148 "Either no matchers are defined and the reflection argument matcher is used by default or all matchers are defined explicitly (Eg by using refEq()).");149 }150 // put all matchers back since pull removes them151 for (IArgumentMatcher matcher : matchers) {152 LastControl.reportMatcher(matcher);153 }154 return;155 }156 Object[] arguments = invocation.getArguments();157 if (arguments == null) {158 return;159 }160 for (Object argument : arguments) {161 LastControl.reportMatcher(new ReflectionArgumentMatcher<Object>(argument, modes));162 }163 }164 // Pass through delegation165 @Override166 public void assertRecordState() {167 recordState.assertRecordState();168 }169 @Override170 public void andReturn(Object value) {171 recordState.andReturn(value);172 }173 @Override174 public void andThrow(Throwable throwable) {175 recordState.andThrow(throwable);176 }177 @Override178 public void andAnswer(IAnswer<?> answer) {179 recordState.andAnswer(answer);180 }181 @Override182 public void andStubReturn(Object value) {183 recordState.andStubReturn(value);184 }185 @Override186 public void andStubThrow(Throwable throwable) {187 recordState.andStubThrow(throwable);188 }189 @Override190 public void andStubAnswer(IAnswer<?> answer) {191 recordState.andStubAnswer(answer);192 }193 @Override194 public void asStub() {195 recordState.asStub();196 }197 @Override198 public void times(Range range) {199 recordState.times(range);200 }201 @Override202 public void checkOrder(boolean value) {203 recordState.checkOrder(value);204 }205 @Override206 public void replay() {207 recordState.replay();208 }209 @Override210 public void verify() {211 recordState.verify();212 }213 /*public void setDefaultReturnValue(Object value) {214 recordState.setDefaultReturnValue(value);215 }216 public void setDefaultThrowable(Throwable throwable) {217 recordState.setDefaultThrowable(throwable);218 }219 public void setDefaultVoidCallable() {220 recordState.setDefaultVoidCallable();221 }222 public void setDefaultMatcher(ArgumentsMatcher matcher) {223 recordState.setDefaultMatcher(matcher);224 }225 public void setMatcher(Method method, ArgumentsMatcher matcher) {226 recordState.setMatcher(method, matcher);227 }*/228 /**229 * @see org.easymock.internal.IMocksControlState#andDelegateTo(java.lang.Object)230 */231 @Override232 public void andDelegateTo(Object value) {233 recordState.andDelegateTo(value);234 }235 /**236 * @see org.easymock.internal.IMocksControlState#andStubDelegateTo(java.lang.Object)237 */238 @Override239 public void andStubDelegateTo(Object value) {240 recordState.andStubDelegateTo(value);241 }242 /**243 * @see org.easymock.internal.IMocksControlState#checkIsUsedInOneThread(boolean)244 */245 @Override246 public void checkIsUsedInOneThread(boolean value) {247 recordState.checkIsUsedInOneThread(value);248 }249 /**250 * @see org.easymock.internal.IMocksControlState#makeThreadSafe(boolean)251 */252 @Override253 public void makeThreadSafe(boolean value) {254 recordState.makeThreadSafe(value);255 }256 }257}...

Full Screen

Full Screen

Source:RecordStateJavaTest.java Github

copy

Full Screen

...85 EasyMock.verify(capture1);86 EasyMock.verify(capture2);87 }88 89 public void testReportCapture_SameTwice() throws IllegalStateExceptionWrapper {90 EasyMock.replay(behavior);91 92 ArgumentCapture capture = EasyMock.createMock(ArgumentCapture.class);93 EasyMock.replay(capture);94 95 recordState.reportCapture(capture);96 97 try {98 recordState.reportCapture(capture);99 } catch (IllegalStateExceptionWrapper expected) {100 }101 102 EasyMock.verify(behavior);103 EasyMock.verify(capture);104 }105 106 public void testCheckCanSwitchToReplay_noCurrentSetter() {107 EasyMock.replay(behavior);108 109 assertNull(recordState.currentSetter);110 recordState.checkCanSwitchToReplay();111 assertNull(recordState.currentSetter);112 113 EasyMock.verify(behavior);114 }115 116 public void testCheckCanSwitchToReplay_withCurrentSetter() {117 EasyMock.replay(behavior);118 119 ExpectationSettersImpl setter = EasyMock.createMock(ExpectationSettersImpl.class);120 setter.retire();121 EasyMock.replay(setter);122 123 recordState.currentSetter = setter;124 recordState.checkCanSwitchToReplay();125 assertNull(recordState.currentSetter);126 127 EasyMock.verify(setter);128 EasyMock.verify(behavior);129 }130 131 public void testGetExpectationSetter_noSetter() {132 EasyMock.replay(behavior);133 assertNull(recordState.currentSetter);134 135 try {136 recordState.getExpectationSetter();137 } catch (IllegalStateExceptionWrapper expected) {138 }139 EasyMock.verify(behavior);140 }141 142 public void testGetExpectationSetter_withSetter() throws IllegalStateExceptionWrapper {143 EasyMock.replay(behavior);144 145 ExpectationSettersImpl setter = EasyMock.createMock(ExpectationSettersImpl.class);146 EasyMock.replay(setter);147 148 recordState.currentSetter = setter;149 assertSame(setter, recordState.getExpectationSetter());150 151 EasyMock.verify(setter);152 EasyMock.verify(behavior);153 }154 155 public void testInvoke() throws Throwable {156 EasyMock.replay(behavior);157 Call call = EasyMock.createMock(Call.class);158 EasyMock.expect(call.getDefaultReturnValue()).andReturn(0);159 EasyMock.expect(call.getArguments()).andReturn(new ArrayList<Object>());160 EasyMock.replay(call);161 162 ExpectationSettersImpl setter = recordState.currentSetter;163 164 assertEquals(0, recordState.invoke(call).answer(null));165 assertNull(recordState.argumentCaptures);166 assertNull(recordState.argumentMatchers);167 assertNotNull(recordState.currentSetter);168 assertNotSame(setter, recordState.currentSetter);169 170 EasyMock.verify(behavior);171 EasyMock.verify(call);172 }173 174 public void testInvoke_retirePrevious() {175 EasyMock.replay(behavior);176 Call call = EasyMock.createMock(Call.class);177 EasyMock.expect(call.getDefaultReturnValue()).andReturn(0);178 EasyMock.expect(call.getArguments()).andReturn(new ArrayList<Object>());179 EasyMock.replay(call);180 181 ExpectationSettersImpl setter = EasyMock.createMock(ExpectationSettersImpl.class);182 setter.retire();183 EasyMock.replay(setter);184 185 recordState.currentSetter = setter;186 187 recordState.invoke(call);188 assertNotSame(setter, recordState.currentSetter);189 190 EasyMock.verify(behavior);191 EasyMock.verify(call);192 EasyMock.verify(setter);193 }194}...

Full Screen

Full Screen

Source:ConstraintsToStringTest.java Github

copy

Full Screen

...20import org.easymock.internal.matchers.Not;21import org.easymock.internal.matchers.NotNull;22import org.easymock.internal.matchers.Null;23import org.easymock.internal.matchers.Or;24import org.easymock.internal.matchers.Same;25import org.easymock.internal.matchers.StartsWith;26import org.junit.Before;27import org.junit.Test;2829public class ConstraintsToStringTest {30 private StringBuffer buffer;3132 @Before33 public void setup() {34 buffer = new StringBuffer();35 }3637 @Test38 public void sameToStringWithString() {39 new Same("X").appendTo(buffer);40 assertEquals("same(\"X\")", buffer.toString());4142 }4344 @Test45 public void nullToString() {46 Null.NULL.appendTo(buffer);47 assertEquals("isNull()", buffer.toString());48 }4950 @Test51 public void notNullToString() {52 NotNull.NOT_NULL.appendTo(buffer);53 assertEquals("notNull()", buffer.toString());54 }5556 @Test57 public void anyToString() {58 Any.ANY.appendTo(buffer);59 assertEquals("<any>", buffer.toString());60 }6162 @Test63 public void sameToStringWithChar() {64 new Same('x').appendTo(buffer);65 assertEquals("same('x')", buffer.toString());66 }6768 @Test69 public void sameToStringWithObject() {70 Object o = new Object() {71 @Override72 public String toString() {73 return "X";74 }75 };76 new Same(o).appendTo(buffer);77 assertEquals("same(X)", buffer.toString());78 }7980 @Test81 public void equalsToStringWithString() {82 new Equals("X").appendTo(buffer);83 assertEquals("\"X\"", buffer.toString());8485 }8687 @Test88 public void equalsToStringWithChar() {89 new Equals('x').appendTo(buffer);90 assertEquals("'x'", buffer.toString()); ...

Full Screen

Full Screen

Same

Using AI Code Generation

copy

Full Screen

1import org.easymock.internal.matchers.Same;2import org.junit.Test;3import static org.easymock.EasyMock.*;4public class SameTest {5 public void testSame(){6 Same s = new Same("abc");7 boolean result = s.matches("abc");8 System.out.println(result);9 }10}

Full Screen

Full Screen

Same

Using AI Code Generation

copy

Full Screen

1import org.easymock.EasyMock;2import org.easymock.internal.matchers.Same;3import org.junit.Test;4import static org.junit.Assert.*;5import static org.easymock.EasyMock.*;6public class Test1 {7 public void test1() {8 ClassToBeTested classToBeTested = new ClassToBeTested();9 Dependency dependency = createMock(Dependency.class);10 classToBeTested.setDependency(dependency);11 Object expected = new Object();12 dependency.method1(same(expected));13 replay(dependency);14 classToBeTested.method1(expected);15 verify(dependency);16 }17}18import org.easymock.EasyMock;19import org.junit.Test;20import static org.junit.Assert.*;21import static org.easymock.EasyMock.*;22public class Test2 {23 public void test2() {24 ClassToBeTested classToBeTested = new ClassToBeTested();25 Dependency dependency = createMock(Dependency.class);26 classToBeTested.setDependency(dependency);27 Object expected = new Object();28 dependency.method1(same(expected));29 replay(dependency);30 classToBeTested.method1(expected);31 verify(dependency);32 }33}34import org.easymock.EasyMock;35import org.junit.Test;36import static org.junit.Assert.*;37import static org.easymock.Easy

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.

Most used methods in Same

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful