1

I am about to write unit testcase with junit5 for my spring integration project. But when i am running a sample testcase, i am getting an exception. the below given code is my sample testcase,

@SpringBootTest
class IntegrationUtilsTest {
    @Test
    @DisplayName("sample test")
    void sample() {
        Assert.assertTrue(true);
    }
}

and I am getting the following log while running this test,

2020-12-08 18:06:45.780 DEBUG   --- [           main] com.tngtech.archunit.ArchConfiguration   : No configuration found in classpath at archunit.properties => Using default configuration

        ██╗ ██╗   ██╗ ████████╗ ███████╗   ██████╗ ████████╗ ████████╗ ███████╗
        ██║ ██║   ██║ ╚══██╔══╝ ██╔═══██╗ ██╔════╝ ╚══██╔══╝ ██╔═════╝ ██╔═══██╗
        ██║ ████████║    ██║    ███████╔╝ ╚█████╗     ██║    ██████╗   ███████╔╝
  ██╗   ██║ ██╔═══██║    ██║    ██╔════╝   ╚═══██╗    ██║    ██╔═══╝   ██╔══██║
  ╚██████╔╝ ██║   ██║ ████████╗ ██║       ██████╔╝    ██║    ████████╗ ██║  ╚██╗
   ╚═════╝  ╚═╝   ╚═╝ ╚═══════╝ ╚═╝       ╚═════╝     ╚═╝    ╚═══════╝ ╚═╝   ╚═╝

:: JHipster   :: Running Spring Boot 2.2.7.RELEASE ::
:: https://www.jhipster.tech ::

2020-12-08 18:06:46.950  INFO 5036 --- [           main] c.m.a.i.service.IntegrationUtilsTest     : No active profile set, falling back to default profiles: default
2020-12-08 18:06:50.330 DEBUG 5036 --- [           main] i.m.c.u.i.logging.InternalLoggerFactory  : Using SLF4J as the default logging framework
2020-12-08 18:06:51.722  WARN 5036 --- [           main] .h.i.HttpRequestHandlingMessagingGateway : The 'requestPayloadType' attribute will have no relevance for one of the specified HTTP methods '[GET, HEAD, OPTIONS]'
2020-12-08 18:06:51.727  WARN 5036 --- [           main] .h.i.HttpRequestHandlingMessagingGateway : The 'requestPayloadType' attribute will have no relevance for one of the specified HTTP methods '[GET, HEAD, OPTIONS]'
2020-12-08 18:06:51.732  WARN 5036 --- [           main] .h.i.HttpRequestHandlingMessagingGateway : The 'requestPayloadType' attribute will have no relevance for one of the specified HTTP methods '[GET, HEAD, OPTIONS]'
2020-12-08 18:06:53.314  WARN 5036 --- [           main] ockingLoadBalancerClientRibbonWarnLogger : You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon is in maintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2020-12-08 18:06:53.685  INFO 5036 --- [           main] c.m.a.i.service.IntegrationUtilsTest     : Started IntegrationUtilsTest in 7.545 seconds (JVM running for 8.789)
2020-12-08 18:06:53.710 DEBUG 5036 --- [           main] reactor.util.Loggers$LoggerFactory       : Using Slf4j logging framework
2020-12-08 18:06:53.712 DEBUG 5036 --- [           main] reactor.core.publisher.Hooks             : Hooking onLastOperator: org.springframework.security.core.context.SecurityContext
2020-12-08 18:06:53.758 DEBUG 5036 --- [           main] reactor.core.publisher.Hooks             : Reset onLastOperator: org.springframework.security.core.context.SecurityContext
2020-12-08 18:06:54.118  WARN 5036 --- [extShutdownHook] i.m.c.i.d.DropwizardMeterRegistry        : Failed to apply the value function for the gauge 'spring.integration.channels'. Note that subsequent logs will be logged at debug level.

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'nullChannel': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:212)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:623)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:611)
    at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1242)
    at org.springframework.integration.config.IntegrationManagementConfigurer.lambda$registerComponentGauges$1(IntegrationManagementConfigurer.java:444)
    at io.micrometer.core.instrument.dropwizard.DropwizardMeterRegistry.lambda$newGauge$0(DropwizardMeterRegistry.java:87)
    at com.codahale.metrics.Slf4jReporter.logGauge(Slf4jReporter.java:342)
    at com.codahale.metrics.Slf4jReporter.report(Slf4jReporter.java:258)
    at com.codahale.metrics.ScheduledReporter.report(ScheduledReporter.java:253)
    at com.codahale.metrics.ScheduledReporter.stop(ScheduledReporter.java:200)
    at com.codahale.metrics.ScheduledReporter.close(ScheduledReporter.java:245)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.java:339)
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:273)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:579)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:551)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:1091)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:512)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:1084)
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1060)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1029)
    at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:948)

Can anyone help me understand this. why this BeanCreationNotAllowedException ?

UPDATE 1

I added the class and created the spring.factories file. But I am not able to start the application. I am getting the following error.

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method metricsWebMvcConfigurer in org.springframework.boot.actuate.autoconfigure.metrics.web.servlet.WebMvcMetricsAutoConfiguration required a single bean, but 2 were found:
    - prometheusMeterRegistry: defined by method 'prometheusMeterRegistry' in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.class]
    - consoleLoggingRegistry: defined by method 'consoleLoggingRegistry' in class path resource [io/github/jhipster/config/metric/JHipsterLoggingMetricsExportConfiguration.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed.

Which bean i should use and how to make it qualified for autowiring?

Sanal M
  • 187
  • 4
  • 17

1 Answers1

0

The fix for this problem is going to be available in the upcoming (December 10th) Spring Boot 2.2.12: https://github.com/spring-projects/spring-boot/pull/24095. That Jhipster has to be upgraded respectively.

If you are not interested in metrics, you may have your own NoopMeterRegistry bean for that test configuration.

As a workaround trick you can use the code from the relevant PR:

@AutoConfigureAfter({ MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class })
@AutoConfigureBefore(IntegrationAutoConfiguration.class)
@Configuration(proxyBeanMethods = false)
public class IntegrationMetricsAutoConfiguration {

}

and a spring.factories entry for this class against org.springframework.boot.autoconfigure.EnableAutoConfiguration key.

UPDATE

According the error you show about two MeterRegistry beans, it looks like we need to add similar workaround for that JHipsterLoggingMetricsExportConfiguration since it is auto-configured unconditionally and out of order:

@AutoConfigureAfter(JHipsterLoggingMetricsExportConfiguration.class)
@AutoConfigureBefore(CompositeMeterRegistryAutoConfiguration.class)
@Configuration
public class JHipsterLoggingMetricsFixerAutoConfiguration {

}

And add this class as an entry to the same mentioned spring.factories file.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Hi Bilan, I have updated the error now i am getting above. Can you please help? – Sanal M Dec 29 '20 at 08:09
  • As I said: you don't need to add a custom code but just upgrade to that latest Spring Boot `2.2.12`: https://github.com/spring-projects/spring-boot/releases/tag/v2.2.12.RELEASE. You have an error now related to JHipster. Probably that `JHipsterLoggingMetricsExportConfiguration` must have something like `@AutoConfigureBefore(CompositeMeterRegistryAutoConfiguration.class)`. Different story though... – Artem Bilan Dec 29 '20 at 19:02
  • I'm working in a project, along with many others. I don't think its possible to directly change the spring boot version. hence I am more concentrated on the work around. So can you please give a clarity on it? – Sanal M Dec 30 '20 at 03:31
  • Please, find an UPDATE in my answer. – Artem Bilan Dec 30 '20 at 15:19