How to use describeTo method of org.jmock.test.unit.support.MockAction class

Best Jmock-library code snippet using org.jmock.test.unit.support.MockAction.describeTo

Source:InvocationExpectationTests.java Github

copy

Full Screen

...29 assertTrue("expected " + expected + ", was " + actual,30 equalTo(expected).matches(actual));31 return result;32 }33 public void describeTo(Description description) {34 }35 };36 }37 38 public void testMatchesAnythingByDefault() {39 assertTrue("should match", expectation.matches(40 new Invocation(new Object(), methodFactory.newMethod("method"), Invocation.NO_PARAMETERS)));41 assertTrue("should match", expectation.matches(42 new Invocation(new Object(), methodFactory.newMethod("anotherMethod"), 43 new Object[]{1,2,3,4})));44 }45 46 public void testCanConstrainTargetObject() {47 Object anotherObject = "anotherObject";48 49 expectation.setObjectMatcher(sameInstance(targetObject));50 51 assertTrue("should match", expectation.matches(new Invocation(targetObject, method, Invocation.NO_PARAMETERS)));52 assertTrue("should not match", !expectation.matches(new Invocation(anotherObject, method, Invocation.NO_PARAMETERS)));53 }54 55 public void testCanConstrainMethod() {56 Method anotherMethod = methodFactory.newMethod("anotherMethod");57 58 expectation.setMethodMatcher(equalTo(method));59 60 assertTrue("should match", expectation.matches(new Invocation(targetObject, method, Invocation.NO_PARAMETERS)));61 assertTrue("should not match", !expectation.matches(new Invocation(targetObject, anotherMethod, Invocation.NO_PARAMETERS)));62 }63 64 public void testCanConstrainArguments() {65 Object[] args = {1,2,3,4};66 Object[] differentArgs = {5,6,7,8};67 Object[] differentArgCount = {1,2,3};68 Object[] noArgs = null;69 70 expectation.setParametersMatcher(new AllParametersMatcher(args));71 72 assertTrue("should match", expectation.matches(new Invocation(targetObject, method, args)));73 assertTrue("should not match", !expectation.matches(new Invocation(targetObject, method, differentArgs)));74 assertTrue("should not match", !expectation.matches(new Invocation(targetObject, method, differentArgCount)));75 assertTrue("should not match", !expectation.matches(new Invocation(targetObject, method, noArgs)));76 }77 78 public void testDoesNotMatchIfMatchingCountMatcherDoesNotMatch() throws Throwable {79 Invocation invocation = new Invocation("targetObject", methodFactory.newMethod("method"), Invocation.NO_PARAMETERS);80 81 int maxInvocationCount = 3;82 83 expectation.setCardinality(new Cardinality(0, maxInvocationCount));84 85 int i;86 for (i = 0; i < maxInvocationCount; i++) {87 assertTrue("should match after " + i +" invocations", expectation.matches(invocation));88 expectation.invoke(invocation);89 }90 assertFalse("should not match after " + i + " invocations", expectation.matches(invocation));91 }92 93 public void testMustMeetTheRequiredInvocationCountButContinuesToMatch() throws Throwable {94 Invocation invocation = new Invocation("targetObject", methodFactory.newMethod("method"));95 96 int requiredInvocationCount = 3;97 expectation.setCardinality(new Cardinality(requiredInvocationCount, Integer.MAX_VALUE));98 99 int i;100 for (i = 0; i < requiredInvocationCount; i++) {101 assertTrue("should match after " + i +" invocations", 102 expectation.matches(invocation));103 assertFalse("should not be satisfied after " + i +" invocations",104 expectation.isSatisfied());105 106 expectation.invoke(invocation);107 }108 assertTrue("should match after " + i +" invocations", 109 expectation.matches(invocation));110 assertTrue("should be satisfied after " + i +" invocations",111 expectation.isSatisfied());112 }113 public void testPerformsActionWhenInvoked() throws Throwable {114 final Method stringReturningMethod = methodFactory.newMethod("tester", new Class[0], String.class, new Class[0]);115 Invocation invocation = new Invocation(targetObject, stringReturningMethod, Invocation.NO_PARAMETERS);116 MockAction action = new MockAction();117 118 action.expectInvoke = true;119 action.expectedInvocation = invocation;120 action.result = "result";121 122 expectation.setAction(action);123 124 Object actualResult = expectation.invoke(invocation);125 126 assertSame("actual result", action.result, actualResult);127 assertTrue("action1 was invoked", action.wasInvoked);128 }129 130 public void testPerformsSideEffectsWhenInvoked() throws Throwable {131 FakeSideEffect sideEffect1 = new FakeSideEffect();132 FakeSideEffect sideEffect2 = new FakeSideEffect();133 134 expectation.addSideEffect(sideEffect1);135 expectation.addSideEffect(sideEffect2);136 137 Invocation invocation = new Invocation("targetObject", methodFactory.newMethod("method"));138 expectation.invoke(invocation);139 140 assertTrue("side effect 1 should have been performed", sideEffect1.wasPerformed);141 assertTrue("side effect 2 should have been performed", sideEffect2.wasPerformed);142 }143 144 public void testDescriptionIncludesSideEffects() {145 FakeSideEffect sideEffect1 = new FakeSideEffect();146 FakeSideEffect sideEffect2 = new FakeSideEffect();147 148 sideEffect1.descriptionText = "side-effect-1";149 sideEffect2.descriptionText = "side-effect-2";150 151 expectation.addSideEffect(sideEffect1);152 expectation.addSideEffect(sideEffect2);153 154 String description = StringDescription.toString(expectation);155 156 AssertThat.stringIncludes("should include description of sideEffect1",157 sideEffect1.descriptionText, description);158 AssertThat.stringIncludes("should include description of sideEffect2",159 sideEffect2.descriptionText, description);160 161 }162 163 public void testReturnsNullIfHasNoActionsWhenInvoked() throws Throwable {164 Invocation invocation = new Invocation(targetObject, method, Invocation.NO_PARAMETERS);165 166 Object actualResult = expectation.invoke(invocation);167 168 assertNull("should have returned null", actualResult);169 }170 171 public void testFailsIfActionReturnsAnIncompatibleValue() throws Throwable {172 final Method stringReturningMethod = methodFactory.newMethod("tester", new Class[0], String.class, new Class[0]);173 Invocation invocation = new Invocation(targetObject, stringReturningMethod, Invocation.NO_PARAMETERS);174 ReturnValueAction action = new ReturnValueAction(new Integer(666));175 expectation.setAction(action);176 177 try {178 expectation.invoke(invocation);179 fail("Should have thrown an IllegalStateException");180 } catch (IllegalStateException expected) {181 AssertThat.stringIncludes("Shows returned type", "java.lang.Integer", expected.getMessage());182 AssertThat.stringIncludes("Shows expected return type", "java.lang.String", expected.getMessage());183 }184 }185 186 /**187 * @see CardinalityTests.testHasARequiredAndMaximumNumberOfExpectedInvocations188 */189 public void testHasARequiredAndMaximumNumberOfExpectedInvocations() throws Throwable {190 Invocation invocation = new Invocation(targetObject, method, Invocation.NO_PARAMETERS);191 192 expectation.setCardinality(new Cardinality(1, 1));193 194 assertTrue(expectation.allowsMoreInvocations());195 assertFalse(expectation.isSatisfied());196 197 expectation.invoke(invocation);198 expectation.invoke(invocation);199 200 assertFalse(expectation.allowsMoreInvocations());201 assertTrue(expectation.isSatisfied());202 }203 204 public void testMatchesIfAllOrderingConstraintsMatch() {205 FakeOrderingConstraint orderingConstraint1 = new FakeOrderingConstraint();206 FakeOrderingConstraint orderingConstraint2 = new FakeOrderingConstraint();207 208 expectation.addOrderingConstraint(orderingConstraint1);209 expectation.addOrderingConstraint(orderingConstraint2);210 211 Invocation invocation = new Invocation(targetObject, method, Invocation.NO_PARAMETERS);212 213 orderingConstraint1.allowsInvocationNow = true;214 orderingConstraint2.allowsInvocationNow = true;215 assertTrue(expectation.matches(invocation));216 217 orderingConstraint1.allowsInvocationNow = true;218 orderingConstraint2.allowsInvocationNow = false;219 assertFalse(expectation.matches(invocation));220 221 orderingConstraint1.allowsInvocationNow = false;222 orderingConstraint2.allowsInvocationNow = true;223 assertFalse(expectation.matches(invocation));224 225 orderingConstraint1.allowsInvocationNow = false;226 orderingConstraint2.allowsInvocationNow = false;227 assertFalse(expectation.matches(invocation));228 }229 230 public void testDescriptionIncludesCardinality() {231 final Cardinality cardinality = new Cardinality(2, 2);232 expectation.setCardinality(cardinality);233 234 AssertThat.stringIncludes("should include cardinality description",235 StringDescription.toString(cardinality), 236 StringDescription.toString(expectation));237 }238 239 public void testDescribesNumberOfInvocationsReceived() throws Throwable {240 Invocation invocation = new Invocation(targetObject, method, Invocation.NO_PARAMETERS);241 242 expectation.setCardinality(new Cardinality(2,3));243 244 AssertThat.stringIncludes("should describe as not invoked",245 "never invoked", StringDescription.toString(expectation));246 247 expectation.invoke(invocation);248 AssertThat.stringIncludes("should describe as not invoked",249 "invoked 1 time", StringDescription.toString(expectation));250 251 expectation.invoke(invocation);252 expectation.invoke(invocation);253 AssertThat.stringIncludes("should describe as not invoked",254 "invoked 3 times", StringDescription.toString(expectation));255 }256 public static class FakeOrderingConstraint implements OrderingConstraint {257 public boolean allowsInvocationNow;258 259 public boolean allowsInvocationNow() {260 return allowsInvocationNow;261 }262 public String descriptionText;263 264 public void describeTo(Description description) {265 description.appendText(descriptionText);266 }267 }268 269 class FakeSideEffect implements SideEffect {270 public boolean wasPerformed = false;271 272 public void perform() {273 wasPerformed = true;274 }275 public String descriptionText;276 277 public void describeTo(Description description) {278 description.appendText(descriptionText);279 }280 }281}...

