static void thatCaptureAllArgumentsForTheVerifiedMethods() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); List <string> stringList = new List <string> { "3" }; // When mockList.add("Fred"); mockList.add(stringList); mockList.clear(); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList)).add((string)argument.capture()); ((fflib_MyList.IList)mocks.verify(mockList)).add((List <string>)argument.capture()); System.assertEquals(stringList, (List <string>)argument.getValue(), "the argument captured is not as expected"); List <object> argsCaptured = argument.getAllValues(); System.assertEquals(2, argsCaptured.size(), "expected 2 argument to be captured"); System.assertEquals("Fred", (string)argsCaptured[0], "the first value is not as expected"); }
private static void whenVerifyMethodNeverCalledMatchersAreReset() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob"); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).get(fflib_Match.anyInteger()); ((fflib_MyList.IList)mocks.verify(mockList)).add(fflib_Match.anyString()); }
private static void verifySingleMethodCallWithMultipleArguments() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.set(0, "bob"); // Then ((fflib_MyList.IList)mocks.verify(mockList)).set(0, "bob"); ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).set(0, "fred"); }
private static void verifyMethodNotCalled() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.get(0); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).add("bob"); ((fflib_MyList.IList)mocks.verify(mockList)).get(0); }
private static void whenVerifyMultipleCallsWithMatchersShouldReturnCorrectMethodCallCounts() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob"); mockList.add("fred"); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2))).add(fflib_Match.anyString()); ((fflib_MyList.IList)mocks.verify(mockList)).add("fred"); ((fflib_MyList.IList)mocks.verify(mockList)).add(fflib_Match.stringContains("fred")); }
static void thatDoesNotCaptureAnythingWhenCaptorIsWrappedInAMatcher() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("Same", "Same", "First call", "First call"); mockList.add("Same", "Same", "Second call", "Second call"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList)).add((string)fflib_Match.allOf(fflib_Match.eqString("Same"), fflib_Match.eqString("Same"), argument.capture()), (string)fflib_Match.allOf(fflib_Match.eqString("Same"), fflib_Match.eqString("Same"), argument.capture()), (string)fflib_Match.allOf(argument.capture(), fflib_Match.eqString("First call")), (string)fflib_Match.allOf(argument.capture(), fflib_Match.eqString("First call"))); List <object> capturedValues = argument.getAllValues(); System.assertEquals(0, capturedValues.size(), "nothing should have been capture because the matcher it not really a capture type, but a allOf()"); System.assertEquals(null, (string)argument.getValue(), "nothing should have been capture because the matcher it not really a capture type, but a allOf()"); }
static void thatCanPerformFurtherAssertionsOnCapturedArgumentValue() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); //When TestInnerClass testValue = new TestInnerClass(); testValue.i = 4; testValue.s = "5"; mockList.set(1, testValue); //Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(TestInnerClass)); ((fflib_MyList.IList)mocks.verify(mockList)).set(fflib_Match.anyInteger(), argument.capture()); object capturedArg = argument.getValue(); System.assertNotEquals(null, capturedArg, "CapturedArg should not be null"); System.assert(capturedArg is TestInnerClass, "CapturedArg should be SObject, instead was " + capturedArg); TestInnerClass testValueCaptured = (TestInnerClass)capturedArg; System.assertEquals(4, testValueCaptured.i, "the values inside the argument captured should be the same of the original one"); System.assertEquals("5", testValueCaptured.s, "the values inside the argument captured should be the same of the original one"); }
static void thatCaptureAllArgumentswhenMethodIsCalledWithTheSameArgument() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("Fred"); mockList.add("Barney"); mockList.add("Wilma"); mockList.add("Barney"); mockList.add("Barney"); mockList.add("Betty"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList, 6)).add((string)argument.capture()); List <object> argsCaptured = argument.getAllValues(); System.assertEquals(6, argsCaptured.size(), "expected 6 arguments to be captured"); System.assertEquals("Fred", (string)argsCaptured[0], "the first value is not as expected"); System.assertEquals("Barney", (string)argsCaptured[1], "the second value is not as expected"); System.assertEquals("Wilma", (string)argsCaptured[2], "the third value is not as expected"); System.assertEquals("Barney", (string)argsCaptured[3], "the fourth value is not as expected"); System.assertEquals("Barney", (string)argsCaptured[4], "the fifth value is not as expected"); System.assertEquals("Betty", (string)argsCaptured[5], "the sixth value is not as expected"); }
private static void thatCustomMessageIsAdded() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob"); mockList.add("bob"); mockList.add("bob"); string customAssertMessage = "Custom message to explain the reason of the verification"; // Then try { ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2).description(customAssertMessage))).add(fflib_Match.anyString()); System.assert(false, "an exception was expected"); } catch (Exception exc) { string exceptionMessage = exc.getMessage(); string expectedMessage = string.format(BASIC_VERIFY_ASSERTION_MESSAGE, new List <string> { "2", "3" }) + fflib_MyList.getStubClassName() + ".add(String). " + customAssertMessage + "."; System.assertEquals(expectedMessage, exceptionMessage, "The exception was caught, but the message was not as expected. " + "Expected: [" + expectedMessage + "], Actual: [" + exceptionMessage + "]."); } }
private static void thatThrownExceptionIfCalledLessThanAtLeastOnce() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob"); mockList.add("fred"); mockList.add("bob"); // Then try { ((fflib_MyList.IList)mocks.verify(mockList, mocks.atLeastOnce())).add("rob"); System.assert(false, "an exception was expected because we are asserting that the method is called one times when instead is not called"); } catch (fflib_ApexMocks.ApexMocksException ex) { string expectedMessage = "Expected : 1 or more times, Actual: 0 -- Wanted but not invoked: " + fflib_MyList.getStubClassName() + ".add(String)."; string actualMessage = ex.getMessage(); System.assertEquals(expectedMessage, actualMessage, "the exception has been caught as expected, however the message is not as expected"); } }
private static void thatBetweenThrownExceptionIfCalledMoreThanAtMostNumberOfTimesWithMatchers() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob"); mockList.add("fred"); mockList.add("bob"); mockList.add("fred"); mockList.add("fred"); mockList.add("fred"); // Then try { ((fflib_MyList.IList)mocks.verify(mockList, mocks.between(3, 5))).add(fflib_Match.anyString()); System.assert(false, "an exception was expected because we are asserting that the method is called 5 times when instead is called six times"); } catch (fflib_ApexMocks.ApexMocksException ex) { string expectedMessage = "Expected : 5 or fewer times, Actual: 6 -- Wanted but not invoked: " + fflib_MyList.getStubClassName() + ".add(String)."; string actualMessage = ex.getMessage(); System.assertEquals(expectedMessage, actualMessage, "the exception has been caught as expected, however the message is not as expected"); } }
private static void whenVerifyCustomMatchersCanBeUsed() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.get(1); mockList.get(2); mockList.get(3); mockList.get(4); mockList.get(5); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(3))).get((int)fflib_Match.matches(new isOdd())); ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2))).get((int)fflib_Match.matches(new isEven())); }
private static void verifyMethodCallWhenNoCallsBeenMadeForType() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).add("bob"); }
private static void verifyMultipleMethodCallsWithSameSingleArgument() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob"); mockList.add("bob"); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2))).add("bob"); }
private static void thatVerifiesAtLeastOnceNumberOfTimesWithMatchers() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob"); mockList.add("fred", "fred", "fred", "fred"); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.atLeastOnce())).add(fflib_Match.anyString()); }
private static void verifyTimesMethodHasBeenCalledWithMatchers() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob1"); mockList.add("bob2"); mockList.add("bob3"); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(3))).add(fflib_Match.anyString()); }
private static void verifyNeverMethodHasBeenNotCalledWithMatchers() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("fred", "fred", "fred", "fred"); mockList.add("fred", "fred", "fred", "fred"); mockList.add("fred", "fred", "fred", "fred"); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).add(fflib_Match.anyString()); }
private static void verifyNeverMethodHasNotBeenCalled() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob1"); mockList.add("bob2"); mockList.add("bob3"); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).add("bob"); }
private static void verifyTimesMethodHasBeenCalled() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob"); mockList.add("bob"); mockList.add("bob"); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(3))).add("bob"); }
static void thatCaptureAllArgumentsFromMultipleMethods() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("Fred"); mockList.add("Barney"); mockList.get2(3, "pebble"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList, 2)).add((string)argument.capture()); ((fflib_MyList.IList)mocks.verify(mockList)).get2((int)fflib_Match.eq(3), (string)argument.capture()); List <object> argsCaptured = argument.getAllValues(); System.assertEquals(3, argsCaptured.size(), "expected 3 argument to be captured"); System.assertEquals("Fred", (string)argsCaptured[0], "the first value is not as expected"); System.assertEquals("Barney", (string)argsCaptured[1], "the second value is not as expected"); System.assertEquals("pebble", (string)argsCaptured[2], "the third value is not as expected"); }
static void thatArgumentValueIsCaptured() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("Fred"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList)).add((string)argument.capture()); System.assertEquals("Fred", (string)argument.getValue(), "the argument captured is not as expected"); }
private static void thatVerifiesAtLeastOnce() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob"); mockList.add("fred"); mockList.add("bob"); mockList.add("bob"); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.atLeastOnce())).add("bob"); }
private static void thatVerifiesAtMostSameNumberOfTimes() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob"); mockList.add("fred"); mockList.add("bob"); mockList.add("bob"); mockList.add("fred"); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.atMost(3))).add("bob"); }
private static void thatVerifiesAtLeastNumberOfTimesWhenIsCalledMoreTimes() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob"); mockList.add("fred"); mockList.add("bob"); mockList.add("fred"); mockList.add("bob"); mockList.add("fred"); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.atLeast(2))).add("bob"); }
static void thatDoesNotCaptureIfNotVerified() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("3"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(List <string>)); ((fflib_MyList.IList)mocks.verify(mockList, fflib_ApexMocks.NEVER)).add((List <string>)argument.capture()); List <object> argsCaptured = argument.getAllValues(); System.assertEquals(0, argsCaptured.size(), "expected 0 argument to be captured"); System.assertEquals(null, argument.getValue(), "no value should be captured, so must return null"); }
static void thatCaptureArgumentFromRequestedParameter() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("Fred", "Barney", "Wilma", "Betty"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList)).add((string)fflib_Match.eq("Fred"), (string)fflib_Match.eq("Barney"), (string)argument.capture(), (string)fflib_Match.eq("Betty")); System.assertEquals("Wilma", (string)argument.getValue(), "the argument captured is not as expected, should be Wilma because is the 3rd parameter in the call"); }
static void thatCaptureOnlyMethodsThatMatchesWithOtherMatcherAsWell() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("Same", "Same", "First call", "First call"); mockList.add("Same", "Same", "Second call", "Second call"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList)).add(fflib_Match.eqString("Same"), fflib_Match.eqString("Same"), (string)argument.capture(), fflib_Match.eqString("First call")); System.assertEquals("First call", (string)argument.getValue()); }
static void thatCaptureArgumentOnlyFromVerifiedMethod() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("Fred"); //the next call should be ignored because is not the method that has under verify, //even if have the same type specified in the capturer. mockList.addMore("Barney"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList)).add((string)argument.capture()); System.assertEquals("Fred", (string)argument.getValue(), "the argument captured is not as expected"); System.assertEquals(1, argument.getAllValues().size(), "the argument captured should be only one"); }
private static void thatVerifyNeverFailsWhenCalledMoreTimes() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("bob"); mockList.add("bob"); // Then try { ((fflib_MyList.IList)mocks.verify(mockList, mocks.never())).add("bob"); System.assert(false, "an exception was expected"); } catch (Exception exc) { assertFailMessage(exc.getMessage(), 0, 2); } }
static void thatCanHandleMultipleCapturesInOneMethodCall() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("Fred", "Barney", "Wilma", "Betty"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList)).add((string)fflib_Match.eq("Fred"), (string)argument.capture(), (string)argument.capture(), (string)fflib_Match.eq("Betty")); List <object> argsCaptured = argument.getAllValues(); System.assertEquals(2, argsCaptured.size(), "expected 2 argument to be captured"); System.assertEquals("Barney", (string)argsCaptured[0], "the first value is not as expected"); System.assertEquals("Wilma", (string)argsCaptured[1], "the second value is not as expected"); }