title: Spring Boot multiple profiles DataSource
body: <p>I am writing a Spring Boot application with two Oracle datasources - Here is my application.yml file:</p>
  
  <pre><code>spring:
      profiles: test
  datasource:
    app2:
         url: jdbc:oracle:thin:@//rap-vin:1521/APP221
         username: user2
         password: pwd2
         driver-class-name: oracle.jdbc.OracleDriver
    app:
         url: jdbc:oracle:thin:@//eap10-san:1522/APP22
         username: user
         password: pwd
         driver-class-name: oracle.jdbc.OracleDriver
  application:
    app:
         url: example.com/app
  </code></pre>
  
  <p>I've a DBConfiguration class that looks like:</p>
  
  <pre><code>@Configuration
  public class DBConfiguration {
  
      @Bean
      @Primary
      @ConfigurationProperties(prefix = "datasource.app")
      public DataSource appDataSource() {
          DataSource ds = DataSourceBuilder.create().build();
          return ds;
      }
  
      @Bean
      @ConfigurationProperties(prefix = "datasource.app2")
      public DataSource app2DataSource() {
          DataSource ds = DataSourceBuilder.create().build();
          return ds;
      }
  }
  </code></pre>
  
  <p>And then I've a test class:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = DBConfiguration.class)
  public class Test {
  
      private JdbcTemplate jdbcTemplateAPP;
      private JdbcTemplate jdbcTemplateAPP2;
      //private RestTemplate restTemplate;
  
  
      @Autowired
      public void setDataSource(final DataSource appDataSource,
                  @Qualifier("app2DataSource") final DataSource app2DataSource) {
              this.jdbcTemplateAPP = new JdbcTemplate(appDataSource);
              this.jdbcTemplateAPP2 = new JdbcTemplate(app2DataSource);
          }
  
      @Test
      public void testJob() throws Exception {
          int count= jdbcTemplateAPP.queryForObject("select count(*) from parm", int.class);
  
          Assert.assertTrue(count==1);
      }
  }
  </code></pre>
  
  <p>But whenever I run the test I get the db urls as null.
  What am I doing wrong? Also how should I read in the application.app url?Appreciate your help with this - (fyi I'm a Spring newbie)
  The error:</p>
  
  <pre><code>java.sql.SQLException: The url cannot be null
      at java.sql.DriverManager.getConnection(DriverManager.java:649) ~[na:1.8.0_77]
      at java.sql.DriverManager.getConnection(DriverManager.java:208) ~[na:1.8.0_77]
      at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:305) ~[tomcat-jdbc-8.0.33.jar:na]
      at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:200) ~[tomcat-jdbc-8.0.33.jar:na]
      at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:708) [tomcat-jdbc-8.0.33.jar:na]
      at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:642) [tomcat-jdbc-8.0.33.jar:na]
      at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:464) [tomcat-jdbc-8.0.33.jar:na]
      at org.apache.tomcat.jdbc.pool.ConnectionPool.&lt;init&gt;(ConnectionPool.java:141) [tomcat-jdbc-8.0.33.jar:na]
      at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:115) [tomcat-jdbc-8.0.33.jar:na]
      at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:102) [tomcat-jdbc-8.0.33.jar:na]
      at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:126) [tomcat-jdbc-8.0.33.jar:na]
      at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:390) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:470) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:480) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:490) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:496) [spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at com.tgt.trans.intl.batchtest.mib.tests.MIB022Test.testJob(MIB022Test.java:35) [test-classes/:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_77]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_77]
      at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_77]
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) [junit-4.12.jar:4.12]
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) [junit-4.12.jar:4.12]
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit-4.12.jar:4.12]
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) [junit-4.12.jar:4.12]
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193) [spring-test-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:na]
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na]
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:670) [.cp/:na]
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na]
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na]
  </code></pre>