Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PostsToChatter {
- public interface ChatterPoster {
- void postToChatter(Id parentRecord, String text);
- }
- @TestVisible private class RealChatterPoster implements ChatterPoster {
- public void postToChatter(Id parentRecord, String text) {
- if (!Test.isRunningTest()) callConnectAPIHere(parentRecord, text);
- // Note that if we keep this minimal (one line) we still obtain full code coverage even though the Connect API call does not actually go through.
- }
- }
- @TestVisible private ChatterPoster myPoster;
- // ... constructors and code ...
- public PostsToChatter() {
- // Add a default delegate (the real Chatter poster class
- myPoster = new RealChatterPoster();
- }
- public void doSomethingRequiringAChatterPost() {
- // ... stuff happens ...
- myPoster.postToChatter(someId, someText);
- }
- }
- @isTest
- public static class PostsToChatter_TEST {
- private class MockChatterPoster implements PostsToChatter.ChatterPoster {
- public Id expectedId;
- public String expectedString;
- public Integer timesCalled = 0;
- public void postToChatter(Id parentRecord, String text) {
- System.assertEquals(expectedId, parentRecord, 'correct parent');
- System.assertEquals(expectedString, text, 'correct post body');
- timesCalled++;
- }
- }
- @isTest
- public static void testTheClass() {
- PostsToChatter p = new PostsToChatter();
- p.myPoster = new MockChatterPoster();
- // ... invoke class functionality...
- System.assertEquals(1, p.myPoster.timesCalled, 'chatter post called');
- // Post contents validated in the mock - or do it here if preferred.
- }
- }
Add Comment
Please, Sign In to add comment