Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Of course, I could write something like <em>don't use singleton, they are evil, use Guice/Spring/whatever</em> but first, this wouldn't answer your question and second, you sometimes <strong>have to</strong> deal with singleton, when using legacy code for example. </p> <p>So, let's not discuss the good or bad about singleton (there is another <a href="https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons">question</a> for this) but let's see how to handle them during testing. First, let's look at a common implementation of the singleton:</p> <pre><code>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"; } } </code></pre> <p>There are two testing problems here:</p> <ol> <li><p>The constructor is private so we we can't extend it (and we can't control the creation of instances in tests but, well, that's the point of singletons).</p></li> <li><p>The <code>getInstance</code> is static so it's hard to inject a fake instead of the singleton object <strong>in the code using the singleton</strong>.</p></li> </ol> <p>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 <a href="http://blog.jayway.com/2010/01/15/learn-to-stop-worrying-and-love-the-singleton/" rel="noreferrer">Learn to Stop Worrying and Love the Singleton</a> (you don't even need a mocking framework in that case). If you don't, <strong>modern</strong> mocking frameworks based on interception and AOP concepts allow to overcome the previously mentioned problems.</p> <p>For example, <a href="http://www.weblogism.com/item/254/mocking-static-method-calls" rel="noreferrer">Mocking Static Method Calls</a> shows how to mock a Singleton using <a href="http://jmockit.googlecode.com/svn-history/r2177/trunk/www/tutorial/BehaviorBasedTesting.html#expectation" rel="noreferrer">JMockit Expectations</a>.</p> <p>Another option would be to use <a href="https://github.com/jayway/powermock" rel="noreferrer">PowerMock</a>, an extension to Mockito or JMock which allows to mock stuff normally not mockable like static, final, private or constructor methods. Also you can access the internals of a class. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload