Skip to content

Commit

Permalink
check if bean of type ResourceLoader exists (#906)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
sdelamo and timyates authored Dec 1, 2023
1 parent 3cd9e0b commit 0ad6b0e
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<List<AnnotationValue<Sql>>> sqlAnnotations = specDefinition
.findAnnotation(Sql.Sqls.class)
Expand Down

0 comments on commit 0ad6b0e

Please sign in to comment.