Full Screen

Full Screen

Source:MockAction.java Github

copy

Full Screen

...36 else {37 return result;38 }39 }40 public void describeTo(Description description) {41 description.appendText(descriptionText);42 }43}...

Full Screen

Full Screen

describeTo

Using AI Code Generation

copy

Full Screen

1import org.jmock.MockObjectTestCase;2import org.jmock.core.Invocation;3import org.jmock.core.Stub;4import org.jmock.core.constraint.IsEqual;5import org.jmock.core.constraint.IsInstanceOf;6import org.jmock.core.constraint.IsAnything;7import org.jmock.core.constraint.IsSame;8import org.jmock.core.constraint.IsCollectionContaining;9import org.jmock.core.constraint.IsIn;10import org.jmock.core.constraint.IsNull;11import org.jmock.core.constraint.IsStringStarting;12import org.jmock.core.constraint.IsStringEnding;13import org.jmock.core.constraint.IsStringContaining;14import org.jmock.core.constraint.IsLessThan;15import org.jmock.core.constraint.IsGreaterThan;16import org.jmock.core.constraint.IsLessThanOrEqualTo;17import org.jmock.core.constraint.IsGreaterThanOrEqualTo;18import org.jmock.core.constraint.IsNot;19import org.jmock.core.constraint.IsTypeCompatibleWith;20import org.jmock.core.constraint.IsCompatibleType;21import org.jmock.core.constraint.IsArrayContaining;22import org.jmock.core.constraint.IsArrayContainingInOrder;23import org.jmock.core.constraint.IsArrayWithSize;24import org.jmock.core.constraint.IsCollectionContainingInOrder;25import org.jmock.core.constraint.IsCollectionWithSize;26import org.jmock.core.constraint.IsMapContaining;27import org.jmock.core.constraint.IsMapContainingKey;28import org.jmock.core.constraint.IsMapContainingValue;29import org.jmock.core.constraint.IsMapWithSize;30import org.jmock.core.constraint.IsStringMatching;31import org.jmock.core.constraint.IsSame;32import org.jmock.core.constraint.IsRegExp;

