public <T> T oneOf(T mockObject) { return exactly(1).of(mockObject); }
public void testMatchersCanCopeWithDifferentArgumentTypes() { context.checking(new Expectations() {{ exactly(1).of (anything()).method(withName("m")).with(startsWith("x")); exactly(1).of (anything()).method(withName("m")).with(greaterThan(0)); }}); mock.m(1); // should not throw ClassCastException }
@Test public void canSetExpectationsWithMatchersForMethodsWithArgumentsOfPrimitiveTypes() { context.checking(new Expectations() {{ exactly(1).of (mock).withBoolean(with.booleanIs(equal(true))); exactly(1).of (mock).withByte(with.byteIs(equal((byte)10))); exactly(1).of (mock).withShort(with.shortIs(equal((short) 10))); exactly(1).of (mock).withInt(with.intIs(equal(10))); exactly(1).of (mock).withLong(with.longIs(equal(10L))); exactly(1).of (mock).withFloat(with.floatIs(equal(10.0f))); exactly(1).of (mock).withDouble(with.doubleIs(equal(10.0))); }}); mock.withBoolean(true); mock.withByte((byte)10); mock.withShort((short)10); mock.withInt(10); mock.withLong(10L); mock.withFloat(10.0f); mock.withDouble(10.0); context.assertIsSatisfied(); }
public void testCanSpecifyNoArguments() { context.checking(new Expectations() {{ allowing (anything()).method(withName("do.*")).withNoArguments(); allowing (anything()).method(withName("do.*")).with(equal("X"), equal("Y")); }}); mock1.doSomething(); mock1.doSomethingWith("X", "Y"); try { mock1.doSomethingWith("x", "y"); fail("doSomething should not have been expected"); } catch (ExpectationError e) { // expected } }
public void testCanSpecifyFlexibleArgumentMatchers() { context.checking(new Expectations() {{ allowing (anything()).method(withName("doSomethingWith")).with(equal("x"), equal("y")); allowing (anything()).method(withName("doSomethingWith")).with(equal("X"), equal("Y")); }}); mock1.doSomethingWith("x", "y"); mock1.doSomethingWith("X", "Y"); mock2.doSomethingWith("x", "y"); mock2.doSomethingWith("X", "Y"); try { mock1.doSomething(); fail("doSomething should not have been expected"); } catch (ExpectationError e) { // expected } }
public void testAllOrNoneOfTheParametersMustBeSpecifiedByMatchers() { try { context.checking(new Expectations() {{ oneOf (mock).doSomethingWithBoth(with.<String>is(equal("a-matcher")), "not-a-matcher"); }}); } catch (IllegalArgumentException expected) { } }
public void testSpecifyingAnyValueOfPrimitiveType() { context.checking(new Expectations() {{ allowing (mock).doSomethingWithBoth(with.booleanIs(any(boolean.class)), with.booleanIs(any(boolean.class))); }}); mock.doSomethingWithBoth(true, true); mock.doSomethingWithBoth(true, false); mock.doSomethingWithBoth(false, true); mock.doSomethingWithBoth(false, false); } }
public <T> T allowing(T mockObject) { return atLeast(0).of(mockObject); }
public void testMatchesParameterValues() { context.checking(new Expectations() {{ oneOf (mock).doSomethingWith(with.<String>is(equal("hello"))); oneOf (mock).doSomethingWith(with.<String>is(equal("goodbye"))); }}); mock.doSomethingWith("hello"); mock.doSomethingWith("goodbye"); context.assertIsSatisfied(); }
public <T> T never(T mockObject) { return exactly(0).of(mockObject); }
public void testNotAllExpectationsOfSameMockMustUseMatchers() { context.checking(new Expectations() {{ oneOf (mock).doSomethingWithBoth(with.<String>is(equal("x")), with.<String>is(equal("y"))); oneOf (mock).doSomethingWith("z"); }}); mock.doSomethingWithBoth("x", "y"); mock.doSomethingWith("z"); context.assertIsSatisfied(); }
public MethodClause allowing(Matcher<?> mockObjectMatcher) { return atLeast(0).of(mockObjectMatcher); }
public static <T> ListModel<T> mockModel(Mockery mockery, final T... items) { // cannot mock generic types @SuppressWarnings("unchecked") final ListModel<T> model = mockery.mock(ListModel.class); mockery.checking(new Expectations() { { atMost(1).of(model).getItemCount(); will(returnValue(items.length)); for (int i = 0; i < items.length; i++) { atMost(1).of(model).getItem(i); will(returnValue(items[i])); } } }); return model; }
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 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 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 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(); }