简体   繁体   中英

Mocking static method in other static method

I have small class that return Properties object populated by message_xx.properties located in my classpath:

public class PropertiesUtils {

  public static Properties loadProperties(Locale locale) {
    try {
      ClassLoader loader = getClassLoader();
      Properties properties = new Properties();
      String filePath = "messages_" + locale.getLanguage().toLowerCase() + ".properties";
      InputStream is = loader.getResourceAsStream(filePath);
      properties.load(is);
      is.close();
      return properties;
    } catch (Exception e) {
      log.error("Error loading properties by locale "+locale, e);
      return null;
    }
  }

  public static ClassLoader getClassLoader(){
    return PropertiesUtils.class.getClassLoader();
  }
}

Everything works fine, but I would like to create Unit Test for this. I want to mock method "getClassLoader" to return some specific InputStream that I want in my junit test context.

@RunWith(PowerMockRunner.class)
@PrepareForTest(value=PropertiesUtils.class)
public class PropertiesUtilsTest {

    @Mock
    private Locale zuluLocale;
    @Mock
    private Locale etruscanLocale;

    @Mock
    private ClassLoader mockClassLoader;

    @Before
    public void init(){

        Mockito.when(zuluLocale.getLanguage()).thenReturn("zulu");
        Mockito.when(etruscanLocale.getLanguage()).thenReturn("etruscan");

        Mockito.when(mockClassLoader.getResourceAsStream("messages_zulu.properties")).thenReturn(IOUtils.toInputStream("2=esal"));
        Mockito.when(mockClassLoader.getResourceAsStream("messages_etruscan.properties")).thenReturn(IOUtils.toInputStream("2=ezimbili"));

        PowerMockito.mockStatic(PropertiesUtils.class);
        PowerMockito.when(PropertiesUtils.getClassLoader()).thenReturn(mockClassLoader);
    }

    @After
    public void finalize(){
        zuluLocale = null;
        etruscanLocale = null;
    }

    @Test
    public void loadPropertiesTest(){
        Assert.assertNull(PropertiesUtils.loadProperties(new Locale("en")));
        Assert.assertNotNull(PropertiesUtils.loadProperties(zuluLocale));
        Assert.assertNotNull(PropertiesUtils.loadProperties(etruscanLocale));

        Assert.assertEquals("esal", PropertiesUtils.loadProperties(zuluLocale).getProperty("2"));
        Assert.assertEquals("ezimbili", PropertiesUtils.loadProperties(etruscanLocale).getProperty("2"));
    }
}

This test fails as AssertionFailedError in line:

Assert.assertNotNull(PropertiesUtils.loadProperties(zuluLocale));

OK, it was dummy, question, all I need to do is:

PowerMockito.when(PropertiesUtils.loadProperties(zuluLocale)).thenCallRealMethod();
PowerMockito.when(PropertiesUtils.loadProperties(etruscanLocale)).thenCallRealMethod();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM