gpt4 book ai didi

java - 使用 Spring Boot 通过 JUnit4 测试插入 h2database 时出现问题

转载 作者:行者123 更新时间:2023-12-02 06:23:11 27 4
gpt4 key购买 nike

正在关注有关如何设置 Spring Boot 项目并针对 h2 数据库编写单元测试的在线教程...

在编写 JUnit 测试方面我对 Spring Boot 很陌生,而且对使用内存 h2 数据库也很陌生。

写了一个测试,但现在不起作用!

<小时/>

pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

</dependencies>
<小时/>

代码结构

demo
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
│   ├── java
│   │   └── com
│   │   └── example
│   │   └── demo
│   │   ├── Booking.java
│   │   ├── DemoApplication.java
│   │   └── ReservationRepository.java
│   └── resources
│   ├── application.properties
│   ├── static
│   └── templates
└── test
├── java
│   └── com
│   └── example
│   └── demo
│   └── DemoApplicationTests.java
└── resources
└── data.sql
<小时/>

预订.java

@Entity
public class Booking {

@Id
@GeneratedValue
private Long id;

// booking_name
private String bookingName;

// group_size
private int groupSize;

Booking(){ }

public Booking(String bookingName, int groupSize) {
this.bookingName = bookingName;
this.groupSize = groupSize;
}

// Getters & setters omitted for brevity
}
<小时/>

预订存储库

import org.springframework.data.jpa.repository.JpaRepository;

public interface ReservationRepository extends JpaRepository<Booking, Long> {
}
<小时/>

演示应用程序测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

@Autowired
ReservationRepository reservationRepository;

@Test
public void contextLoads() {
Assert.assertNotNull("The reservation repository should be non-null", this.reservationRepository);
}

@Test
public void testLoadingResultsInDatabase() {
List<Booking> bookings = this.reservationRepository.findAll();
Assert.assertNotNull("There must be a response", bookings);
Assert.assertTrue("There should be at least one booking", bookings.size() > 0);
}
}
<小时/>

数据.sql

insert into booking ( booking_name, group_size) values ('LiveLessons', 1000);
insert into booking ( booking_name, group_size) values ('John Doe', 1);
<小时/>

当我运行此 JUnit 测试时:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/home/work/demo/target/test-classes/data.sql]: insert into booking ( booking_name, group_size) values ('LiveLessons', 1000); nested exception is org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "ID"; SQL statement:
insert into booking ( booking_name, group_size) values ('LiveLessons', 1000) [23502-199]

Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/home/work/demo/target/test-classes/data.sql]: insert into booking ( booking_name, group_size) values ('LiveLessons', 1000); nested exception is org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "ID"; SQL statement:
insert into booking ( booking_name, group_size) values ('LiveLessons', 1000) [23502-199]
at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:509) ~[spring-jdbc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:238) ~[spring-jdbc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "ID"; SQL statement:
insert into booking ( booking_name, group_size) values ('LiveLessons', 1000) [23502-199]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:457) ~[h2-1.4.199.jar:1.4.199]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:427) ~[h2-1.4.199.jar:1.4.199]
at org.h2.message.DbException.get(DbException.java:205) ~[h2-1.4.199.jar:1.4.199]
at org.h2.message.DbException.get(DbException.java:181) ~[h2-1.4.199.jar:1.4.199]
<小时/>

我可能做错了什么?

最佳答案

您的 sql 中缺少 ID。

关于java - 使用 Spring Boot 通过 JUnit4 测试插入 h2database 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55815439/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com