Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.
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 @@ -9,6 +9,7 @@
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -47,6 +48,7 @@ private List<GraphQLErrorFactory> scanForExceptionHandlers(
return emptyList();
}
return Arrays.stream(objClz.getDeclaredMethods())
.map(method -> AopUtils.getMostSpecificMethod(method, objClz))
.filter(ReflectiveMethodValidator::isGraphQLExceptionHandler)
.map(method -> withReflection(context.getBean(name), method))
.collect(toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand All @@ -29,13 +30,14 @@ public void setup() {
Mockito.when(applicationContext.getBeanFactory()).thenReturn(beanFactory);
Mockito.when(beanFactory.getBeanDefinitionNames()).thenReturn(new String[] {"Test"});
Mockito.when(applicationContext.containsBean("Test")).thenReturn(true);
Mockito.doReturn(TestClass.class).when(applicationContext).getType("Test");

errorHandlerFactory = new GraphQLErrorHandlerFactory();
}

@Test
void createFindsCollectionHandler() {
Mockito.doReturn(TestClass.class).when(applicationContext).getType("Test");

GraphQLErrorHandler handler = errorHandlerFactory.create(applicationContext, true);
assertThat(handler).isInstanceOf(GraphQLErrorFromExceptionHandler.class);
GraphQLErrorFromExceptionHandler errorHandler = (GraphQLErrorFromExceptionHandler) handler;
Expand All @@ -44,6 +46,23 @@ void createFindsCollectionHandler() {
.isNotEmpty();
}

@Test
void createFindsExceptionHandlerEvenIfProxy() {
Mockito.doReturn(proxyTestClass()).when(applicationContext).getType("Test");

GraphQLErrorHandler handler = errorHandlerFactory.create(applicationContext, true);
assertThat(handler).isInstanceOf(GraphQLErrorFromExceptionHandler.class);
GraphQLErrorFromExceptionHandler errorHandler = (GraphQLErrorFromExceptionHandler) handler;
assertThat(errorHandler.getFactories())
.as("handler.factories should not be empty")
.isNotEmpty();
}

private Class<? extends TestClass> proxyTestClass() {
TestClass proxy = (TestClass) new ProxyFactory(new TestClass()).getProxy();
return proxy.getClass();
}

public static class TestClass {

@ExceptionHandler(IllegalArgumentException.class)
Expand Down