Issue
I'm using Mockito to test a replication library, in tests I'm mocking a DAO. However, the library is constructed in a way that it scans all available DAOs, as a result of that I get multiple beans (mocked one and not mocked one).
I am creating a spy of bean like that:
@Bean
@Primary
@Autowired
public DAOBean daoBean (DAOBean dao){
return Mockito.spy(dao);
}
I wonder if it is possible to somehow put my mocked bean instead of existing one and not just create a new one
pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
<scope>test</scope>
</dependency>
Solution
If you want to make sure that the original bean is not created during tests you can
- Run tests with a Spring profile for example
test
using@ActiveProfiles("test")
- Annotate original bean with
@Profile("!test")
Answered By - Maciej Walkowiak
Answer Checked By - Marie Seifert (JavaFixing Admin)