### Question:
LoggingUtils { static void addNameAndVersion( @NonNull Map<String, Object> paramsObject, @NonNull Context context) { Context applicationContext = context.getApplicationContext(); if (applicationContext != null && applicationContext.getPackageManager() != null) { try { PackageInfo info = applicationContext.getPackageManager().getPackageInfo( applicationContext.getPackageName(), 0); String nameString = null; if (info.applicationInfo != null) { CharSequence name = info.applicationInfo.loadLabel(applicationContext.getPackageManager()); if (name != null) { nameString = name.toString(); } paramsObject.put(FIELD_APP_NAME, nameString); } if (StripeTextUtils.isBlank(nameString)) { paramsObject.put(FIELD_APP_NAME, info.packageName); } paramsObject.put(FIELD_APP_VERSION, info.versionCode); } catch (PackageManager.NameNotFoundException nameNotFound) { paramsObject.put(FIELD_APP_NAME, UNKNOWN); paramsObject.put(FIELD_APP_VERSION, UNKNOWN); } } else { paramsObject.put(FIELD_APP_NAME, NO_CONTEXT); paramsObject.put(FIELD_APP_VERSION, NO_CONTEXT); } }    }### Answer:
@Test public void addNameAndVersion_whenApplicationContextIsNull_addsNoContextValues() { Context context = mock(Context.class); when(context.getApplicationContext()).thenReturn(null); Map<String, Object> paramsMap = new HashMap<>(); LoggingUtils.addNameAndVersion(paramsMap, context); assertEquals(LoggingUtils.NO_CONTEXT, paramsMap.get(LoggingUtils.FIELD_APP_NAME)); assertEquals(LoggingUtils.NO_CONTEXT, paramsMap.get(LoggingUtils.FIELD_APP_VERSION)); }
@Test public void addNameAndVersion_whenPackageInfoNotFound_addsUnknownValues() { Context context = mock(Context.class); when(context.getApplicationContext()).thenReturn(context); final String dummyName = "dummy_name"; PackageManager manager = mock(PackageManager.class); when(context.getPackageManager()).thenReturn(manager); when(context.getPackageName()).thenReturn(dummyName); try { when(manager.getPackageInfo(dummyName, 0)) .thenThrow(new PackageManager.NameNotFoundException()); } catch (PackageManager.NameNotFoundException namex) { fail("Unexpected exception thrown."); } Map<String, Object> paramsMap = new HashMap<>(); LoggingUtils.addNameAndVersion(paramsMap, context); assertEquals(LoggingUtils.UNKNOWN, paramsMap.get(LoggingUtils.FIELD_APP_NAME)); assertEquals(LoggingUtils.UNKNOWN, paramsMap.get(LoggingUtils.FIELD_APP_VERSION)); }