title: How to access a in-memory database in spring
body: <p>I am developing a standalone java application using in-memory embedded database. I refereed few documents and I wrote the following code. I am using spring Boot.
   These are the steps I did so far:
  In, pom file I added these dependencies.</p>
  
  <pre><code> &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
               &lt;groupId&gt;com.h2database&lt;/groupId&gt;
               &lt;artifactId&gt;h2&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;version&gt;5.1.36&lt;/version&gt;
      &lt;/dependency&gt; 
  </code></pre>
  
  <p>In application.properties file   </p>
  
  <pre><code> spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  spring.datasource.url=jdbc:mysql://localhost:3306/test
  spring.datasource.username=root
  spring.datasource.password=admin
  </code></pre>
  
  <p>I created a scripts  schema.sql and data.sql
  schema.sql</p>
  
  <pre><code>CREATE TABLE users
  (
      id int(11) NOT NULL AUTO_INCREMENT,
      name varchar(100) NOT NULL,
      email varchar(100) DEFAULT NULL,
      PRIMARY KEY (id)
  );
  </code></pre>
  
  <p>data.sql</p>
  
  <pre><code>insert into users(id, name, email) values(1,'demouser','user@test.com'); 
  </code></pre>
  
  <p>Here is my repository class.</p>
  
  <pre><code>@Repository
  public class UserRepository {
  
      @Autowired
      private JdbcTemplate jdbcTemplate;
  
      @Transactional(readOnly = true)
      public List&lt;User&gt; findAll() {
          return jdbcTemplate.query("select * from users", new UserRowMapper());
      }
  
      class UserRowMapper implements RowMapper&lt;User&gt; {
          @Override
          public User mapRow(ResultSet rs, int rowNum) throws SQLException {
              User user = new User();
              user.setId(rs.getInt("id"));
              user.setName(rs.getString("name"));
              user.setEmail(rs.getString("email"));
              return user;
          }
  </code></pre>
  
  <p>Here is my junit class</p>
  
  <pre><code>@Autowired
      private UserRepository userRepository;
      private EmbeddedDatabase db;
  
      @Before
      public void setUp() {
          // db = new EmbeddedDatabaseBuilder().addDefaultScripts().build();
          Object db = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("schema.sql")
                  .addScript("data.sql").build();
      }
  
      @Test
      public void findAllUsers() {
          List&lt;User&gt; users = userRepository.findAll();
          System.out.println(users.get(0).getName());
          assertNotNull(users);
          assertTrue(!users.isEmpty());
      } 
  </code></pre>
  
  <p>when I run  my junit I am getting the following error.</p>
  
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRepository': Unsatisfied dependency expressed through field 'jdbcTemplate': Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  </code></pre>
  
  <p>Do I have to add any other dependency or download a database.</p>