下面列出了怎么用org.mockito.cglib.core.NamingPolicy的API类实例代码及写法,或者点击链接到github查看源代码。
private static Class<?> createDynamicClassForMetric(final String className, Map<String, Class<?>> dynamicFieldTypes){
BeanGenerator beanGenerator = new BeanGenerator();
beanGenerator.setNamingPolicy(new NamingPolicy(){
@Override
public String getClassName(String prefix,String source, Object key, Predicate names){
return className;
}});
BeanGenerator.addProperties(beanGenerator, dynamicFieldTypes);
beanGenerator.setSuperclass(TaggedLogAPIEntity.class);
return (Class<?>) beanGenerator.createClass();
}
private static Class<?> createDynamicClassForMetric(final String className,
Map<String, Class<?>> dynamicFieldTypes) {
BeanGenerator beanGenerator = new BeanGenerator();
beanGenerator.setNamingPolicy(new NamingPolicy() {
@Override
public String getClassName(String prefix, String source, Object key, Predicate names) {
return className;
}
});
BeanGenerator.addProperties(beanGenerator, dynamicFieldTypes);
beanGenerator.setSuperclass(TaggedLogAPIEntity.class);
return (Class<?>)beanGenerator.createClass();
}
@Test
public void shouldSetMockitoNamingPolicy() throws Exception {
//given
MockitoMethodProxy methodProxy = new MethodProxyBuilder().build();
//when
new CGLIBHacker().setMockitoNamingPolicy(methodProxy);
//then
Object realMethodProxy = Whitebox.invokeMethod(methodProxy, "getMethodProxy", new Object[0]);
Object createInfo = Whitebox.getInternalState(realMethodProxy, "createInfo");
NamingPolicy namingPolicy = (NamingPolicy) Whitebox.getInternalState(createInfo, "namingPolicy");
assertEquals(MockitoNamingPolicy.INSTANCE, namingPolicy);
}
@Test
public void shouldSetMockitoNamingPolicyEvenIfMethodProxyIsProxied() throws Exception {
//given
MockitoMethodProxy proxiedMethodProxy = spy(new MethodProxyBuilder().build());
//when
new CGLIBHacker().setMockitoNamingPolicy(proxiedMethodProxy);
//then
Object realMethodProxy = Whitebox.invokeMethod(proxiedMethodProxy, "getMethodProxy", new Object[0]);
Object createInfo = Whitebox.getInternalState(realMethodProxy, "createInfo");
NamingPolicy namingPolicy = (NamingPolicy) Whitebox.getInternalState(createInfo, "namingPolicy");
assertEquals(MockitoNamingPolicy.INSTANCE, namingPolicy);
}
@Test
public void shouldSetMockitoNamingPolicy() throws Exception {
//given
MockitoMethodProxy methodProxy = new MethodProxyBuilder().build();
//when
new CGLIBHacker().setMockitoNamingPolicy(methodProxy);
//then
Object realMethodProxy = Whitebox.invokeMethod(methodProxy, "getMethodProxy", new Object[0]);
Object createInfo = Whitebox.getInternalState(realMethodProxy, "createInfo");
NamingPolicy namingPolicy = (NamingPolicy) Whitebox.getInternalState(createInfo, "namingPolicy");
assertEquals(MockitoNamingPolicy.INSTANCE, namingPolicy);
}
@Test
public void shouldSetMockitoNamingPolicyEvenIfMethodProxyIsProxied() throws Exception {
//given
MockitoMethodProxy proxiedMethodProxy = spy(new MethodProxyBuilder().build());
//when
new CGLIBHacker().setMockitoNamingPolicy(proxiedMethodProxy);
//then
Object realMethodProxy = Whitebox.invokeMethod(proxiedMethodProxy, "getMethodProxy", new Object[0]);
Object createInfo = Whitebox.getInternalState(realMethodProxy, "createInfo");
NamingPolicy namingPolicy = (NamingPolicy) Whitebox.getInternalState(createInfo, "namingPolicy");
assertEquals(MockitoNamingPolicy.INSTANCE, namingPolicy);
}