1

I am trying to create a H2 database in my Spring Boot project, when I run a cucumber test.

I have this in my application.yml:

booking:
  datasource:
    username: sa
    password:
    url: jdbc:h2:mem:bookingdb;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1
    driver-class-name: org.h2.Driver

My cucumber test is stored in my Cucumber package in src/acceptTest/java folder. And the below data.sql file is stored in src/acceptTest/resources folder:

CREATE TABLE tlink (
  link_id int,
  ext_id varchar(255),
  address_id varchar(255),
  client_id varchar(255),
  instance varchar(255),
  source varchar(255),
  timestamp datetime2
);

INSERT INTO TLINK(link_id, ext_id, address_id, client_id, instance, source, timestamp) VALUES(13582,'0000059811','3037260','0000059811','1','1', '2018-08-22 15:13:34');

When I run the runner class, the tests are being executed, but the database isn't being created.

Below are some of the logs:

Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement

Caused by: org.h2.jdbc.JdbcSQLException: Table "tclientlink" not found; SQL statement:

The CREATE statement in data.sql does not seem to be getting picked up in my code. Why could this be happening?

Community
  • 1
  • 1
  • Your SQL looks fine to me, which makes me think that the SQL code is not even being run. – Tim Biegeleisen Sep 15 '18 at 09:09
  • This doesn't make any sense. ` java.net.ConnectException: Connection timed out: connect` means that your connection string is wrong. If your connection string is wrong, your program should be dead and you should not be receiving any further error messages about table tlink not found. – Mike Nakis Sep 15 '18 at 09:10

1 Answers1

0

What your message in log said is that you don't have connection to your database. At first you need establish in application.proprties connection to your server e.g. localhost/server and then when you will have connection than will be table mapped to your class which has Annotation @Entity. Annotation @Entity is just that all data will be mapped to this object and everything you will reach from this Entity class. But you need first create connection to server.

Check this link to create connection to H2 thought application.properties

https://dzone.com/articles/integrate-h2-database-in-your-spring-boot-applicat

  • Thanks, I have an application.yml file in my test folder. I have done something similar to what your link is suggesting. I will post it when I can later today. Thank you –  Sep 15 '18 at 09:31
  • I have updated my question above with the _application.yml_ –  Sep 16 '18 at 10:59
  • 1. You need to create table with sql. @Entity is just mapping for tables. You are not creating db table with @Entity. 2. Your insert and create looks OK. Do you have h2 dependency in pom.xml ? ` com.h2database h2 runtime ` Look at this link http://www.springboottutorial.com/spring-boot-and-spring-jdbc-with-h2 –  Sep 17 '18 at 07:58
  • In my _build.gradle_ dependencies, I have `testRuntime 'com.h2database:h2'` –  Sep 17 '18 at 08:58
  • In my application.yml, there are 2 different databases that need to point to a H2 instance, which is why this scenario is trickier than any tutorials I've found online –  Sep 17 '18 at 08:59
  • 1
    I've split out my sql into schema.sql and data.sql. I I am able to log into the H2 console, but the table isn't being created via the code. –  Sep 17 '18 at 12:00
  • Have you called your sql script?. Try look at this link https://stackoverflow.com/questions/39280340/spring-boot-run-sql-scripts-and-get-data-on-application-startup in application.yml set your script which will be invoked –  Sep 17 '18 at 12:10
  • I thought that once you run the tests, spring will look for data.sql –  Sep 17 '18 at 12:43
  • Spring look at your application.yml and you need to define which sql will be triggered. In that answer it is defined what you need to set to invoker sql script. –  Sep 17 '18 at 12:59
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180205/discussion-between-adviseforfree-and-user9847788). –  Sep 17 '18 at 13:34