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 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 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(); }
@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")); }
public void testExpectsAtMost() { context.checking(new Expectations() {{ atMost(2).of (mock).doSomething(); }}); context.assertIsSatisfied(); mock.doSomething(); context.assertIsSatisfied(); mock.doSomething(); assertAnotherInvocationFailsTheTest(); }
public void testCanPassLiteralValuesToWithMethodToMeanEqualTo() { context.checking(new Expectations() {{ exactly(2).of (mock).doSomethingWithBoth(with.<String>is(anything()), with("y")); }}); mock.doSomethingWithBoth("x", "y"); mock.doSomethingWithBoth("z", "y"); context.assertIsSatisfied(); }
public void testCanPassLiteralValuesToWithMethodToMeanEqualTo() { context.checking(new Expectations() {{ exactly(2).of (mock).doSomethingWithBoth(with(any(String.class)), with("y")); }}); mock.doSomethingWithBoth("x", "y"); mock.doSomethingWithBoth("z", "y"); context.assertIsSatisfied(); }
/***********************************/ private DisplayHierarchy.DisplayHierarchyContext contextFixture( final boolean isSeen, String mockName ) { final DisplayHierarchy.DisplayHierarchyContext dhContext = context.mock( DisplayHierarchy.DisplayHierarchyContext.class, mockName ); final RDFNodeWrapper r = new RDFNodeWrapper( rm.page().getModelW(), ResourceFactory.createResource( "http://example/foo#r0") ); context.checking(new Expectations() {{ atLeast(0).of (dhContext).isSeen( r ); will( returnValue( isSeen ) ); }}); return dhContext; }
public void testCanEasilySpecifySequenceOfStubsForSameMethod() { context.checking(new Expectations() {{ atLeast(1).of (mock).returnString(); will(onConsecutiveCalls(returnValue("hello"), returnValue("bonjour"), returnValue("guten Tag"))); }}); assertEquals("hello", mock.returnString()); assertEquals("bonjour", mock.returnString()); assertEquals("guten Tag", mock.returnString()); } }