Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit test in Spring - overriding and ignoring beans from application other configuration classes

We have large application written in Spring 3. I need to write JUnit test checking behavior of some service. It is not a unit but part of a system. There are some services and repositories working together insite it -> lot of injected beans inside. The app also uses aspects.

My question is. How to manage config and beans in this case of tests? I need to use beans defined in app configes and in tests only redefine beans using persistence to work with a embedded db. So I need to use beans from src as they are defined and override only some causing troubles (persistance beans, beans using webservices,...) In test package I made Config class definying beans for persistance, using datasource for hsql. But I don`t know what next. I tried to annotate Test config class with:

@Configuration
@EnableAspectJAutoProxy
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ, proxyTargetClass = true)
@ComponentScan(basePackages = "com.example.our.app")
public class MyTestConfig implements TransactionManagementConfigurer {

to scan whole application and use configuration of beans from src folder. But this also takes configs from other tests causing problems. Is this whole good strategy or not? What now - use excludeFilters to remove other test configs? Or is this strategy whole bad?

thanks

like image 492
mefi Avatar asked Oct 04 '22 15:10

mefi


1 Answers

I think best way here to use is Spring profiles. Check here now to use H2 for tests with profiles.

like image 127
Michail Nikolaev Avatar answered Oct 13 '22 10:10

Michail Nikolaev