Skip to content

Instantly share code, notes, and snippets.

@Kotlin-Native
Created July 15, 2015 13:04
Show Gist options
  • Select an option

  • Save Kotlin-Native/7f774c1ed09b59655ad8 to your computer and use it in GitHub Desktop.

Select an option

Save Kotlin-Native/7f774c1ed09b59655ad8 to your computer and use it in GitHub Desktop.
import static org.junit.Assert.assertEquals;
import java.lang.reflect.Method;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;
public class Assert {
public static void assertThat(T actual,
Matcher matcher) {
MatcherAssert.assertThat(actual, matcher);
}
public static T assertThat(T object) {
return assertThat(object, true);
}
public static T assertThatNot(T object) {
return assertThat(object, false);
}
private static T assertThat(final T object,
final boolean value) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(object.getClass());
enhancer.setCallback(new InvocationHandler() {
@Override
public Object invoke(Object proxy,
Method method,
Object[] args) throws Throwable {
if (method.getReturnType() != Boolean.TYPE) {
throw new IllegalStateException(
"Wrong usage of assertThat for method "
+ method.getName()
+ ". Please use with boolean methods only");
}
boolean result
= (Boolean)method.invoke(object, args);
assertEquals("result of method "
+ method.getName(), value, result);
return result;
}
});
return (T) enhancer.create();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment