【问题标题】:maven sprinbootest junit 5 is not populating properly database with datesmaven sprinbootest junit 5 没有用日期正确填充数据库
【发布时间】:2021-12-31 23:20:53
【问题描述】:

我正在使用 springboot 和 JPA,但在通过 maven 测试时遇到问题。当我从 IDE 运行时,它们通过了。

问题与日期有关。我在 resources/data.sql 中有此插入:

插入订单(order_id、number、client_id、created_on) 值 (2, '0002', 1, '2021-11-21 16:12:20.414');

但是当我恢复 order_id = 2 时,created_on 日期为空

这仅在我使用 mvn install 运行时发生,直接从 IDE 运行测试已正确设置日期。

pom 依赖:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>


    <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>5.3.5.Final</version>
        </dependency>

    <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

有什么想法吗?

谢谢

【问题讨论】:

  • 首先我看到您已经手动定义了休眠等版本,这在 Spring Boot 项目中通常是错误的方式......让 Spring Boot 定义您正在使用的版本。此外,您已将 junit 定义为依赖项,但尚未将 junit 的范围定义为测试...我还建议使用更新版本的 spring boot (2.6.0) 而不是旧版本..这是 EoL

标签: java spring-boot maven unit-testing junit


【解决方案1】:

没有看到测试很难说,但我认为问题在于数据库中的日期格式和您通过测试的日期格式。数据库接收到不正确的格式,因此它输入 null。 如果您可以显示一些代码,也许我可以提供更多帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 2012-10-07
    • 2015-05-20
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多