I'm trying to unit test a method that does
public static Context fromLanguageTag(final String languageTag) {
final Context context = new Context();
final Locale locale = Locale.forLanguageTag(languageTag);
context.language = locale.getLanguage().length()==3 ? locale.getLanguage() : locale.getISO3Language();
return context;
}
To test that I need to mock java.util.Locale
. I'm using PowerMock and Mockito:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Locale.class })
public class ContextTest {
public void testFromLanguageTag() throws Exception {
mockStatic(Locale.class);
final Locale mockLocale = mock(Locale.class);
when(mockLocale.getLanguage()).thenReturn(LANGUAGE_3_OUTPUT);
when(mockLocale.getISO3Language()).thenReturn(LANGUAGE_ISO);
when(Locale.forLanguageTag(Mockito.eq(LANGUAGE_TAG_LONG_INPUT))).thenReturn(mockLocale);
final Context c = Context.fromLanguageTag(LANGUAGE_TAG_LONG_INPUT);
assertThat(c.getLanguage()).isEqualTo(LANGUAGE_3_OUTPUT);
}
}
But it seems the mocked method calls from mockLocale
are never called; instead I get a java.util.MissingResourceException
from java.util.Locale.getISO3Language
(which I want to mock). How can fix that?