Se esperaba una combinación de bean pero se encontraron 3: objectMapper, halObjectMapper, _halObjectMapper en junitTest

Tengo un problema con mi testing. Estoy usando java spring e bash ejecutar la testing junit para verificar si mi server está activo.

Esta es la testing que estoy intentando:

@RunWith(SpringJUnit4ClassRunner::class) @ContextConfiguration(classes = arrayOf(ServiceContext::class,DatabaseContext::class)) @Transactional open class newtest : AbstractTestController(){ @Test fun echoTest() { mockMvc.perform(get("/echo").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk) } } 

Esta es la class AbstractTestController que mi nueva class de testing está extendiendo :

 @WebAppConfiguration abstract class AbstractTestController : DatabaseInitializedTest() { @Autowinetworking lateinit var webAppContext: WebApplicationContext lateinit var mockMvc: MockMvc @Autowinetworking lateinit var authHelper: OAuthHelper @Autowinetworking private lateinit var userService: UserService fun adminToken(username: String) = authHelper.addBearerToken(userService.getUserByUsername(username), *AuthoritiesService.adminAuthorities.toTypedArray()) fun clientItToken(username: String) = authHelper.addBearerToken(userService.getUserByUsername(username), *AuthoritiesService.clientItAuthorities.toTypedArray()) fun clientManagerToken(username: String) = authHelper.addBearerToken(userService.getUserByUsername(username), *AuthoritiesService.clientManagerAuthorities.toTypedArray()) fun endUserToken(username: String) = authHelper.addBearerToken(userService.getUserByUsername(username), *AuthoritiesService.endUserAuthorities.toTypedArray()) @Autowinetworking lateinit var mapper: ObjectMapper fun <T> ResultActions.and200ReturnClass(clazz: TypeReference<T>): ResultActions { if (this.andReturn().response.status == HttpServletResponse.SC_OK && this.andReturn().response.contentAsString.isNotEmpty()) { mapper.readValue<T>(this.andReturn().response.contentAsString, clazz)!! } return this } @Before fun initialize() { mockMvc = MockMvcBuilders .webAppContextSetup(webAppContext) .apply<DefaultMockMvcBuilder>(SecurityMockMvcConfigurers.springSecurity()) .alwaysDo<DefaultMockMvcBuilder>(MockMvcResultHandlers.print()) .build() } } 

Mi configuration :

  @Configuration @Import(value = *arrayOf( OAuth2ServerConfiguration::class, OAuth2ServerConfiguration.AuthorizationServerConfiguration::class, OAuth2ServerConfiguration.ResourceServerConfiguration::class, SecurityConfig::class, OAuthHelper::class, WebConfig::class, DatabaseInitializator::class, LowerCaser::class)) @ComponentScan("com.hyg","com.hyg.service", "com.hyg.web", "com.hyg.utils") @EnableAutoConfiguration @EnableAspectJAutoProxy(proxyTargetClass = true) open class ServiceContext @Configuration @EnableJpaRepositories("com.hyg") @EnableTransactionManagement @EnableAutoConfiguration @PropertySource("classpath:application.properties") open class DatabaseContext { val DATA_PACKAGE = "com.hyg.data" val INTERCEPTOR_KEY = "hibernate.ejb.interceptor" @Value("\${spring.datasource.username:${Const.NONE}}") lateinit var username: String @Value("\${spring.datasource.url}") lateinit var url: String @Value("\${spring.datasource.driverClassName}") lateinit var driverClass: String @Value("\${spring.datasource.password}") lateinit var password: String @Bean @Primary open fun objectMapper(): ObjectMapper { val mapper = ObjectMapper() mapper.registerModule(KotlinModule()) mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true) mapper.configure(MapperFeature.USE_STD_BEAN_NAMING, true) mapper.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false) mapper.registerModule(JavaTimeModule()) mapper.registerModule(localDateTimeSerializer()) return mapper } @Bean open fun localDateTimeSerializer(): SimpleModule { val localDateTimeSerializer = SimpleModule() val serializer = LocalDateTimeSerializer( DateTimeFormatter.ofPattern(Const.DATE_TIME_PATTERN)) val deserializer = LocalDateTimeDeserializer( DateTimeFormatter.ofPattern(Const.DATE_TIME_PATTERN)) localDateTimeSerializer.addSerializer(LocalDateTime::class.java, serializer) localDateTimeSerializer.addDeserializer(LocalDateTime::class.java, deserializer) return localDateTimeSerializer } @Bean open fun dataSource(): DataSource { val ds = DataSourceBuilder.create().driverClassName(driverClass).username(username).password(password).url(url).build() val proxy = ProxyDataSource() proxy.setDataSource(ds) proxy.setListener(DataSourceQueryCountListener()) return proxy } @Bean open fun entityManagerFactory( factory: EntityManagerFactoryBuilder, dataSource: DataSource, properties: JpaProperties): LocalContainerEntityManagerFactoryBean { val jpaProperties = HashMap<String, Any>() jpaProperties.putAll(properties.getHibernateProperties(dataSource)) jpaProperties.put(INTERCEPTOR_KEY, hibernateInterceptor()) return factory.dataSource(dataSource).packages(DATA_PACKAGE) .properties(jpaProperties as MutableMap<String, *>) .build() } @Bean open fun hibernateInterceptor(): HibernateStatisticsInterceptor { return HibernateStatisticsInterceptor() } @Bean open fun requestStatisticsInterceptor(): RequestStatisticsInterceptor { return RequestStatisticsInterceptor() } } 

