Skip to content

Instantly share code, notes, and snippets.

@dimhold
Created June 11, 2013 19:44
Show Gist options
  • Save dimhold/5759968 to your computer and use it in GitHub Desktop.
Save dimhold/5759968 to your computer and use it in GitHub Desktop.
Util class for unit tests (gist:5759950) just for understand: What is a true way of unit testing
package how.to.use.mockito;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
public class Util {
public static void downloadInFile(String url, String file) {
try (
ReadableByteChannel byteChannel = Channels.newChannel(new URL(url).openStream());
FileOutputStream fileOutputStream = new FileOutputStream(file);
) {
fileOutputStream.getChannel().transferFrom(byteChannel, 0, Long.MAX_VALUE);
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean isMemoryEnough() {
long totalMemory = Runtime.getRuntime().totalMemory();
long freeMemory = Runtime.getRuntime().freeMemory();
double freeMemoryInPercent = (double) freeMemory/(double) totalMemory;
if (freeMemoryInPercent > AppConfig.MEMORY_ENOUGH_IN_PERCENT_LIMIT) {
return true;
} else {
return false;
}
}
class AppConfig {
public static final double MEMORY_ENOUGH_IN_PERCENT_LIMIT = 0.48;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment