Skip to content

Instantly share code, notes, and snippets.

/*Example of how you can rewrite your code to avoid the need for mocks in your tests */
/*-----------------------------------------------
Implementation where you need mocks for testing
-------------------------------------------------*/
public class MyClassThatNeedsTesting {
private BusinessLogicClass blc;
public int estimatedTotalCost(SomeDataStructure input) {
import java.util.WeakHashMap;
public class PubSub {
static @FunctionalInterface interface Subscriber<T> {
public void hereYouGo(T message);
}
static class Publisher<T> {
WeakHashMap<Subscriber<T>, Boolean> subscribers = new WeakHashMap<>();
@ChristinGorman
ChristinGorman / gist:3a2a7ddae9230202a765
Last active August 29, 2015 14:18
GC of lambdas in java 8
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;
public class Test {
/**
* Still don't know why the first two lambdas don't get cleared by the GC :(
*/
public static void main(String[] args) throws Exception {
@ChristinGorman
ChristinGorman / gist:6890462
Last active December 25, 2015 00:49
Attempt at function pointers in java :-)
package no.gorman.stop.it;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
/**
* I like using Mockito in tests and wondered if I could create