Guest User

Untitled

a guest
May 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. @isTest static void testMethodOne() {
  2. Test.StartTest();
  3. Test.setMock(HttpCalloutMock.class, new SFEndpointMock());
  4. SFEndpointMock.indicatorVar ='success';
  5. Test.StopTest();
  6. myClass.performRequest('testData1');
  7. }
  8.  
  9. @isTest static void testMethodTwo() {
  10. Test.StartTest();
  11. Test.setMock(HttpCalloutMock.class, new SFEndpointMock());
  12. SFEndpointMock.indicatorVar ='success';
  13. Test.StopTest();
  14. myClass.performRequest('testData2');
  15. }
  16.  
  17. @isTest
  18. global class SFEndpointMock implements HttpCalloutMock {
  19. public static String indicatorVar;
  20. global HttpResponse respond(HTTPRequest req){
  21. HttpResponse res = new HttpResponse();
  22. if(indicatorVar== 'success') {
  23. res.setStatus('OK');
  24. res.setStatusCode(200);
  25. String json = ***JSON BODY HERE***
  26. res.setBody(json);
  27.  
  28. }
  29.  
  30. else if (indicatorVar== 'success2') {
  31. res.setStatus('OK');
  32. res.setStatusCode(400);
  33. String json = ***DIFFERENT JSON BODY HERE***
  34. res.setBody(json);
  35.  
  36. }
  37. return res;
  38.  
  39. if(!Test.isRunningTest()) {
  40.  
  41. res = http.send(request);
  42. }
  43.  
  44. else {
  45.  
  46. res= MockApiCalls.WebserviceExpectedResponse1();
  47. }
  48.  
  49. public class MockApiCalls {
  50. public static HttpResponse WebserviceExpectedResponse1() {
  51. HttpResponse res = new HttpResponse();
  52. String json = ***JSON BODY HERE***
  53. res.setBody(json);
  54. res.setStatusCode(200);
  55. return res;
  56.  
  57. }
  58.  
  59. public static HttpResponse WebserviceExpectedResponse2() {
  60. HttpResponse res = new HttpResponse();
  61. String json = ***DIFFERENT JSON BODY HERE***
  62. res.setBody(json);
  63. res.setStatusCode(400);
  64. return res;
  65.  
  66. }
  67. }
  68.  
  69. @isTest global class SFEndpointMock implements HttpCalloutMock {
  70. private String indicatorVar;
  71. global SFEndpointMock(String indicatorVar) {
  72. this.indicatorVar = indicatorVar;
  73. }
  74. global HttpResponse respond(HTTPRequest req){
  75. ...
  76. }
  77. }
  78.  
  79. Test.setMock(HttpCalloutMock.class, new SFEndpointMock('success'));
Add Comment
Please, Sign In to add comment