ExceptionPersister { public void store(StoredException storedException){ String fileName = "FoamStoredException-" + storedException.platform.toString() + System.currentTimeMillis() + new Random().nextInt(); OutputStream out = null; try { String json = new Gson().toJson(storedException); out = context.openFileOutput(fileName, Context.MODE_PRIVATE); final OutputStreamWriter writer = new OutputStreamWriter(out, "ISO8859_1"); writer.write(json); writer.flush(); } catch (Exception ex) { utils.logIssue("Could not write exception to a file.", ex); } finally { closeStream(out); } } ExceptionPersister(Context context); Map<String, StoredException> loadAll(); void store(StoredException storedException);  }
@Test public void testStore() throws IOException { Context mockContext = mock(Context.class); File tempFile = File.createTempFile("foam_unit_testing_stored_exception", ".txt"); FileOutputStream unitTestOutputStream = new FileOutputStream(tempFile); when(mockContext.openFileOutput(anyString(), anyInt())).thenReturn(unitTestOutputStream); ExceptionPersister exceptionPersister = new ExceptionPersister(mockContext); StoredException storedException = new StoredException(ServiceType.MIXPANEL, "unit_testing_message", "unit_testing_threadname", "unit\ntesting\nstack\ntrace"); exceptionPersister.store(storedException); Scanner scanner = new Scanner(new FileReader(tempFile)); StringBuilder fileContents = new StringBuilder(); while(scanner.hasNext()){ fileContents.append(scanner.next()); } scanner.close(); assertTrue(fileContents.length() > 0); String expected = "{" + "\"platform\":\"MIXPANEL\"," + "\"message\":\"unit_testing_message\"," + "\"threadName\":\"unit_testing_threadname\"," + "\"stackTrace\":\"unit\\ntesting\\nstack\\ntrace\"" + "}"; assertEquals(expected, fileContents.toString()); assertTrue("Unable to clean up unit testing temp file [" + tempFile.getAbsolutePath() + "].", tempFile.delete()); }