Skip to content

Instantly share code, notes, and snippets.

@ZergyPoo
Created November 22, 2023 01:39
Show Gist options
  • Save ZergyPoo/3d48b39f8c563cc79fa32e21d7866755 to your computer and use it in GitHub Desktop.
Save ZergyPoo/3d48b39f8c563cc79fa32e21d7866755 to your computer and use it in GitHub Desktop.
Stub/Singleton Pattern
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();
}
}
public class DemoService {
public void someMethodThatDoesSomething() {
DemoMapper mapper = DemoMapper.getInstance();
Something s = mapper.someMethodThatDoesSomething();
}
}
@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