Full Screen

Full Screen

describeTo

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.unit.support;2import org.jmock.core.Invocation;3import org.jmock.core.InvocationMatcher;4import org.jmock.core.InvocationStub;5import org.jmock.core.Stub;6import org.jmock.core.stub.ReturnStub;7import org.jmock.core.stub.ThrowStub;8import org.jmock.core.stub.ThrowStub;9import org.jmock.test.unit.support.MockAction;10import org.jmock.test.unit.support.MockBuilder;11import org.jmock.test.unit.support.MockObjectTestCase;12import org.jmock.test.unit.support.MockStub;13public class MockActionTest extends MockObjectTestCase {14 MockAction action = new MockAction();15 Invocation invocation = new Invocation("INVOKED-OBJECT", "INVOKED-METHOD",16 new Object[0], new Class[0]);17 public void testReturnsStubWithSpecifiedReturnValue() {18 Object returnValue = new Object();19 Stub expectedStub = new ReturnStub(returnValue);20 action.setStub(expectedStub);21 Stub actualStub = action.newStub(invocation);22 assertEquals("should return stub", expectedStub, actualStub);23 }24 public void testReturnsStubWithSpecifiedException() {25 Throwable throwable = new Throwable();26 Stub expectedStub = new ThrowStub(throwable);27 action.setStub(expectedStub);28 Stub actualStub = action.newStub(invocation);29 assertEquals("should return stub", expectedStub, actualStub);30 }31 public void testReturnsStubWithSpecifiedStub() {32 Stub expectedStub = new MockStub();33 action.setStub(expectedStub);34 Stub actualStub = action.newStub(invocation);35 assertEquals("should return stub", expectedStub, actualStub);36 }37 public void testReturnsStubWithSpecifiedStubAndMatcher() {38 Stub expectedStub = new MockStub();39 InvocationMatcher expectedMatcher = new MockMatcher();40 action.setStub(expectedStub, expectedMatcher);41 Stub actualStub = action.newStub(invocation);42 assertEquals("should return stub", expectedStub, actualStub);43 }44 public void testDescribesStubWithSpecifiedReturnValue() {45 Object returnValue = new Object();46 Stub expectedStub = new ReturnStub(returnValue);47 action.setStub(expectedStub);48 action.newStub(invocation);49 assertEquals("should describe stub", "return " + returnValue, action50 .describeTo(new StringBuffer()).toString());51 }52 public void testDescribesStubWithSpecifiedException() {

Full Screen

Full Screen

describeTo

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.unit.support;2import org.jmock.core.Action;3import org.jmock.core.Invocation;4import org.jmock.core.stub.Stub;5import org.jmock.test.unit.support.MockAction;6public class MockActionTest extends org.jmock.test.unit.AbstractMockObjectTestCase {7 MockAction action = new MockAction();8 Invocation invocation = null;9 Stub stub = null;10 Action action1 = null;11 public void testDescribeTo() {12 action.describeTo(new StringBuffer());13 }14 public void testInvoke() {15 action.invoke(invocation);16 }17 public void testNext() {18 action.next();19 }20 public void testSetNext() {21 action.setNext(stub);22 }23 public void testSetNextAction() {24 action.setNextAction(action1);25 }26 public void testVerify() {27 action.verify();28 }29}

Full Screen

Full Screen

describeTo

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.unit.support;2import org.jmock.core.Invocation;3import org.jmock.core.Stub;4import org.jmock.core.stub.StubSequence;5public class MockAction implements Stub {6 private String description;7 public MockAction(String description) {8 this.description = description;9 }10 public StringBuffer describeTo(StringBuffer buffer) {11 return buffer.append(description);12 }13 public Object invoke(Invocation invocation) throws Throwable {14 return null;15 }16}17package org.jmock.test.unit.support;18import junit.framework.TestCase;19import org.jmock.core.Invocation;20import org.jmock.core.Stub;21import org.jmock.core.stub.StubSequence;22public class MockActionTest extends TestCase {23 public void testDescribesItself() {24 MockAction action = new MockAction("mock action");25 assertEquals("mock action", action.describeTo(new StringBuffer()).toString());26 }27}28package org.jmock.test.unit.support;29import junit.framework.TestCase;30import org.jmock.core.Invocation;31import org.jmock.core.Stub;32import org.jmock.core.stub.StubSequence;33public class MockActionTest extends TestCase {34 public void testDescribesItself() {35 MockAction action = new MockAction("mock action");36 assertEquals("mock action", action.describeTo(new StringBuffer()).toString());37 }38}39package org.jmock.test.unit.support;40import junit.framework.TestCase;41import org.jmock.core.Invocation;42import org.jmock.core.Stub;43import org.jmock.core.stub.StubSequence;44public class MockActionTest extends TestCase {45 public void testDescribesItself() {46 MockAction action = new MockAction("mock action");47 assertEquals("mock action", action.describeTo(new StringBuffer()).toString());48 }49}50package org.jmock.test.unit.support;51import junit.framework.TestCase;52import org.jmock.core.Invocation;53import org.jmock.core.Stub;54import org.jmock.core.stub.StubSequence;

Full Screen

Full Screen

describeTo

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.unit.support;2import org.jmock.Mock;3import org.jmock.MockObjectTestCase;4import org.jmock.core.Invocation;5import org.jmock.core.Stub;6import org.jmock.util.Verifier;7public class MockActionTest extends MockObjectTestCase {8 public void testDescribeTo() {9 Mock mock = mock(Stub.class);10 mock.stubs().method("invoke").will(returnValue("Hello World"));11 Invocation invocation = new Invocation("invoke", new Object[0], 0);12 String description = ((Stub) mock.proxy()).invoke(invocation);13 assertEquals("Hello World", description);14 Verifier.describeTo(description, "Hello World");15 }16}17java -cp .;jmock.jar;hamcrest.jar org.jmock.test.unit.support.MockActionTest18OK (1 test)

Full Screen

Full Screen

describeTo

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.unit.support;2import org.jmock.api.Action;3import org.jmock.api.Invocation;4import org.jmock.lib.action.ReturnValueAction;5public class MockAction implements Action {6 private final String description;7 public MockAction(String description) {8 this.description = description;9 }10 public Object invoke(Invocation invocation) throws Throwable {11 return null;12 }13 public void describeTo(Description description) {14 description.appendText(this.description);15 }16 public static Action returnValue(Object value, String description) {17 return new ReturnValueAction(value, new MockAction(description));18 }19}20package org.jmock.test.unit;21import static org.jmock.Expectations.returnValue;22import static org.jmock.test.unit.support.MockAction.returnValue;23import org.jmock.Mockery;24import org.jmock.integration.junit4.JUnitRuleMockery;25import org.jmock.test.unit.support.MockAction;26import org.junit.Rule;27import org.junit.Test;28public class MockActionTest {29 @Rule public Mockery context = new JUnitRuleMockery();30 public void describesReturnValueActionWithDescription() {31 MockAction returnValueAction = returnValue(42, "return 42");32 context.checking(new Expectations() {{33 oneOf (mock).doSomething(); will(returnValueAction);34 }});35 mock.doSomething();36 }37}38package org.jmock.test.unit;39import static org.jmock.Expectations.returnValue;40import static org.jmock.test.unit.support.MockAction.returnValue;41import org.jmock.Mockery;42import org.jmock.integration.junit4.JUnitRuleMockery;43import org.jmock.test.unit.support.MockAction;44import org.junit.Rule;45import org.junit.Test;46public class MockActionTest {47 @Rule public Mockery context = new JUnitRuleMockery();48 public void describesReturnValueActionWithDescription() {49 MockAction returnValueAction = returnValue(42, "return 42");50 context.checking(new Expectations() {{

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 Jmock-library automation tests on LambdaTest cloud grid

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

Most used method in MockAction

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful