### Question:
OpenWeatherMapApiKey { public String getKey() { String key = getKeyFromPreferences(); if (key == null) { key = getKeyFromResources(); } if (key == null) { key = getDefaultKey(); } return key; } OpenWeatherMapApiKey(Context context); String getKey(); static final String PREFERENCE_KEY; }### Answer:
@Test public void testDefaultKey() { OpenWeatherMapApiKey key = new OpenWeatherMapApiKey(context); assertTrue(key.getKey().startsWith("4aba43")); }
@Test public void testKeyFromPreferences() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); prefs.edit().putString(OpenWeatherMapApiKey.PREFERENCE_KEY, "test_key").commit(); OpenWeatherMapApiKey key = new OpenWeatherMapApiKey(context); assertEquals("test_key", key.getKey()); prefs.edit().remove(OpenWeatherMapApiKey.PREFERENCE_KEY).commit(); }
@Test public void testEmptyKeyInPreferences() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); prefs.edit().putString(OpenWeatherMapApiKey.PREFERENCE_KEY, "").commit(); OpenWeatherMapApiKey key = new OpenWeatherMapApiKey(context); assertTrue(key.getKey().startsWith("4aba43")); prefs.edit().remove(OpenWeatherMapApiKey.PREFERENCE_KEY).commit(); }