Skip to content

Commit

Permalink
Make sure mock is of correct type
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Feb 15, 2021
1 parent c16567c commit 8305da8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Collection<Resource> generate(BeanInfo bean, String beanClassName,
FieldCreator beanField = clientProxy.getFieldCreator(BEAN_FIELD, DescriptorUtils.extToInt(beanClassName))
.setModifiers(ACC_PRIVATE | ACC_FINAL);
if (mockable) {
clientProxy.getFieldCreator(MOCK_FIELD, Object.class).setModifiers(ACC_PRIVATE | ACC_VOLATILE);
clientProxy.getFieldCreator(MOCK_FIELD, providerType.descriptorName()).setModifiers(ACC_PRIVATE | ACC_VOLATILE);
}
FieldCreator contextField = null;
if (BuiltinScope.APPLICATION.is(bean.getScope())) {
Expand All @@ -133,7 +133,7 @@ Collection<Resource> generate(BeanInfo bean, String beanClassName,
implementGetContextualInstance(clientProxy, providerType);
implementGetBean(clientProxy, beanField.getFieldDescriptor());
if (mockable) {
implementMockMethods(clientProxy);
implementMockMethods(clientProxy, providerType);
}

for (MethodInfo method : getDelegatingMethods(bean, bytecodeTransformerConsumer, transformUnproxyableClasses)) {
Expand Down Expand Up @@ -231,17 +231,19 @@ Collection<Resource> generate(BeanInfo bean, String beanClassName,
return classOutput.getResources();
}

private void implementMockMethods(ClassCreator clientProxy) {
private void implementMockMethods(ClassCreator clientProxy, ProviderType providerType) {
MethodCreator clear = clientProxy
.getMethodCreator(MethodDescriptor.ofMethod(clientProxy.getClassName(), CLEAR_MOCK_METHOD_NAME, void.class));
clear.writeInstanceField(FieldDescriptor.of(clientProxy.getClassName(), MOCK_FIELD, Object.class), clear.getThis(),
clear.writeInstanceField(FieldDescriptor.of(clientProxy.getClassName(), MOCK_FIELD, providerType.descriptorName()),
clear.getThis(),
clear.loadNull());
clear.returnValue(null);

MethodCreator set = clientProxy
.getMethodCreator(
MethodDescriptor.ofMethod(clientProxy.getClassName(), SET_MOCK_METHOD_NAME, void.class, Object.class));
set.writeInstanceField(FieldDescriptor.of(clientProxy.getClassName(), MOCK_FIELD, Object.class), set.getThis(),
set.writeInstanceField(FieldDescriptor.of(clientProxy.getClassName(), MOCK_FIELD, providerType.descriptorName()),
set.getThis(),
set.getMethodParam(0));
set.returnValue(null);
}
Expand All @@ -268,7 +270,8 @@ void implementDelegate(ClassCreator clientProxy, ProviderType providerType, Fiel
if (mockable) {
//if mockable and mocked just return the mock
ResultHandle mock = creator.readInstanceField(
FieldDescriptor.of(clientProxy.getClassName(), MOCK_FIELD, Object.class.getName()), creator.getThis());
FieldDescriptor.of(clientProxy.getClassName(), MOCK_FIELD, providerType.descriptorName()),
creator.getThis());
BytecodeCreator falseBranch = creator.ifNull(mock).falseBranch();
falseBranch.returnValue(falseBranch.checkCast(mock, providerType.className()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ static <T> void installMock(T instance, T mock) {
if (inst == null) {
throw new IllegalStateException("No test in progress");
}
if (!(instance.getClass().getSuperclass().isAssignableFrom(mock.getClass()))) {
throw new RuntimeException(mock
+ " is not assignable to type " + instance.getClass().getSuperclass());
}
try {
Method setMethod = instance.getClass().getDeclaredMethod("arc$setMock", Object.class);
setMethod.invoke(instance, mock);
Expand Down

0 comments on commit 8305da8

Please sign in to comment.