Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression in test-context caching [SPR-17049] #21587

Closed
spring-projects-issues opened this issue Jul 16, 2018 · 5 comments
Closed

Regression in test-context caching [SPR-17049] #21587

spring-projects-issues opened this issue Jul 16, 2018 · 5 comments
Assignees
Labels
in: test

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Jul 16, 2018

David J. M. Karlsen opened SPR-17049 and commented

Found in  5.1.0-SNAPSHOT

Error Message

Failed to load ApplicationContext

Stacktrace

java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'cachedConnectionFactory' defined in class path resource [com/edb/fs/tac/jfr/srv/ws/infra/mq/MqConfig.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=true; factoryBeanName=mqConfig; factoryMethodName=connectionFactory; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/edb/fs/tac/jfr/srv/ws/infra/mq/MqConfig.class]] for bean 'cachedConnectionFactory': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=camelCxfTransportTest.MockConfig; factoryMethodName=cachedConnectionFactory; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/edb/fs/tac/jfr/srv/ws/CamelCxfTransportTest$MockConfig.class]] bound.

 

The case:

The MqConfig has been loaded in a previous test (but the configuration class is not active for the test in which the exception is thrown.

In the test that throws the error I have an inner config class which has the connection factory as a mock:

@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD )
@ContextConfiguration( classes = {
 WsServiceConfig.class,
 ContextConfig.class,
 CamelCxfTransportConfig.class}
 )
@SpringBootTest(classes = \{ApplicationConfiguration.class, CamelCxfTransportTest.MockConfig.class })
public class CamelCxfTransportTest extends AbstractCamelTest {

 @Configuration
 static class MockConfig {
 @Bean
 public DestinationManager destinationManager() {
 return new DestinationManager();
 }

 @Bean
 public ConfigurationManager configurationManager() {
 return new ConfigurationManager();
 }

 @Bean
 public MockConnectionFactory cachedConnectionFactory( ConfigurationManager configurationManager,
 DestinationManager destinationManager ) {
 return new MockConnectionFactory(destinationManager,configurationManager);
 }

 @Bean
 public JmsTransactionManager jmsTransactionManager(ConnectionFactory connectionFactory) {
 return new JmsTransactionManager(connectionFactory);
 }

 @Bean
 public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
 return new JmsTemplate(connectionFactory);
 }

....rest of normal testcode...
 }

 production-config:

@Configuration
@ConfigurationProperties("mq")
public class MqConfig {

 //this one is a value since: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-vs-value
 @Value("#\{T(com.ibm.msg.client.wmq.WMQConstants).${mq.reconnectOptionsConstant}}")
 private int reconnectOptions;

// ...properties...setters..getters...

 @Bean("cachedConnectionFactory")
 @Primary
 public ConnectionFactory connectionFactory(UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter ) {
 CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
 cachingConnectionFactory.setReconnectOnException(reconnectOnException);
 cachingConnectionFactory.setSessionCacheSize(sessionCacheSize);
 cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter);

 return cachingConnectionFactory;
 }

 @Bean
 protected UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter( MQConnectionFactory mqConnectionFactory ) {
 UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
 userCredentialsConnectionFactoryAdapter.setUsername(username);
 userCredentialsConnectionFactoryAdapter.setPassword(password);
 userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqConnectionFactory);

 return userCredentialsConnectionFactoryAdapter;
 }

 @Bean
 protected MQConnectionFactory mqConnectionFactory()
 throws JMSException
 {
 MQConnectionFactory mqConnectionFactory = new MQConnectionFactory();
 mqConnectionFactory.setTransportType(com.ibm.msg.client.wmq.WMQConstants.WMQ_CM_CLIENT);
 mqConnectionFactory.setConnectionNameList(connectionNameList);
 mqConnectionFactory.setQueueManager(queueManager);
 mqConnectionFactory.setChannel(channel);
 mqConnectionFactory.setClientReconnectTimeout(reconnectTimeout);
 mqConnectionFactory.setClientReconnectOptions(reconnectOptions);

 return mqConnectionFactory;
 }

 @Bean
 public JmsTransactionManager jmsTransactionManager(ConnectionFactory connectionFactory) {
 JmsTransactionManager jmsTransactionManager = new JmsTransactionManager();
 jmsTransactionManager.setConnectionFactory(connectionFactory);

 return jmsTransactionManager;
 }

which leads me to think that something has changed wrt test-context caching, as a bean is available to the testclass when it should not even be visible.

If I run the test-class isolated (mvn.... test=thetestclass) it will pass fine, hence I think it is due to context caching.


Affects: 5.1 RC1

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 16, 2018

Sam Brannen commented

Can you please create a simple, scaled-down project that reproduces the error you've encountered and make that available to us (e.g., via the "issues" repo)?

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 16, 2018

Sam Brannen commented

Also, if you could clean up the formatting of the stacktrack and examples you've provided already, that might help to better analyze the issue.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 16, 2018

Sam Brannen commented

Tip: when you edit, just use the "Text" tab: that's likely easier than dealing with the nuances of the "Visual" tab. ;-)

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 18, 2018

David J. M. Karlsen commented

This problem magically disappeared in an updated spring-boot 2.1.x build

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 20, 2018

Sam Brannen commented

Glad to hear it "fixed itself".

I'll close this issue now.

Cheers

@spring-projects-issues spring-projects-issues added type: regression in: test labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: regression label Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test
Projects
None yet
Development

No branches or pull requests

2 participants