public void testCanReturnDefaultValueFromFlexibleExpectation() { context.checking(new Expectations() {{ allowing (anything()).method(withName(".*")); }}); mock1.returnInt(); // should not fail }
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 }
public void testCanSpecifyMethodNameRegexDirectly() { context.checking(new Expectations() {{ allowing (anything()).method("doSomething.*"); }}); mock1.doSomething(); mock1.doSomething(); mock2.doSomethingWith("x", "y"); try { mock1.method1(); fail("method1 should not have been expected"); } catch (ExpectationError e) { // expected } }
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 testCanSpecifyFlexibleMethodMatchers() { context.checking(new Expectations() {{ allowing (anything()).method(withName("doSomething.*")); }}); mock1.doSomething(); mock1.doSomething(); mock2.doSomethingWith("x", "y"); try { mock1.method1(); fail("method1 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 } }