static void thatAnswerToVoidAndNotVoidMethods() { // 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.FirstAnswer(), mockList)).get(3); ((fflib_MyList)mocks.doAnswer(new fflib_AnswerTest.BasicAnswer(), mockList)).addMore("Hi hi Hello Hi hi"); ((fflib_MyList)mocks.doAnswer(new fflib_AnswerTest.SecondAnswer(), mockList)).get2(4, "Hi hi Hello Hi hi"); mocks.stopStubbing(); // When string answer1 = mockList.get(3); string answer2 = mockList.get2(4, "Hi hi Hello Hi hi"); 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(), "the last method called should be the addMore, so should be the last to set the actualInvocation variable."); System.assertEquals("this is the first answer", answer1, "the answer was not the one expected"); System.assertEquals("and this is the second one", answer2, "the answer was not the one expected"); }
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 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"); }
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"); }