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!)