Este es el error completo que obtengo:

  2017-11-17 20:03:52.477 WARN --- [ main] oswcsGenericWebApplicationContext : Exception encountenetworking during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration$$EnhancerBySpringCGLIB$$ecfe2bfa]: Constructor threw exception; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available: expected single matching bean but found 3: objectMapper,halObjectMapper,_halObjectMapper 2017-11-17 20:03:52.477 INFO --- [ main] osjeaAnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown 2017-11-17 20:03:52.499 INFO --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2017-11-17 20:03:52.508 ERROR --- [ main] ostest.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@6b19b79] to prepare test instance [com.hyginex.Integration.newtest@7a08da83] java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189) at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration$$EnhancerBySpringCGLIB$$ecfe2bfa]: Constructor threw exception; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available: expected single matching bean but found 3: objectMapper,halObjectMapper,_halObjectMapper at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1154) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1056) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:134) at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:61) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251) at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ... 24 common frames omitted Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration$$EnhancerBySpringCGLIB$$ecfe2bfa]: Constructor threw exception; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available: expected single matching bean but found 3: objectMapper,halObjectMapper,_halObjectMapper at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:271) ... 41 common frames omitted Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available: expected single matching bean but found 3: objectMapper,halObjectMapper,_halObjectMapper at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:172) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1114) at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectProvider.getIfAvailable(DefaultListableBeanFactory.java:1643) at org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration.<init>(EndpointMBeanExportAutoConfiguration.java:63) at org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration$$EnhancerBySpringCGLIB$$ecfe2bfa.<init>(<generated>) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ... 43 common frames omitted 

Espero que ayude a resolver mi problema. Intenté poner @Primary en el Bean que hace el ObjectMapper o agregar esas annotations @ Order @ ConditionalOnMissingBean pero no funcionó … Gracias

Tiene varios beans con el tipo ObjectMapper en el context de Spring. Intenta agregar la anotación @Qualifier("objectMapper") para mapper (en AbstractTestController ) o objectMapper a objectMapper :

 @WebAppConfiguration abstract class AbstractTestController : DatabaseInitializedTest() { ... @Qualifier("objectMapper") @Autowinetworking lateinit var mapper: ObjectMapper ... } 

o

 @WebAppConfiguration abstract class AbstractTestController : DatabaseInitializedTest() { ... @Autowinetworking lateinit var objectMapper: ObjectMapper ... }