title: How to test Spring CrudRepository using @DataJpaTest in Spring boot 2.1.0.M4 using Junit 5
body: <p>I cannot test my spring crud repository using spring boot 2.1.0.M4 with Junit5 and <code>@DataJpaTest</code>.
  I am using the following spring crud repository interface.</p>
  
  <pre><code>@Repository
  public interface DestinationRepository extends CrudRepository&lt;Destination, String&gt; {
  
      Optional&lt;Destination&gt; findCityCode(String code, String cityIsolci);
  
  }
  </code></pre>
  
  <p>And this is my unit test class</p>
  
  <pre><code>package com.test.repository;
  
  import static org.assertj.core.api.Assertions.assertThat;
  import static org.junit.jupiter.api.Assertions.assertTrue;
  
  import com.test.Destination;
  
  import java.util.Optional;
  
  import org.junit.Assert;
  import org.junit.jupiter.api.Test;
  import org.junit.jupiter.api.extension.ExtendWith;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
  import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
  import org.springframework.test.context.junit.jupiter.SpringExtension;
  import org.springframework.test.context.junit4.SpringRunner;
  
  @RunWith(SpringRunner.class)
  @DataJpaTest
  // @ExtendWith(SpringExtension.class)
  public class DestinationRepositoryTest {
  
      @Autowired
      private TestEntityManager entityManager;
  
      @Autowired
      private DestinationRepository destinationRepository;
  
      @Test
      public void whenfindByCodeAndCityIsolciThenReturnDestination() throws Exception {
          this.entityManager.persist(new Destination("DXB", "Y", "Dubai gate comment"));
  
          Optional&lt;Destination&gt; found = destinationRepository.findByCodeAndCityIsolci("DXB", "Y");
  
          Assert.assertTrue(found.isPresent());
  
          // assertThat(user.getVin()).isEqualTo("1234");
      }
  
      // @Test
      // public void whenfindByCodeAndCityIsolciThenReturnDestination() {
      // // given
      // Destination dubai = destinationRepository.save(new Destination("DXB", "Y", "Dubai gate comment"));
      //
      // // when
      // Optional&lt;Destination&gt; found = destinationRepository.findByCodeAndCityIsolci("DXB", "Y");
      //
      // // then
      // Assert.assertTrue(found.isPresent());
      // Assert.assertEquals(found.get().getGateComment(), dubai.getGateComment());
      // }
  
      // @Test
      // public void shouldFindAllDestinations() {
      // Destination dubai = destinationRepository.save(new Destination("DXB", "Y", "Dubai gate comment"));
      // Destination syd = destinationRepository.save(new Destination("SYD", "Y", "SYD gate comment"));
      //
      // Iterable&lt;Destination&gt; destinations = destinationRepository.findAll();
      //
      // assertThat(destinations).hasSize(2).contains(dubai, syd);
      // }
  
  }
  </code></pre>
  
  <p>This is my POM.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;***.***.***&lt;/groupId&gt;
          &lt;artifactId&gt;******&lt;/artifactId&gt;
          &lt;version&gt;1.0.0&lt;/version&gt;
          &lt;relativePath /&gt;
          &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;artifactId&gt;*****&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
      &lt;name&gt;${project.artifactId}&lt;/name&gt;
      &lt;description&gt;TEST&lt;/description&gt;
  
      &lt;prerequisites&gt;
          &lt;maven&gt;${maven.version}&lt;/maven&gt;
      &lt;/prerequisites&gt;
  
      &lt;properties&gt;
          &lt;start-class&gt;c*.*.Application&lt;/start-class&gt;
          &lt;junit.jupiter.version&gt;5.2.0&lt;/junit.jupiter.version&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-webflux&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-starter-config&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-rsa&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;!-- Sql server driver --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.microsoft.sqlserver&lt;/groupId&gt;
              &lt;artifactId&gt;mssql-jdbc&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Lombok dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Commons dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
              &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;commons-validator&lt;/groupId&gt;
              &lt;artifactId&gt;commons-validator&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Logging dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-starter-sleuth&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;net.logstash.logback&lt;/groupId&gt;
              &lt;artifactId&gt;logstash-logback-encoder&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Swagger dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;io.springfox&lt;/groupId&gt;
              &lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;io.springfox&lt;/groupId&gt;
              &lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;io.springfox&lt;/groupId&gt;
              &lt;artifactId&gt;springfox-bean-validators&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Test dependencies --&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;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;com.vaadin.external.google&lt;/groupId&gt;
                      &lt;artifactId&gt;android-json&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Spring REST Docs dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.restdocs&lt;/groupId&gt;
              &lt;artifactId&gt;spring-restdocs-core&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.restdocs&lt;/groupId&gt;
              &lt;artifactId&gt;spring-restdocs-webtestclient&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.restdocs&lt;/groupId&gt;
              &lt;artifactId&gt;spring-restdocs-asciidoctor&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Spring Auto REST Docs dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;capital.scalable&lt;/groupId&gt;
              &lt;artifactId&gt;spring-auto-restdocs-core&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hsqldb&lt;/groupId&gt;
              &lt;artifactId&gt;hsqldb&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- JUnit Jupiter API and Engine --&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
              &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
              &lt;version&gt;${junit.jupiter.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
              &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
              &lt;version&gt;${junit.jupiter.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/mockwebserver --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.squareup.okhttp3&lt;/groupId&gt;
              &lt;artifactId&gt;mockwebserver&lt;/artifactId&gt;
              &lt;version&gt;3.10.0&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
              &lt;artifactId&gt;gson&lt;/artifactId&gt;
              &lt;version&gt;2.8.5&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- https://mvnrepository.com/artifact/io.projectreactor/reactor-test --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;io.projectreactor&lt;/groupId&gt;
              &lt;artifactId&gt;reactor-test&lt;/artifactId&gt;
              &lt;version&gt;3.1.8.RELEASE&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.mock-server&lt;/groupId&gt;
              &lt;artifactId&gt;mockserver-netty&lt;/artifactId&gt;
              &lt;version&gt;5.4.1&lt;/version&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;resources&gt;
              &lt;resource&gt;
                  &lt;filtering&gt;true&lt;/filtering&gt;
                  &lt;directory&gt;src/main/resources&lt;/directory&gt;
                  &lt;includes&gt;
                      &lt;include&gt;**/*.xml&lt;/include&gt;
                      &lt;include&gt;**/*.properties&lt;/include&gt;
                      &lt;include&gt;**/*.yml&lt;/include&gt;
                  &lt;/includes&gt;
                  &lt;excludes&gt;
                      &lt;exclude&gt;**/*.jks&lt;/exclude&gt;
                  &lt;/excludes&gt;
              &lt;/resource&gt;
              &lt;resource&gt;
                  &lt;filtering&gt;false&lt;/filtering&gt;
                  &lt;directory&gt;src/main/resources&lt;/directory&gt;
                  &lt;includes&gt;
                      &lt;include&gt;**/*.jks&lt;/include&gt;
                  &lt;/includes&gt;
                  &lt;excludes&gt;
                      &lt;exclude&gt;**/*.xml&lt;/exclude&gt;
                      &lt;exclude&gt;**/*.properties&lt;/exclude&gt;
                      &lt;exclude&gt;**/*.yml&lt;/exclude&gt;
                  &lt;/excludes&gt;
              &lt;/resource&gt;
          &lt;/resources&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;com.github.ekryd.sortpom&lt;/groupId&gt;
                  &lt;artifactId&gt;sortpom-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;com.github.spotbugs&lt;/groupId&gt;
                  &lt;artifactId&gt;spotbugs-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-checkstyle-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-failsafe-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-javadoc-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-pmd-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;excludeFromFailureFile&gt;${project.basedir}/exclude-pmd.properties&lt;/excludeFromFailureFile&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-site-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-source-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.asciidoctor&lt;/groupId&gt;
                  &lt;artifactId&gt;asciidoctor-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.eclipse.m2e&lt;/groupId&gt;
                  &lt;artifactId&gt;lifecycle-mapping&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.gaul&lt;/groupId&gt;
                  &lt;artifactId&gt;modernizer-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.jacoco&lt;/groupId&gt;
                  &lt;artifactId&gt;jacoco-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&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;plugin&gt;
                  &lt;groupId&gt;pl.project13.maven&lt;/groupId&gt;
                  &lt;artifactId&gt;git-commit-id-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
      &lt;profiles&gt;
          &lt;profile&gt;
              &lt;id&gt;dev&lt;/id&gt;
              &lt;activation&gt;
                  &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;
              &lt;/activation&gt;
              &lt;properties&gt;
                  &lt;spring.profiles.active&gt;dev&lt;/spring.profiles.active&gt;
              &lt;/properties&gt;
          &lt;/profile&gt;
          &lt;profile&gt;
              &lt;id&gt;qa&lt;/id&gt;
              &lt;properties&gt;
                  &lt;spring.profiles.active&gt;qa&lt;/spring.profiles.active&gt;
              &lt;/properties&gt;
          &lt;/profile&gt;
          &lt;profile&gt;
              &lt;id&gt;prod&lt;/id&gt;
              &lt;properties&gt;
                  &lt;spring.profiles.active&gt;prod&lt;/spring.profiles.active&gt;
              &lt;/properties&gt;
          &lt;/profile&gt;
          &lt;profile&gt;
              &lt;id&gt;eclipse&lt;/id&gt;
              &lt;dependencyManagement&gt;
                  &lt;dependencies&gt;
                      &lt;dependency&gt;
                          &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
                          &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
                          &lt;version&gt;${junit.jupiter.version}&lt;/version&gt;
                          &lt;scope&gt;test&lt;/scope&gt;
                      &lt;/dependency&gt;
                      &lt;dependency&gt;
                          &lt;groupId&gt;org.junit.platform&lt;/groupId&gt;
                          &lt;artifactId&gt;junit-platform-launcher&lt;/artifactId&gt;
                          &lt;version&gt;1.1.1&lt;/version&gt;
                          &lt;scope&gt;test&lt;/scope&gt;
                      &lt;/dependency&gt;
                  &lt;/dependencies&gt;
              &lt;/dependencyManagement&gt;
              &lt;dependencies&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
                      &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
                  &lt;/dependency&gt;
                  &lt;dependency&gt;
                      &lt;groupId&gt;org.junit.platform&lt;/groupId&gt;
                      &lt;artifactId&gt;junit-platform-launcher&lt;/artifactId&gt;
                  &lt;/dependency&gt;
  
              &lt;/dependencies&gt;
          &lt;/profile&gt;
      &lt;/profiles&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>I just want to know what is the right set of annotations to run the test successfully.
  I have tried using different combinations of </p>
  
  <pre><code>@DataJpaTest
  @RunWith(SpringRunner.class)
  @SpringBootTest(classes = Application.class)
  @ExtendWith(SpringExtension.class)
  </code></pre>
  
  <p>If I just use @DataJpaTest with @RunWith(SpringRunner.class)
  I get the following error:</p>
  
  <pre><code>    [ERROR] whenfindByCodeAndCityIsolciThenReturnDestination  Time elapsed: 1.237 s  &lt;&lt;&lt; ERROR!
  org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [insert into dbo.ek_destinations (city_isolci, gatecmt, code) values (?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
          at com.test.repository.DestinationRepositoryTest.whenfindByCodeAndCityIsolciThenReturnDestination(DestinationRepositoryTest.java:51)
  Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement
          at com.test.repository.DestinationRepositoryTest.whenfindByCodeAndCityIsolciThenReturnDestination(DestinationRepositoryTest.java:51)
  Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: EK_DESTINATIONS in statement [insert into dbo.ek_destinations (city_isolci, gatecmt, code) values (?, ?, ?)]
          at com.test.repository.DestinationRepositoryTest.whenfindByCodeAndCityIsolciThenReturnDestination(DestinationRepositoryTest.java:51)
  Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: EK_DESTINATIONS
          at com.test.repository.DestinationRepositoryTest.whenfindByCodeAndCityIsolciThenReturnDestination(DestinationRepositoryTest.java:51)
  </code></pre>
  
  <p>P.S. Just want to mention that this was working fine with spring boot version 2.0.3.RELEASE.
  I can see from the release notes (<a href="https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1.0-M2-Release-Notes" rel="nofollow noreferrer">https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1.0-M2-Release-Notes</a>) that a bootstrap mode was introduced. This may or may not be causing the problem.</p>