org.test4j.integration.spring.faker.FakeReferenceBean Maven / Gradle / Ivy
package org.test4j.integration.spring.faker;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.test4j.mock.Invocation;
import org.test4j.mock.Mock;
import org.test4j.mock.MockUp;
import static org.test4j.integration.spring.SpringStartup.ReferenceBean_Name;
/**
* 替换dubbo reference bean
*
* @author darui.wu
*/
public class FakeReferenceBean extends MockUp {
public FakeReferenceBean() {
super(ReferenceBean_Name);
}
private transient ApplicationContext context;
@Mock
public void setApplicationContext(Invocation inv, ApplicationContext applicationContext) {
this.context = applicationContext;
inv.proceed();
}
/**
* 对 ReferenceBean 的getObject方法的Mock
*
* @param inv
* @return
* @throws Exception
*/
@Mock
public Object getObject(Invocation inv) {
FactoryBean factory = inv.getTarget();
Class clazz = factory.getObjectType();
try {
return context.getBean(clazz);
} catch (NoSuchBeanDefinitionException e) {
return inv.proceed();
}
}
}