CglibObserver implements FeatureObserver { public <T> T observe(Object toObserve, FeatureContext context) { throwIllegalArgumentExceptionIfNull(context, "FeatureContext instance is required."); throwIllegalArgumentExceptionIfNull(toObserve, "Observable object is required."); Enhancer e = new Enhancer(); e.setSuperclass(toObserve.getClass()); e.setCallback(new CglibMethodInterceptor(toObserve, context)); T proxiedObject = null; if(hasStandardConstructor(toObserve.getClass())) { proxiedObject = (T)e.create(); } else { proxiedObject = createProxyWithFirstUsableConstructor(e, toObserve); } if(proxiedObject == null) { throw new CannotCreateProxyException("CglibObserver did not found a constructor to enhance target class"); } return proxiedObject; }  T observe(Object toObserve, FeatureContext context);  }
@Test(expected = IllegalArgumentException.class) public void observe_WithoutContext_throwsException() { observer.observe(new JavaProxiedClass(), null); }
@Test(expected = IllegalArgumentException.class) public void observe_WithoutObservableObject_throwsException() { observer.observe(null, context); }
@Test public void observe_WithStandardConstructorObject_ReturnsProxy() { CglibObserverTest test = observer.observe(new CglibObserverTest(), context); assertThat(Enhancer.isEnhanced(test.getClass()), is(equalTo(true))); }
@Test public void observe_WithObjectConstructorObject_ReturnsProxy() { CglibProxiedObjectConstructorClass test = observer.observe(new CglibProxiedObjectConstructorClass("test"), context); assertThat(Enhancer.isEnhanced(test.getClass()), is(equalTo(true))); }
@Test public void observe_WithNativeConstructorObject_ReturnsNull() { CglibProxiedNativeConstructorClass test = observer.observe(new CglibProxiedNativeConstructorClass(1), context); assertThat(Enhancer.isEnhanced(test.getClass()), is(equalTo(true))); }