简体   繁体   English

SQL错误无法通过Spring Boot App中的Cucumber测试创建H2数据库

[英]SQL Error unable to create H2 Database via Cucumber tests in Spring Boot App

I am trying to create a H2 database in my Spring Boot project, when I run a cucumber test. 运行黄瓜测试时,我正在尝试在Spring Boot项目中创建H2数据库。

I have this in my application.yml : 我的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. 我的黄瓜测试存储在src / acceptTest / java文件夹中的Cucumber软件包中。 And the below data.sql file is stored in src/acceptTest/resources folder: 下面的data.sql文件存储在src / acceptTest / resources文件夹中:

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. 当我运行Runner类时,正在执行测试,但未创建数据库。

Below are some of the logs: 以下是一些日志:

Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement 引起原因:org.hibernate.exception.SQLGrammarException:无法准备语句

Caused by: org.h2.jdbc.JdbcSQLException: Table "tclientlink" not found; 原因:org.h2.jdbc.JdbcSQLException:未找到表“ tclientlink”; SQL statement: SQL语句:

The CREATE statement in data.sql does not seem to be getting picked up in my code. data.sql中的CREATE语句似乎没有在我的代码中被使用。 Why could this be happening? 为什么会这样呢?

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 eg localhost/server and then when you will have connection than will be table mapped to your class which has Annotation @Entity. 首先,您需要在application.proprties中建立与服务器(例如localhost / server)的连接,然后在建立连接时将表映射到具有Annotation @Entity的类。 Annotation @Entity is just that all data will be mapped to this object and everything you will reach from this Entity class. 注释@Entity只是将所有数据映射到该对象,并且您将从该Entity类访问的所有内容都将映射到该对象。 But you need first create connection to server. 但是您首先需要创建与服务器的连接。

Check this link to create connection to H2 thought application.properties 检查此链接以创建与H2思想application.properties的连接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM