@Test public void shouldProxifyConcreteClassesWithDefaultConstructors() { Proxifier proxifier = new ObjenesisProxifier(); TheClass proxy = proxifier.proxify( TheClass.class, new MethodInvocation<TheClass>() { public Object intercept( TheClass proxy, Method method, Object[] args, SuperMethod superMethod) { return true; } }); assertTrue(proxy.wasCalled()); }
@Test public void shouldProxifyInterfaces() { Proxifier proxifier = new ObjenesisProxifier(); TheInterface proxy = proxifier.proxify( TheInterface.class, new MethodInvocation<TheInterface>() { public Object intercept( TheInterface proxy, Method method, Object[] args, SuperMethod superMethod) { return true; } }); assertTrue(proxy.wasCalled()); }
@Test public void shouldNotProxifyJavaLangObjectMethods() throws Exception { Proxifier proxifier = new DefaultProxifier(); Object proxy = proxifier.proxify( DefaultProxifierTest.class, new MethodInvocation() { public Object intercept( Object proxy, Method method, Object[] args, SuperMethod superMethod) { Assert.fail("should not call this Method interceptor"); return null; } }); new Mirror().on(proxy).invoke().method("finalize").withoutArgs(); }
@Test public void shouldTryAllConstructorsInDeclarationOrder() { Proxifier proxifier = new DefaultProxifier(); TheClassWithManyConstructors proxy = (TheClassWithManyConstructors) proxifier.proxify( TheClassWithManyConstructors.class, new MethodInvocation() { public Object intercept( Object proxy, Method method, Object[] args, SuperMethod superMethod) { return superMethod.invoke(proxy, args); } }); assertTrue(proxy.wasNumberConstructorCalled()); assertThat(proxy.getNumber(), is(nullValue())); }
@Test public void shouldProxifyConcreteClassesWithComplexConstructorsAndPassNullForAllParameters() { Proxifier proxifier = new DefaultProxifier(); TheClassWithComplexConstructor proxy = (TheClassWithComplexConstructor) proxifier.proxify( TheClassWithComplexConstructor.class, new MethodInvocation() { public Object intercept( Object proxy, Method method, Object[] args, SuperMethod superMethod) { return superMethod.invoke(proxy, args); } }); assertThat(proxy.getFirstDependency(), is(nullValue())); assertThat(proxy.getSecondDependency(), is(nullValue())); }
@Test public void shouldNeverCallSuperclassConstructors() { Proxifier proxifier = new ObjenesisProxifier(); TheClassWithManyConstructors proxy = proxifier.proxify( TheClassWithManyConstructors.class, new MethodInvocation<TheClassWithManyConstructors>() { public Object intercept( TheClassWithManyConstructors proxy, Method method, Object[] args, SuperMethod superMethod) { return superMethod.invoke(proxy, args); } }); assertFalse(proxy.wasNumberConstructorCalled()); assertThat(proxy.getNumber(), is(nullValue())); }