mocking a singleton class
Of course, I could write something like don't use singleton, they are evil, use Guice/Spring/whatever but first, this wouldn't answer your question and second, you sometimes have to deal with singleton, when using legacy code for example.
So, let's not discuss the good or bad about singleton (there is another question for this) but let's see how to handle them during testing. First, let's look at a common implementation of the singleton:
public class Singleton {
private Singleton() { }
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
public String getFoo() {
return "bar";
}
}
There are two testing problems here:
The constructor is private so we can't extend it (and we can't control the creation of instances in tests but, well, that's the point of singletons).
The
getInstance
is static so it's hard to inject a fake instead of the singleton object in the code using the singleton.
For mocking frameworks based on inheritance and polymorphism, both points are obviously big issues. If you have the control of the code, one option is to make your singleton "more testable" by adding a setter allowing to tweak the internal field as described in Learn to Stop Worrying and Love the Singleton (you don't even need a mocking framework in that case). If you don't, modern mocking frameworks based on interception and AOP concepts allow to overcome the previously mentioned problems.
For example, Mocking Static Method Calls shows how to mock a Singleton using JMockit Expectations.
Another option would be to use PowerMock, an extension to Mockito
or JMock
which allows to mock stuff normally not mock-able like static, final, private or constructor methods. Also you can access the internals of a class.
Mocking a singleton with mockito
What you are asking is not possible because your legacy code relies on a static method getInstance()
and Mockito does not allow to mock static methods, so the following line won't work
when(FormatterService.getInstance()).thenReturn(formatter);
There are 2 ways around this problem:
Use a different mocking tool, such as PowerMock, that allows to mock static methods.
Refactor your code, so that you don't rely on the static method. The least invasive way I can think of to achieve this is by adding a constructor to
DriverSnapshotHandler
that injects aFormatterService
dependency. This constructor will be only used in tests and you production code will continue to use the real singleton instance.public static class DriverSnapshotHandler {
private final FormatterService formatter;
//used in production code
public DriverSnapshotHandler() {
this(FormatterService.getInstance());
}
//used for tests
DriverSnapshotHandler(FormatterService formatter) {
this.formatter = formatter;
}
public String getImageURL() {
return formatter.formatTachoIcon();
}
}
Then, your test should look like this :
FormatterService formatter = mock(FormatterService.class);
when(formatter.formatTachoIcon()).thenReturn("MockedURL");
DriverSnapshotHandler handler = new DriverSnapshotHandler(formatter);
handler.getImageURL();
verify(formatter, atLeastOnce()).formatTachoIcon();
Mocking a singleton in Groovy
You can use global mocks
def publisher = new Publisher()
publisher << new RealSubscriber() << new RealSubscriber()
def anySubscriber = GroovyMock(RealSubscriber, global: true)
when:
publisher.publish("message")
then:
2 * anySubscriber.receive("message")
Mocking a singleton c# class in nunit
Here is the solution:-
using System;
using EFBL;
using NUnit.Framework;
using TypeMock.ArrangeActAssert;
using System.Diagnostics;
namespace EFBL.CalendarIntegration.Google
{
[TestFixture]
public class GoogleSyncEventProcessorSpec
{
public GoogleSyncEventProcessor googleSync;
public GoogleWatchManager googleManager;
[SetUp]
public void Init() {
googleManager = Isolate.Fake.Instance<GoogleWatchManager>();
googleSync = GoogleSyncEventProcessor.Instance;
}
[Test]
public void RequiresThatTwoWaySyncLiveBeFalse()
{
Isolate.NonPublic.Property.WhenGetCalled(googleManager, "IsGoogleTwoWaySynLive").WillReturn(false);
Assert.AreEqual(false, googleManager.IsGoogleTwoWaySynLive);
}
Related Topics
How I Save and Retrieve an Image on My Server in a Java Webapp
Plsql Jdbc: How to Get Last Row Id
Convert String to Int Array in Java
Java - How to Receive Point Coordinates After Mouse Button Release (Jfreechart)
Throw Checked Exceptions from Mocks with Mockito
How to Copy File Inside Jar to Outside the Jar
Check Line for Unprintable Characters While Reading Text File
Check If a File Is Locked in Java
Filter Java Stream to 1 and Only 1 Element
Arrays.Aslist(Int[]) Not Working
Java 'Final' Method: What Does It Promise
Comparing Time Is Incorrect When Picking 12:00
How to Create a Hashmap with Two Keys (Key-Pair, Value)
Does Java Support Inner/Local/Sub Methods