Created
November 22, 2023 01:39
-
-
Save ZergyPoo/3d48b39f8c563cc79fa32e21d7866755 to your computer and use it in GitHub Desktop.
Stub/Singleton Pattern
This file contains 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 virtual class DemoMapper { | |
@TestVisible | |
private static DemoMapper instance; | |
protected DemoMapper() {} | |
public static DemoMapper getInstance() { | |
if (instance != null) { | |
return instance; | |
} | |
instance = new DemoMapper(); | |
return instance; | |
} | |
public virtual Something someMethodThatDoesSomething() { | |
return new Something(); | |
} | |
} |
This file contains 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 class DemoService { | |
public void someMethodThatDoesSomething() { | |
DemoMapper mapper = DemoMapper.getInstance(); | |
Something s = mapper.someMethodThatDoesSomething(); | |
} | |
} |
This file contains 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 TestClass { | |
static testMethod void someTest() { | |
DemoMapper.instance = new DemoMapperStub(); | |
// testing here | |
} | |
private class DemoMapperStub extends DemoMapper { | |
public override Something someMethodThatDoesSomething() { | |
return new SomethingElse(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment