Skip to content

Instantly share code, notes, and snippets.

@mclosson
Created October 31, 2016 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mclosson/7fcac5ebeae6f0959022a3186596327b to your computer and use it in GitHub Desktop.
Save mclosson/7fcac5ebeae6f0959022a3186596327b to your computer and use it in GitHub Desktop.
/* Problem */
class Thing {
// testing this will result in expensive calls to event logs and database
public int dostuff() {
int result = something();
EventLogsAndDatabaseLogger.log(1, 2, 3, 4, 5, "message");
return result;
}
}
Thing thing = new Thing();
int result = thing.dostuff(); // Makes expensive calls to db && event logs!
assertEqual(1, result);
/* Solution */
class Thing {
public int dostuff() {
int result = something();
log(1, 2, "message");
return result;
}
private void log(int facility, int level, String message) {
EventLogsAndDatabaseLogger.log(facility, level, message);
}
}
class TestableThing : Thing {
private override void log(int facility, int level, String message) {}
}
TestableThing thing = new TestableThing();
int result = thing.dostuff(); // No longer calls out to db or event logs :)
assertEqual(1, result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment