Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure mock is of correct type #15092

Merged
merged 1 commit into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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