Created
October 31, 2016 18:32
-
-
Save mclosson/7fcac5ebeae6f0959022a3186596327b to your computer and use it in GitHub Desktop.
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
/* 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