Last active
November 10, 2020 14:56
-
-
Save justin-lyon/a73d0216e0bb5255c5509661bf8e5123 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing MyClass { | |
public KbManagerI kavManager; | |
public MyClass() { | |
// Default KbManager Implementation | |
this(new MyKbManager()); | |
} | |
// Allows for Constructor Injection, so we can change the implementation for our tests. | |
public MyClass(KbManagerI km) { | |
this.kavManager = km; | |
} | |
public void doThing(Id articleId, Boolean flagAsNew) { | |
this.kavManager.publishArticle(articleId, flagAsNew); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An interface that describes the method signatures in KbManager.PublishingService | |
public interface KbManagerI { | |
void publishArticle(Id articleId, Boolean flagAsNew); | |
//... add more methods as necessary | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This class implements the interface. It is a pass through that our code will reference instead of the PublishingService | |
public inherited sharing class MyKbManager implements KbManagerI { | |
public void publishArticle(Id articleId, Boolean flagAsNew) { | |
// this implementation of KbManagerI actually calls the real PublishingService | |
KbManager.PublishingService.publishArticle(articleId, flagAsNew); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This mock implements the same interface, so we can inject it into MyClass during MyClassTest | |
public inherited sharing class KbManagerMock implements KbManagerI { | |
// Can Also have private/public members for MyClassTest to access at runtime. | |
public Boolean isSuccess = false; | |
public Boolean throwException = false; | |
public void publishArticle(Id articleId, Boolean flagAsNew) { | |
// this implementation of KbManagerI actually calls the real PublishingService | |
// Do anything | |
if (throwException) { | |
// Throw some exception here, now you can easily test try/catch blocks that are in MyClass | |
} | |
// We called the method we wanted, and it finished executing. We can say this was a success. You could similarly give test data to the mock in the same way that we are working with the booleans. | |
isSuccess = true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IsTest | |
private class MyClassTest { | |
@TestSetup | |
public void setup() {/* ... */} | |
@IsTest | |
static void doThing_givenNewArticle_shouldPublishNew() { | |
KbManagerMock mockPublisher = new KbManagerMock(); | |
/* | |
* Optionally assign data to the mock now | |
* Then pass (re: inject) it to MyClass constructor | |
*/ | |
MyClass unit = new MyClass(mockPublisher); | |
Test.startTest(); | |
unit.publishArticle("someValidArticleId", true); | |
Test.stopTest(); | |
System.assert(mockPublisher.isSuccess, "The mock publish method should have marked this attribute true."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment