Guest User

Untitled

a guest
Jul 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. public class PostsToChatter {
  2. public interface ChatterPoster {
  3. void postToChatter(Id parentRecord, String text);
  4. }
  5.  
  6. @TestVisible private class RealChatterPoster implements ChatterPoster {
  7. public void postToChatter(Id parentRecord, String text) {
  8. if (!Test.isRunningTest()) callConnectAPIHere(parentRecord, text);
  9. // 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.
  10. }
  11. }
  12.  
  13. @TestVisible private ChatterPoster myPoster;
  14.  
  15.  
  16. // ... constructors and code ...
  17.  
  18. public PostsToChatter() {
  19. // Add a default delegate (the real Chatter poster class
  20. myPoster = new RealChatterPoster();
  21. }
  22.  
  23. public void doSomethingRequiringAChatterPost() {
  24. // ... stuff happens ...
  25. myPoster.postToChatter(someId, someText);
  26. }
  27. }
  28.  
  29. @isTest
  30. public static class PostsToChatter_TEST {
  31. private class MockChatterPoster implements PostsToChatter.ChatterPoster {
  32. public Id expectedId;
  33. public String expectedString;
  34. public Integer timesCalled = 0;
  35.  
  36. public void postToChatter(Id parentRecord, String text) {
  37. System.assertEquals(expectedId, parentRecord, 'correct parent');
  38. System.assertEquals(expectedString, text, 'correct post body');
  39. timesCalled++;
  40. }
  41. }
  42.  
  43. @isTest
  44. public static void testTheClass() {
  45. PostsToChatter p = new PostsToChatter();
  46. p.myPoster = new MockChatterPoster();
  47.  
  48. // ... invoke class functionality...
  49.  
  50. System.assertEquals(1, p.myPoster.timesCalled, 'chatter post called');
  51. // Post contents validated in the mock - or do it here if preferred.
  52. }
  53. }
Add Comment
Please, Sign In to add comment