public void testWillAnnounceItHasFinishedIfPriceGoesAboveMaximum() { checking(new Expectations() {{ exactly(1).of (listener).sniperFinished(sniper); }}); sniper.bidAccepted(auction, unbeatableBid); }
public void testCannotSetAnExpectationOnAnObjectThatIsNotAMock() { final ArrayList<String> list = new ArrayList<String>(); try { context.checking(new Expectations() {{ exactly(1).of (list).add("a new element"); }}); fail("should have thrown IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } }
public void testWillLimitBidToMaximum() throws Throwable { checking(new Expectations() {{ exactly(1).of (auction).bid(maximumBid); }}); sniper.bidAccepted(auction, maximumBid.subtract(new Money(1))); }
public void testCanExecuteCommandsThatRepeatWithFixedDelay() { scheduler.scheduleWithFixedDelay(commandA, 2L, 3L, TimeUnit.SECONDS); checking(new Expectations() {{ exactly(3).of(commandA).run(); }}); scheduler.tick(8L, TimeUnit.SECONDS); }
public void testCanExecuteCommandsThatRepeatAtFixedRateButAssumesThatCommandsTakeNoTimeToExecute() { scheduler.scheduleAtFixedRate(commandA, 2L, 3L, TimeUnit.SECONDS); checking(new Expectations() {{ exactly(3).of(commandA).run(); }}); scheduler.tick(8L, TimeUnit.SECONDS); }
public void testCatchesExceptionsAndReportsThemToErrorListener() throws Exception { final AuctionException exception = new AuctionException("test"); checking(new Expectations() {{ allowing (auction).bid(with(any(Money.class))); will(throwException(exception)); exactly(1).of (listener).sniperBidFailed(sniper, exception); }}); sniper.bidAccepted(auction, beatableBid); } }
public void testAllOrNoneOfTheParametersMustBeSpecifiedByMatchers() { try { context.checking(new Expectations() {{ exactly(1).of (mock).doSomethingWithBoth(with(equal("a-matcher")), "not-a-matcher"); }}); } catch (IllegalArgumentException expected) { } }
/** Returns a <code>KeyUpdateHandler</code> that must be called exactly <code>numExpectedCalls</code> times */ public static KeyUpdateHandler createVerifyingKeyUpdateHandler(final int numExpectedCalls) throws GeneralSecurityException, IOException, PasswordException { Mockery mockery = new Mockery(); final KeyUpdateHandler keyUpdateHandler = mockery.mock(KeyUpdateHandler.class); mockery.checking(new Expectations() {{ exactly(numExpectedCalls).of(keyUpdateHandler).updateKey(); }}); return keyUpdateHandler; }
public void testCannotSetAnExpectationWithoutSpecifyingCardinalityAfterPreviousExpectationsWithCardinality() { try { context.checking(new Expectations() {{ exactly(1).of (mock).doSomething(); mock.doSomething(); }}); fail("should have thrown ExpectationError"); } catch (ExpectationError ex) { // expected } }
public void testCannotSetAnExpectationWithoutSpecifyingTheMockObjectAfterOtherExpectations() { try { context.checking(new Expectations() {{ exactly(1).of (mock).doSomething(); exactly(1); }}); fail("should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
public void testCannotSetAnExpectationWithoutSpecifyingTheMockObjectBeforeOtherExpectations() { try { context.checking(new Expectations() {{ exactly(1); exactly(1).of (mock).doSomething(); }}); fail("should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
public void testMockeryCanTranslateExpectationErrorsIntoDifferentExceptionTypeWhenUnexpectedInvocationOccurs() { context.checking(new Expectations() {{ exactly(1).of (mock).method1(); }}); try { mock.method2(); } catch (TranslatedError e) { // expected } catch (ExpectationError e) { fail("should have translated ExpectationError into TranslatedError"); } }
public void testDoesNotAllowUnexpectedParameterValues() { context.checking(new Expectations() {{ exactly(1).of (mock).doSomethingWith(with(equal("hello"))); exactly(1).of (mock).doSomethingWith(with(equal("goodbye"))); }}); try { mock.doSomethingWith("hello"); mock.doSomethingWith("Goodbye"); fail("should have thrown ExpectationError"); } catch (ExpectationError expected) {} }
public void testNotAllExpectationsOfSameMockMustUseMatchers() { context.checking(new Expectations() {{ exactly(1).of (mock).doSomethingWithBoth(with(equal("x")), with(equal("y"))); exactly(1).of (mock).doSomethingWith("z"); }}); mock.doSomethingWithBoth("x", "y"); mock.doSomethingWith("z"); context.assertIsSatisfied(); }
public void testMustSpecifyConstraintsForAllArguments() { try { context.checking(new Expectations() {{ exactly(1).of (mock).doSomethingWith("x", with(equal("y"))); }}); fail("should have thrown IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected } }
public void testMatchesParameterValues() { context.checking(new Expectations() {{ exactly(1).of (mock).doSomethingWith(with(equal("hello"))); exactly(1).of (mock).doSomethingWith(with(equal("goodbye"))); }}); mock.doSomethingWith("hello"); mock.doSomethingWith("goodbye"); context.assertIsSatisfied(); }
public void testErrorMessageIncludesOnceInsteadOfExactly1Time() { context.checking(new Expectations() {{ exactly(1).of (mock).method1(); }}); try { context.assertIsSatisfied(); } catch (ExpectationError e) { String message = StringDescription.toString(e); AssertThat.stringIncludes("should include 'once'", "once", message); } }
public void testErrorMessageIncludesNotInvokedInsteadOfInvokedExactly0Times() { context.checking(new Expectations() {{ exactly(1).of (mock).method1(); }}); try { context.assertIsSatisfied(); } catch (ExpectationError e) { String message = StringDescription.toString(e); AssertThat.stringIncludes("should include 'never invoked'", "never invoked", message); } }
@Test public void poke() { context.checking(new Expectations() { { exactly(3).of(collaborator).readValue(); will(JMockActions.returnEach(1,2,3)); } }); assertThat(new ClassUnderTestForReturnEach(collaborator).prependAndRead("foo"), is("foo 1")); assertThat(new ClassUnderTestForReturnEach(collaborator).prependAndRead("bar"), is("bar 2")); assertThat(new ClassUnderTestForReturnEach(collaborator).prependAndRead("baz"), is("baz 3")); }