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 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 thatArgumentListEmptyForMethodWithNoArgument() { // Given fflib_ApexMocks mocks = new fflib_ApexMocks(); fflib_MyList mockList = (fflib_MyList)mocks.mock(typeof(fflib_MyList)); mocks.startStubbing(); mocks.when(mockList.isEmpty()).thenAnswer(new fflib_AnswerTest.ArgumentListEmptyForMethodWithNoArgument()); mocks.stopStubbing(); // When bool actualValue = mockList.isEmpty(); }
static void thatExceptionIsThrownWhenAccessNegativeIndexArgument() { // 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.ExceptionForNegativeArgumentIndex()); mocks.stopStubbing(); // When string actualValue = mockList.get2(0, "Hi hi Hello Hi hi"); }
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"); }
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 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 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 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 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 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"); } }