PowerMock - Maquette d'un Singleton avec un Constructeur Privé

Je suis en utilisant PowerMock avec EasyMock, et je me demandais comment je pourrais maquette d'un singleton avec un constructeur privé?

Disons que j'ai la classe suivante:

public class Singleton {
    private static Singleton singleton = new Singleton();
    private Singleton() { }

    public static Singleton getInstance() {
        return singleton;
    }

    public int crazyServerStuff() { ... }
}

Et une classe qui utilise ce:

public class Thing {
    public Thing() {}

    public int doStuff(Singleton s) {
        return s.crazyServerStuff() + 42;
    }
}

Comment pourrais-je en dérision le crazyServerStuff méthode?

J'ai essayé ce qui suit:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Singleton.class)
public class ThingTest extends AndroidTestCase {
    @Test
    public void testDoStuff() {
        MemberModifier.suppress(MemberModifier.constructor(Singleton.class));
        Singleton mockSingleton = PowerMock.createMock(Singleton.class);

        ...
    }
}

Mais je reçois l'erreur java.lang.IllegalArgumentException: No visible constructors in class Singleton

Personne ne sait ce que je suis absent?

InformationsquelleAutor Jack | 2014-11-24