Spring override primary bean with non-primary bean

I am trying to override the Spring bean during a test declared in a test configuration using @Primary. One declaration is in the src / main / java path, another, primary, is in the src / test / java path.

However, Spring intentionally replaces the primary bean with a non-primary bean, which I do not want to use for the test. If I just comment out the production configuration of the (src / main / java) bean, it uses the primary test (src / main / test) bean in the test configuration as desired. (Clearly, I cannot comment on the code every time I want to run a test.)

From the magazines:

osbfsDefaultListableBeanFactory - Override the bean definition for the bean 'sqsConnectionFactory' with another definition: replace [Root bean: class [null]; Volume =; abstract = false; lazyInit = false; autowireMode = 3; dependencyCheck = 0; autowireCandidate = TRUE; primary = true; factoryBeanName = testJmsConfiguration; factoryMethodName = sqsConnectionFactory; initMethodName = NULL; destroyMethodName = (output); defined in the class path resource [com / foo / configuration / TestJmsConfiguration.class]]

from

[Root bean: class [null]; Volume =; abstract = false; lazyInit = false; autowireMode = 3; dependencyCheck = 0; autowireCandidate = TRUE; primary = false ; factoryBeanName = jmsConfiguration; factoryMethodName = sqsConnectionFactory; initMethodName = NULL; destroyMethodName = (output); defined in the class path resource [com / foo / configuration / JmsConfiguration.class]]

Why is Spring replacing the primary bean with a non-primary bean, and how can I get Spring to use a bean specifically labeled as a primary bean?

Edit: src / main / java configuration:

@Configuration public class JmsConfiguration { ... other bean declarations here ... @Bean public SQSConnectionFactory sqsConnectionFactory(Region region) throws JMSException { return SQSConnectionFactory.builder() .withRegion(region) .build(); } } 

Test Configuration:

 @Configuration public class TestJmsConfiguration { @Bean(name="messageProducerMock") public MessageProducer mockMessageProducer() { return new MessageProducerMock(); } ... other bean declarations here ... @Bean @Primary public SQSConnectionFactory sqsConnectionFactory(@Qualifier("messageProducerMock") MessageProducer messageProducerMock) throws JMSException { ... returning setup mock here } } 

A class with tests is annotated with:

 @RunWith(SpringRunner.class) @SpringBootTest @ActiveProfiles(profiles = {"test"}) 
+7
java spring spring-boot spring-test dependency-injection
source share
2 answers

@Primary only works at the injection point when there is a conflict, because different beans correspond to the condition that needs to be entered, and a decision needs to be made.

@Primary not used when initializing beans. Since you are using two different methods that create the same bean, and you are not calling either of them, Spring believes that you are trying to override it, so this can happen. Given a name, this is the easiest solution, but remember that your context will still initialize a bean that you do not want to use.

+2
source share

I think the @ContextConfiguration may not be in the test class.

Example test configuration class (src / test / java / TestConfiguration.class):

 @Configuration @ComponentScan public class TestConfiguration { @Bean RabbitSender rabbitSender() { return mock(RabbitSender.class); } } 

Test class example:

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestConfiguration.class) public class SomeServiceTest { } 
0
source share

All Articles