【问题标题】:Issue using Spring Boot with Inserting into h2database with a JUnit4 test使用带有 JUnit4 测试插入 h2database 的 Spring Boot 问题
【发布时间】:2019-09-12 21:19:07
【问题描述】:

正在关注有关如何设置 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

Booking.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
}               

ReservationRepository

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

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

DemoApplicationTests

@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]

我可能做错了什么?

【问题讨论】:

  • 嵌套异常的哪一部分是 org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "ID"; SQL 语句: 你不清楚吗?
  • @Uata - 是的,感谢类似问题的 URL! @GeneratedValue(strategy=GenerationType.IDENTITY) 解决了这个问题!

标签: java spring-boot junit junit4 h2


【解决方案1】:

您的 sql 中缺少 ID。

【讨论】:

  • admly - 它不是自动递增的吗?这在 SQL 中会是什么样子?当我尝试将这个插入到预订(1,booking_name,group_size)值(1,'LiveLessons',1000)中时;还是坏了?
  • 如果你使用 JPA,它会生成一个 id,但你应该在 init sql 脚本中明确声明它。
  • 自己检查,插入预订(id,booking_name,group_size)值(1,'LiveLessons',1000);插入预订(id,booking_name,group_size)值(2,'John Doe',1);作品。当然,您可以通过声明生成策略来解决它。
猜你喜欢
  • 2023-03-23
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2014-08-08
相关资源
最近更新 更多