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 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 + "]."); } }
static void thatCaptureAllArgumentsWithInOrderVerification() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); fflib_InOrder inOrder1 = new fflib_InOrder(mocks, new List <object> { mockList }); // When mockList.add("Fred"); mockList.add("Barney"); mockList.add("Wilma"); mockList.add("Betty"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)inOrder1.verify(mockList, mocks.calls(4))).add((string)argument.capture()); List <object> argsCaptured = argument.getAllValues(); System.assertEquals(4, argsCaptured.size(), "expected 4 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("Wilma", (string)argsCaptured[2], "the third value is not as expected"); System.assertEquals("Betty", (string)argsCaptured[3], "the forth value is not as expected"); }
static void thatDoesNotCaptureAnythingWhenCaptorIsWrappedInAMatcherWithInOrderVerification() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); fflib_InOrder inOrder1 = new fflib_InOrder(mocks, new List <object> { mockList }); // 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)inOrder1.verify(mockList, mocks.calls(1))).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 thatCaptureAllArgumentsFromMultipleMethodsWithInOrderVerification() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); fflib_InOrder inOrder1 = new fflib_InOrder(mocks, new List <object> { mockList }); // When mockList.add("Fred"); mockList.add("Barney"); mockList.get2(3, "pebble"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)inOrder1.verify(mockList, mocks.calls(2))).add((string)argument.capture()); ((fflib_MyList.IList)inOrder1.verify(mockList, mocks.calls(1))).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 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 thatBetweenThrownExceptionIfCalledMoreThanAtMostNumberOfTimes() { // 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("fred"); mockList.add("bob"); mockList.add("bob"); mockList.add("bob"); mockList.add("bob"); mockList.add("bob"); mockList.add("fred"); // Then try { ((fflib_MyList.IList)mocks.verify(mockList, mocks.between(3, 5))).add("bob"); System.assert(false, "an exception was expected because we are asserting that the method is called at most 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 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 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 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 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"); }
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")); }
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"); }
static void thatAnswerOnlyForTheMethodStubbedWithAnswer() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); mocks.startStubbing(); mocks.when(mockList.get(3)).thenReturn("ted"); mocks.when(mockList.get2(0, "Hi hi Hello Hi hi")).thenAnswer(new fflib_AnswerTest.BasicAnswer()); mocks.stopStubbing(); // When mockList.add("one"); string noAnswered = mockList.get(3); mockList.get2(0, "Hi hi Hello Hi hi"); // Then object methodCalled = actualInvocation.getMethod(); System.assert(methodCalled is fflib_QualifiedMethod, "the object returned is not a method as expected"); string expectedMethodSignature = fflib_MyList.getStubClassName() + ".get2(Integer, String)"; System.assertEquals(expectedMethodSignature, ((fflib_QualifiedMethod)methodCalled).toString(), " the method is no the one expected"); System.assertEquals("ted", noAnswered, "the get method should have returned the stubbed string"); }
static void thatCanHandleMultipleCapturesInOneMethodCallWithInOrderVerification() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); fflib_InOrder inOrder1 = new fflib_InOrder(mocks, new List <object> { mockList }); // When mockList.add("Fred", "Barney", "Wilma", "Betty"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)inOrder1.verify(mockList, mocks.calls(1))).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"); }
static void thatCaptureLastArgument() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); // When mockList.add("Barney"); mockList.add("Fred"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList, 2)).add((string)argument.capture()); System.assertEquals("Fred", (string)argument.getValue(), "the argument captured is not as expected"); }
static void thatCaptureLastArgumentWithInOrderVerification() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); fflib_InOrder inOrder1 = new fflib_InOrder(mocks, new List <object> { mockList }); // When mockList.add("Barney"); mockList.add("Fred"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)inOrder1.verify(mockList, mocks.calls(2))).add((string)argument.capture()); System.assertEquals("Fred", (string)argument.getValue(), "the argument captured is not as expected"); }
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()); }
private static void whenVerifyWithCombinedMatchersShouldReturnCorrectMethodCallCounts() { // 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.never())).add((string)fflib_Match.allOf(fflib_Match.eq("bob"), fflib_Match.stringContains("re"))); ((fflib_MyList.IList)mocks.verify(mockList)).add((string)fflib_Match.allOf(fflib_Match.eq("fred"), fflib_Match.stringContains("re"))); ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2))).add((string)fflib_Match.anyOf(fflib_Match.eq("bob"), fflib_Match.eq("fred"))); ((fflib_MyList.IList)mocks.verify(mockList)).add((string)fflib_Match.anyOf(fflib_Match.eq("bob"), fflib_Match.eq("jack"))); ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2))).add((string)fflib_Match.noneOf(fflib_Match.eq("jack"), fflib_Match.eq("tim"))); ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2))).add((string)fflib_Match.noneOf(fflib_Match.anyOf(fflib_Match.eq("jack"), fflib_Match.eq("jill")), fflib_Match.allOf(fflib_Match.eq("tim"), fflib_Match.stringContains("i")))); ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2))).add((string)fflib_Match.isNot(fflib_Match.eq("jack"))); }
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(List <string> .class);
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 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 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 thatVerifyTimesMethodFailsWhenCalledMoreTimesWithMatchers() { // 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 try { ((fflib_MyList.IList)mocks.verify(mockList, mocks.times(2))).add(fflib_Match.anyString()); System.assert(false, "an exception was expected"); } catch (Exception exc) { assertFailMessage(exc.getMessage(), 2, 3); } }
static void thatMultipleAnswersAreHandled() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); mocks.startStubbing(); mocks.when(mockList.get(3)).thenAnswer(new fflib_AnswerTest.FirstAnswer()); mocks.when(mockList.get2(0, "Hi hi Hello Hi hi")).thenAnswer(new fflib_AnswerTest.SecondAnswer()); mocks.stopStubbing(); // When mockList.add("one"); string answer1 = mockList.get(3); string answer2 = mockList.get2(0, "Hi hi Hello Hi hi"); System.assertEquals("this is the first answer", answer1, "the answer wasnt the one expected"); System.assertEquals("and this is the second one", answer2, "the answer wasnt the one expected"); }
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"); }