title: Configure integration test or use Spring JDBC in Unit Test using Spring Boot?
body: <p>Am using the latest versions of Spring Boot, Spring JDBC, and Spring Rest...</p>
  
  <p>My project is setup as a typical Maven project containing the following filesystem structure:</p>
  
  <pre><code>myproject
  |
  --src/main/java/com/myapp
  --src/main/resource/application.properties
  |
  --src/test/java/com/myapp
  --src/test/resources/application.properties
  |
  pom.xml
  </code></pre>
  
  <p>My application.properties are as follows (connecting to a local MySQL 5 database):</p>
  
  <pre><code>spring.datasource.url=jdbc:mysql://localhost:3306/testdb
  spring.datasource.username=root
  spring.datasource.password=
  spring.datasource.name=testdb
  spring.datasource.initialize=true
  spring.datasource.driverClassName=com.mysql.jdbc.Driver      
  </code></pre>
  
  <p>MyDAO:</p>
  
  <pre><code>public interface MyDao {
  
      public List&lt;App&gt; findAllApps(); 
  }
  </code></pre>
  
  <p>MyDaoImpl:</p>
  
  <pre><code>@Repository("myDao")
  public class MyDaoImpl implements MyDao {
  
      @Autowired 
      JdbcTemplate jdbcTemplate;
  
      public List&lt;App&gt; findAllApps() {
          List&lt;App&gt; apps = this.jdbcTemplate.query(
                  "select app_name from app",
                  new RowMapper&lt;App&gt;() {
                      public App mapRow(ResultSet rs, int rowNum) throws SQLException {
                          App app = new App();
                          app.setAppName(rs.getString("app_name"));
                          return app;
                      }
                  });
  
          return apps;
      }
  }
  </code></pre>
  
  <p>Its called in MyService class using Dependency Injection:</p>
  
  <pre><code>@RestController
  public class MyService {
  
      @Autowired
      @Qualifier("myDao")
      MyDao myDao;
  
      @RequestMapping(value = "/apps", method = RequestMethod.GET, consumes = "text/plain", produces = "application/json")
      public void process() throws JsonParseException, IOException {
          List&lt;App&gt; apps = myDao.findAllApps();
          System.out.println(apps.toString());
      }
  }
  </code></pre>
  
  <p>This totally works as stated in my RestController...</p>
  
  <p>But however, in a typical JUnit test:</p>
  
  <pre><code>public class MyServiceTest {
  
      @Autowired
      @Qualifier("myDao")
      MyDao myDao;
  
      @Test
      public void process() {
          List&lt;App&gt; apps = myDao.findAllApps();
      }
  }
  </code></pre>
  
  <p>The call to myDao.findAllApps() returns a NullPointerException...</p>
  
  <p>I even tried running my app (using the embedded tomcat) first by issuing the following from the command line:</p>
  
  <pre><code>mvn spring-boot:run 
  </code></pre>
  
  <p>However a non-database specific JUnit test works inside Eclipse or when I do: </p>
  
  <pre><code>mvn clean install
  </code></pre>
  
  <p>Question(s):</p>
  
  <ol>
  <li><p>How can I set it up so I can run an integration test and it actually hits my database (or a mock db for that matter) from MyServiceTest?</p></li>
  <li><p>Why is the dependency injection failing when trying to inject in MyServiceTest for Spring JDBC?</p></li>
  <li><p>Is there a way to setup my unit tests to test Rest calls?</p></li>
  </ol>
  
  <p>Many thanks for everyone who took the time to read this and many many thanks for the people that respond!</p>
  
  <p>Here's my pom.xml (per Eddu's request):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;myproject&lt;/artifactId&gt;
      &lt;version&gt;0.1.0&lt;/version&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.2.2.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.7&lt;/java.version&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
              &lt;artifactId&gt;spring-data-rest-webmvc&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
              &lt;artifactId&gt;commons-collections4&lt;/artifactId&gt;
              &lt;version&gt;4.0&lt;/version&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
      &lt;repositories&gt;
          &lt;repository&gt;
              &lt;id&gt;spring-releases&lt;/id&gt;
              &lt;name&gt;Spring Releases&lt;/name&gt;
              &lt;url&gt;https://repo.spring.io/libs-release&lt;/url&gt;
          &lt;/repository&gt;
          &lt;repository&gt;
              &lt;id&gt;org.jboss.repository.releases&lt;/id&gt;
              &lt;name&gt;JBoss Maven Release Repository&lt;/name&gt;
              &lt;url&gt;https://repository.jboss.org/nexus/content/repositories/releases&lt;/url&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
  
      &lt;pluginRepositories&gt;
          &lt;pluginRepository&gt;
              &lt;id&gt;spring-releases&lt;/id&gt;
              &lt;name&gt;Spring Releases&lt;/name&gt;
              &lt;url&gt;https://repo.spring.io/libs-release&lt;/url&gt;
          &lt;/pluginRepository&gt;
      &lt;/pluginRepositories&gt;
  
  &lt;/project&gt;
  </code></pre>