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

We have a great application written in Spring 3. I need to write JUnit validation behavior to validate some services. This is not a unit, but part of a system. There are several services and repositories that work together - these are a lot of injected beans inside. The application also uses aspects.

My question is. How to manage configuration and beans in this test case? I need to use beans defined in application configurations, and in tests redefine beans only using persistence to work with built-in db. Therefore, I need to use beans from src, because they are defined and override only some of the causing problems (persistance beans, beans using webservices, ...) In the test package, I defined the beans configuration class for persistance using datasource for hsql. But I do not know what next. I tried to annotate a test configuration class:

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

to scan the entire application and use the beans configuration from the src folder. But it also accepts configurations from other tests causing problems. Is this a good strategy or not? What now is to use excludeFilters to remove other test configurations? Or is this strategy generally bad?

thank

+5
source share
3 answers

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

+2
source

You can selectively overwrite beans with the context merge function provided by the annotation @ContextHierarchy.

, , Spring beans:

@Configuration
@ComponentScan({"com.example.our.app"})
public class MyTestConfig implements TransactionManagementConfigurer {

, - !:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy( {
    @ContextConfiguration(name="testContext", classes = MyTestConfig.class),
})
public class BaseTest {

, , unit test, , beans :

    @ContextHierarchy(@ContextConfiguration(name="testContext", classes = OneUnitTest.Config.class))
    public class OneUnitTest extends AggroBaseTest {
      @Configuration
      static class Config {      
            ..
      }
+2

You can also override another import

<beans>
    <import resource="classpath*:applocationContext.xml" />
    <bean id="dataSourceFactory" class=com.demo.MyNewClass/>
</beans>

And in your class, if you

this.applicationContext.getBean("dataSourceFactory");

extract the class, you will see an instance of the new class

Further

<bean id="dataSourceFactory" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

This way you can override the default behavior

0
source

All Articles