|
| 1 | +package graphql.kickstart.spring.error; |
| 2 | + |
| 3 | +import graphql.kickstart.execution.error.GraphQLErrorHandler; |
| 4 | +import org.assertj.core.api.Assertions; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.mockito.Mockito; |
| 7 | +import org.springframework.boot.SpringApplication; |
| 8 | +import org.springframework.boot.context.event.ApplicationReadyEvent; |
| 9 | +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
| 10 | + |
| 11 | +public class GraphQLErrorStartupListenerTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + void error_handler_is_not_overridden_when_present() { |
| 15 | + GraphQLErrorHandler expectedErrorHandler = Mockito.mock(GraphQLErrorHandler.class); |
| 16 | + ErrorHandlerSupplier errorHandlerSupplier = new ErrorHandlerSupplier(expectedErrorHandler); |
| 17 | + GraphQLErrorStartupListener graphQLErrorStartupListener = new GraphQLErrorStartupListener(errorHandlerSupplier, false); |
| 18 | + graphQLErrorStartupListener.onApplicationEvent(getApplicationReadyEvent()); |
| 19 | + Assertions.assertThat(errorHandlerSupplier.get()).isEqualTo(expectedErrorHandler); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + void error_handler_is_set_when_not_present() { |
| 24 | + ErrorHandlerSupplier errorHandlerSupplier = new ErrorHandlerSupplier(null); |
| 25 | + GraphQLErrorStartupListener graphQLErrorStartupListener = new GraphQLErrorStartupListener(errorHandlerSupplier, false); |
| 26 | + graphQLErrorStartupListener.onApplicationEvent(getApplicationReadyEvent()); |
| 27 | + Assertions.assertThat(errorHandlerSupplier.get()).isNotNull(); |
| 28 | + } |
| 29 | + |
| 30 | + private ApplicationReadyEvent getApplicationReadyEvent() { |
| 31 | + AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext(); |
| 32 | + annotationConfigWebApplicationContext.refresh(); |
| 33 | + return new ApplicationReadyEvent(new SpringApplication(), new String[0], annotationConfigWebApplicationContext); |
| 34 | + } |
| 35 | + |
| 36 | +} |
0 commit comments