HockeyApp extends ServiceImpl implements CrashReportingService { File writeHockeyAppCrashLog(String stackTrace) { File logFile = null; BufferedWriter writer; try { File outputDir = context.getCacheDir(); logFile = File.createTempFile("hockey_app_crash_", ".log", outputDir); writer = new BufferedWriter(new FileWriter(logFile)); writer.write("Package: " + utils.getApplicationPackageName(context) + "\n"); writer.write("Version Code: " + utils.getVersionCode(context) + "\n"); writer.write("Version Name: " + utils.getVersionName(context) + "\n"); writer.write("Android: " + Build.VERSION.RELEASE + "\n"); writer.write("Manufacturer: " + Build.MANUFACTURER + "\n"); writer.write("Model: " + Build.MODEL + "\n"); writer.write("Date: " + getTimestamp() + "\n"); writer.write("\n"); writer.write(stackTrace); writer.close(); } catch (Exception ex) { utils.logIssue("Error writing crash report to temp log file", ex); } return logFile; } HockeyApp(Context context); @Override void enable(String apiKey); @Override boolean isEnabled(); @Override ServiceType getServiceType(); void logEvent(final StoredException storedException, final Callback<Object> deleteStoredExceptionCallback); HockeyAppService createService();  }
@Test public void testWriteHockeyAppCrashLog() { Context mockContext = mock(Context.class); File systemTempDir = new File(System.getProperty("java.io.tmpdir")); when(mockContext.getCacheDir()).thenReturn(systemTempDir); String testStackTrace = "unit\ntesting\nstack\ntrace"; HockeyApp hockeyApp = new HockeyApp(mockContext){ @Override String getTimestamp() { return "Mon May 18 13:31:10 EDT 2015"; } }; hockeyApp.utils = UnitTestUtils.mockUtils(); File file = hockeyApp.writeHockeyAppCrashLog(testStackTrace); assertNotNull(file); assertTrue(file.exists()); assertTrue(file.canRead()); String fileText = null; try { fileText = new Scanner(file).useDelimiter("\\A").next(); } catch (FileNotFoundException e) { fail(); } String expected = "Package: com.percolate.foam.unit.testing\n" + "Version Code: 111\n" + "Version Name: 1.1.1\n" + "Android: null\n" + "Manufacturer: null\n" + "Model: null\n" + "Date: Mon May 18 13:31:10 EDT 2015\n" + "\n" + "unit\n" + "testing\n" + "stack\n" + "trace"; assertEquals(expected, fileText); assertTrue("Failed to clean up unit test file [" + file.getAbsolutePath() + "].", file.delete()); }