我使用的是Spring 2.7.7。
我有几个实体,例如
@Table(name = "users")
public class UsersEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition = "INT(11) UNSIGNED")
private Long id;
@Column(name = "username", columnDefinition = "VARCHAR(256)")
private String username;
@Column(name = "password", columnDefinition = "VARCHAR(256)")
private String password;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "users_id"), inverseJoinColumns = @JoinColumn(name = "roles_id"))
@Builder.Default
private Set<RolesEntity> rolesEntity = new HashSet<>();
}
这是测试中使用的application.properties
:
spring.profiles.active=test
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
直到Spring 2.6.14它工作,现在我在创建表中得到如下错误:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table users (id INT(11) UNSIGNED not null auto_increment, password VARCHAR(256), username VARCHAR(256), primary key (id)) engine=InnoDB" via JDBC Statement
并且,在某行之后:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "create table users (id INT[*](11) UNSIGNED not null auto_increment, password VARCHAR(256), username VARCHAR(256), primary key (id)) engine=InnoDB"; expected "ARRAY, INVISIBLE, VISIBLE, NOT NULL, NULL, AS, DEFAULT, GENERATED, ON UPDATE, NOT NULL, NULL, AUTO_INCREMENT, DEFAULT ON NULL, NULL_TO_DEFAULT, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, COMMENT, PRIMARY KEY, UNIQUE, NOT NULL, NULL, CHECK, REFERENCES, AUTO_INCREMENT, ,, )"; SQL statement:
create table users (id INT(11) UNSIGNED not null auto_increment, password VARCHAR(256), username VARCHAR(256), primary key (id)) engine=InnoDB [42001-214]
我知道这个错误是新版本的H2不接受任何MySql命令,比如INT(11) UNSIGNED
。如果我从表定义中删除它,测试就可以工作了。
因此,我尝试在application.properties
中设置如下:
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;MODE=MYSQL
# also change capital of MySql
# spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;MODE=MySQL
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
没有运气。
现在,我如何求解,而不完全编辑表定义?
存储库
存储库在这里,https://github.com/sineverba/online-banking-backend/tree/develop开发分支。
如果克隆它并将pom.xml更改为
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
代替2.6.14,您可以复制错误。
受影响的测试之一是[src/test/java/com/bitbank/repositories/BankAccountTransactionsRepositoryTest.java][1]
,但通常都是存储库。
更改“src/test/resources”下的application.properties
https://stackoverflow.com/users/11731987/evgenij-ryazanov的以下注解
我希望确保使用了正在测试的应用程序属性。
所以,我试着:
1.编辑上面链接的BankAccountTransactionsRepository,添加@TestPropertySource("classpath:application.properties")
1.添加@TestPropertySource(“类路径:application-notexists.properties“)并真正测试崩溃(Failed to load application
和class path resource [application-notexists.properties] cannot be opened because it does not exist
)
1.将src/test/resources
中的application.properties
重命名为application-notexists.properties
并启动Spring(但单元测试存在Mysql错误)
1条答案
按热度按时间olmpazwi1#
最后,我解决了
第一个月
到每个存储库文件(另请参见How to add the mode=mysql to embedded H2 DB in Spring Boot 1.4.1 for @DataJpaTest?)
感谢所有人,还有EvgeniJ,他为我指明了正确的方向。