From 0ad6b0e87e695e90ab9f0c8df28a49101cd00d70 Mon Sep 17 00:00:00 2001 From: Sergio del Amo Date: Fri, 1 Dec 2023 15:49:53 +0100 Subject: [PATCH] check if bean of type ResourceLoader exists (#906) * check if bean of type ResourceLoader exists Core upate to test 4.1.0 fails with: ``` io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [io.micronaut.core.io.ResourceLoader] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor). at io.micronaut.context.DefaultBeanContext.newNoSuchBeanException(DefaultBeanContext.java:2773) at io.micronaut.context.DefaultApplicationContext.newNoSuchBeanException(DefaultApplicationContext.java:304) at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2735) at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1729) at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:856) at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:848) at io.micronaut.test.support.sql.TestSqlAnnotationHandler.handle(TestSqlAnnotationHandler.java:66) at io.micronaut.test.extensions.AbstractMicronautExtension.afterTestMethod(AbstractMicronautExtension.java:232) at io.micronaut.test.extensions.spock.MicronautSpockExtension.lambda$visitSpecAnnotation$0(MicronautSpockExtension.java:85) at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:101) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at org.spockframework.runtime.model.MethodInfo.invoke(MethodInfo.java:156) ``` * Extract method to reduce nesting --------- Co-authored-by: Tim Yates --- .../test/support/sql/TestSqlAnnotationHandler.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test-core/src/main/java/io/micronaut/test/support/sql/TestSqlAnnotationHandler.java b/test-core/src/main/java/io/micronaut/test/support/sql/TestSqlAnnotationHandler.java index eadf5c818..9f996b8ef 100644 --- a/test-core/src/main/java/io/micronaut/test/support/sql/TestSqlAnnotationHandler.java +++ b/test-core/src/main/java/io/micronaut/test/support/sql/TestSqlAnnotationHandler.java @@ -31,7 +31,6 @@ import java.io.InputStream; import java.net.URL; import java.nio.charset.StandardCharsets; -import java.sql.SQLException; import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -59,10 +58,19 @@ private TestSqlAnnotationHandler() { * @param applicationContext The application context * @param phase The {@link Sql.Phase} to run the scripts in * - * @throws SQLException If an error occurs executing the SQL * @throws IOException If an error occurs reading the SQL */ public static void handle(BeanDefinition specDefinition, ApplicationContext applicationContext, Sql.Phase phase) throws IOException { + if (applicationContext.containsBean(ResourceLoader.class)) { + doHandle(specDefinition, applicationContext, phase); + } else { + if (LOG.isTraceEnabled()) { + LOG.trace("No bean of type ResourceLoader exists. You cannot use the @Sql annotation"); + } + } + } + + private static void doHandle(BeanDefinition specDefinition, ApplicationContext applicationContext, Sql.Phase phase) throws IOException { ResourceLoader resourceLoader = applicationContext.getBean(ResourceLoader.class); Optional>> sqlAnnotations = specDefinition .findAnnotation(Sql.Sqls.class)