Mocking a private constructor

2019-05-07 00:47发布

问题:

The Site class is provided to me by an external team and has a private constructor.

public class Site
{
   int id;
   String brand;

   private Site(int id, String brand)
   {
      this.id = id;
      this.brand = brand;
   }
}

The SiteUtil class (controlled by team) is

public class SiteUtil
{
   public static Site getSite()
   {
     Site site;
     //Logic
     return site; 
   }
 }

The data the getSite() function applies it logic on requires a network call, therefore it needs to be mocked. It doesn't have a setter currently (maybe to maintain consistency with the data source, not so sure)

I mock it as follows

Site mockSite = new Site(1,"Google");
PowerMockito.when(SiteUtil.getSite(1)).thenReturn(mockSite);

The code above of course dosent compile as I use the public constructor. The solution I read was to mock the private constructor of Site object. I'm however at a loss on how to do that (First time writing unit tests!)

回答1:

Assuming that your code accesses to the value of id and brand only through getters, you could simply mock your class Site then return this mock when you call the static method SiteUtil.getSite() as next:

// Use the launcher of powermock
@RunWith(PowerMockRunner.class)
public class MyTestClass {

    @Test
    // Prepare the class for which we want to mock a static method
    @PrepareForTest(SiteUtil.class)
    public void myTest() throws Exception{
        // Build the mock of Site
        Site mockSite = PowerMockito.mock(Site.class);
        // Define the return values of the getters of our mock
        PowerMockito.when(mockSite.getId()).thenReturn(1);
        PowerMockito.when(mockSite.getBrand()).thenReturn("Google");
        // We use spy as we only want to mock one specific method otherwise
        // to mock all static methods use mockStatic instead
        PowerMockito.spy(SiteUtil.class);
        // Define the return value of our static method
        PowerMockito.when(SiteUtil.getSite()).thenReturn(mockSite);

        ...
    }
}


回答2:

As an alternate approach, there's a way to compatibly alter their API if you can get management to back you. Instead of hiding the network lookups in the getSite() method, externalize them into a SiteLookupStrategy:

public class SiteUtil {
    private static SiteLookupStrategy strategy = new DefaultSiteLookupStrategy();

    public static Site getSite(int siteNum) {
        return strategy.lookup(siteNum);
    }

    public static void setLookupStrategy(SiteLookupStrategy strategy) {
        SiteUtil.strategy = strategy;
    }
}

This way, for testing, you could inject your own (mocked) strategy, but existing clients of the code would not need to be changed. (This also has the advantage of making the lookup itself easier to test for that group.)