Explanation about Spring overriding beans when running JUnit tests
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 29 Jul 2016 18:09:44 +0000 (20:09 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 29 Jul 2016 18:10:51 +0000 (20:10 +0200)
SpringJava/JPA/spring-jpa-persistence/src/test/resources/datasource-test-configuration.xml

index 8ad0406..68cd61f 100644 (file)
     <jdbc:embedded-database id="dataSource" type="H2" />\r
        -->\r
        \r
+       <!-- \r
+               1. There are two beans with the same id "dataSource"\r
+                  One declared in datasource-configuration.xml\r
+                  Another one declared in datasource-test-configuration.xml\r
+               2. Both beans are declared in different XML files.\r
+               3. Because there are in different XML files Spring does not complain about having duplicate beans.\r
+               4. Because files in src/test will be loaded in class path after files in src/main this bean will\r
+                  override the one declared in datasource-configuration.xml when running JUnit Tests :)\r
+        -->\r
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">\r
                <property name="driverClassName" value="org.h2.Driver" />\r
                <property name="url" value="jdbc:h2:mem:mybatis_example;INIT=create schema if not exists mybatis_example\;SET SCHEMA mybatis_example;MODE=DB2;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" />\r