Album Cover

Faking & Mocking Java

Close Preview
Here's how we would implement Set Fake Singleton:
public class AppLog...
    private static AppLog log;
public static AppLog singleInstance() { if (log == null) createInstance(); return log; }
public static void setSingleInstance(AppLog newLog) { log = newLog; }
public class SomethingTest... @Test public void testWhatever() { AppLog.setSingleInstance(new FakeAppLog()); // … Any uses of AppLog will really use FakeAppLog. }
With this technique, tests replace the singleton. Thus, just like Alternate Construction, they run the same risk of replacing the log for everybody.
Take care to put the real singleton back for subsequent tests!
(Reading...)
| Tweet...
Page rating: | Rate this Page