private static void thatVerifiesAtLeastOnceWhenIsCalledMoreTimesWithMatchers() { // 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("fred"); // Then ((fflib_MyList.IList)mocks.verify(mockList, mocks.atLeastOnce())).add(fflib_Match.anyString()); }
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 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 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 thatMethodsParametersAreAccessibleWhenCalledWithMatchers() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); mocks.startStubbing(); mocks.when(mockList.get2(fflib_Match.anyInteger(), fflib_Match.anyString())).thenAnswer(new fflib_AnswerTest.ProcessArgumentAnswer()); mocks.stopStubbing(); // When string actualValue = mockList.get2(0, "Hi hi Hello Hi hi"); // Then System.assertEquals("Bye hi Hello Bye hi", actualValue, "the answer is not correct"); }
static void thatStoresMockInstanceIntoInvocationOnMock() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); mocks.startStubbing(); mocks.when(mockList.get2(0, "Hi hi Hello Hi hi")).thenAnswer(new fflib_AnswerTest.BasicAnswer()); mocks.stopStubbing(); // When string mockCalled = mockList.get2(0, "Hi hi Hello Hi hi"); // Then System.assert(actualInvocation.getMock()is fflib_MyList.IList, "the object returned is not a mock instance as expected"); System.assertEquals(mockList, actualInvocation.getMock(), "the mock returned should be the mockList used in the stubbing"); }
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())); }
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"); }
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 thatAnswerToVoidMethod() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); mocks.startStubbing(); ((fflib_MyList)mocks.doAnswer(new fflib_AnswerTest.BasicAnswer(), mockList)).addMore("Hi hi Hello Hi hi"); mocks.stopStubbing(); // When mockList.addMore("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()+ ".addMore(String)"; System.assertEquals(expectedMethodSignature, ((fflib_QualifiedMethod)methodCalled).toString(), "Unexpected method name: "+ methodCalled); }
static void thatStoresMethodIntoInvocationOnMock() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); mocks.startStubbing(); mocks.when(mockList.get2(0, "Hi hi Hello Hi hi")).thenAnswer(new fflib_AnswerTest.BasicAnswer()); mocks.stopStubbing(); // When 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"); }
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 thatArgumentValueIsCapturedWithInOrderVerification() { // 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"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)inOrder1.verify(mockList, mocks.calls(1))).add((string)argument.capture()); System.assertEquals("Fred", (string)argument.getValue(), "the argument captured is not as expected"); }
static void thatAnswerOnlyForTheStubbedParameter() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); mocks.startStubbing(); mocks.when(mockList.get2(0, "Hi hi Hello Hi hi")).thenAnswer(new fflib_AnswerTest.ProcessArgumentAnswer()); mocks.stopStubbing(); // When string actualValue1 = mockList.get2(0, "some string for my method"); string actualValue2 = mockList.get2(0, "Hi hi Hello Hi hi"); string actualValue3 = mockList.get2(0, "another string for the same method"); // Then System.assertEquals("Bye hi Hello Bye hi", actualValue2, "the answer is not correct"); System.assertEquals(null, actualValue1, "the answer is not correct"); System.assertEquals(null, actualValue3, "the answer is not correct"); }
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 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"); }
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 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 thatAnswerToDifferentVoidMethods() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); fflib_AnswerTest.FirstAnswer answer1 = new fflib_AnswerTest.FirstAnswer(); fflib_AnswerTest.SecondAnswer answer2 = new fflib_AnswerTest.SecondAnswer(); System.assertEquals(null, answer1.getMessage(), "the answer message should be null at this stage"); System.assertEquals(null, answer2.getMessage(), "the answer message should be null at this stage"); mocks.startStubbing(); ((fflib_MyList)mocks.doAnswer(answer1, mockList)).addMore("Hi hi Hello Hi hi"); ((fflib_MyList)mocks.doAnswer(answer2, mockList)).add("Hello"); mocks.stopStubbing(); // When mockList.addMore("Hi hi Hello Hi hi"); mockList.add("Hello"); // Then System.assertEquals("this is the first answer", answer1.getMessage(), "the answer was not the one expected"); System.assertEquals("and this is the second one", answer2.getMessage(), "the answer was not the one expected"); }
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"); }
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 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 thatAnswersWithException() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); mocks.startStubbing(); mocks.when(mockList.get2(0, "Hi hi Hello Hi hi")).thenAnswer(new fflib_AnswerTest.ExceptionForAnswer()); mocks.stopStubbing(); // When try { mockList.get2(0, "Hi hi Hello Hi hi"); System.assert(false, "an exception is expected to be thrown on the answer execution"); } catch (fflib_ApexMocks.ApexMocksException ansExpt) { string expectedMessage = "an error occurs on the execution of the answer"; // Then System.assertEquals(expectedMessage, ansExpt.getMessage(), "the message from the answer is not as expected"); } }
private static void thatBetweenThrownExceptionIfCalledLessThanAtLeastNumberOfTimesWithMatchers() { // 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 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 3 times when instead is called only twice"); } catch (fflib_ApexMocks.ApexMocksException ex) { string expectedMessage = "Expected : 3 or more times, Actual: 2 -- 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"); } }
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 thatCaptureAllArguments() { // 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("Betty"); // Then fflib_ArgumentCaptor argument = fflib_ArgumentCaptor.forClass(typeof(string)); ((fflib_MyList.IList)mocks.verify(mockList, 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"); }