How to use describeTo method of org.jmock.test.unit.internal.InvocationExpectationTests class

Best Jmock-library code snippet using org.jmock.test.unit.internal.InvocationExpectationTests.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

describeTo

Using AI Code Generation

copy

Full Screen

1[INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ jmock-library ---2[INFO] [INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ jmock-library ---3[INFO] [INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ jmock-library ---4[INFO] [INFO] --- maven-source-plugin:3.2.1:jar-no-fork (attach-sources) @ jmock-library ---5[INFO] [INFO] --- maven-javadoc-plugin:3.1.1:jar (attach-javadocs) @ jmock-library ---6[INFO] [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ jmock-library ---

Full Screen

Full Screen

describeTo

Using AI Code Generation

copy

Full Screen

1 [junit] at junit.framework.Assert.fail(Assert.java:47)2 [junit] at junit.framework.Assert.failNotEquals(Assert.java:282)3 [junit] at junit.framework.Assert.assertEquals(Assert.java:67)4 [junit] at junit.framework.Assert.assertEquals(Assert.java:74)5 [junit] at org.jmock.test.unit.internal.InvocationExpectationTests.describeTo(InvocationExpectationTests.java:66)6 [junit] at org.jmock.test.unit.internal.InvocationExpectationTests.describeTo(InvocationExpectationTests.java:1)7 [junit] at org.jmock.test.unit.internal.InvocationExpectationTests.testDescribeTo(InvocationExpectationTests.java:57)8 [junit] at org.jmock.test.unit.internal.InvocationExpectationTests.testDescribeTo(InvocationExpectationTests.java:1)9 [junit] at java.lang.reflect.Method.invoke(Native Method)10 [junit] at junit.framework.TestCase.runTest(TestCase.java:154)11 [junit] at junit.framework.TestCase.runBare(TestCase.java:127)12 [junit] at junit.framework.TestResult$1.protect(TestResult.java:106)13 [junit] at junit.framework.TestResult.runProtected(TestResult.java:124)14 [junit] at junit.framework.TestResult.run(TestResult.java:109)15 [junit] at junit.framework.TestCase.run(TestCase.java:118)16 [junit] at junit.framework.TestSuite.runTest(TestSuite.java:208)17 [junit] at junit.framework.TestSuite.run(TestSuite.java:203)18 [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:518)19 [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1096)20 [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:907)

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful