FileHelper { public static OutputStream openWrite(Context context, String filename) throws IOException { File out = context.getFileStreamPath(filename); out.renameTo(context.getFileStreamPath(filename + ".bak")); return context.openFileOutput(filename, Context.MODE_PRIVATE); }  static InputStream openRead(Context context, String filename); static OutputStream openWrite(Context context, String filename); static Configuration loadCurrentSettings(Context context); static Configuration loadPreviousSettings(Context context); static Configuration loadDefaultSettings(Context context); static void writeSettings(Context context, Configuration config); static File getItemFile(Context context, Configuration.Item item); static InputStreamReader openItemFile(Context context, Configuration.Item item); static int poll(StructPollfd[] fds, int timeout); static FileDescriptor closeOrWarn(FileDescriptor fd, String tag, String message); static T closeOrWarn(T fd, String tag, String message);  }
@Test public void testOpenWrite() throws Exception { File file = mock(File.class); File file2 = mock(File.class); FileOutputStream fos = mock(FileOutputStream.class); when(mockContext.getFileStreamPath(eq("filename"))).thenReturn(file); when(mockContext.getFileStreamPath(eq("filename.bak"))).thenReturn(file2); when(mockContext.openFileOutput(eq("filename"), anyInt())).thenReturn(fos); assertSame(fos, FileHelper.openWrite(mockContext, "filename")); Mockito.verify(file).renameTo(file2); Mockito.verify(mockContext).openFileOutput(eq("filename"), anyInt()); }