繁体   English   中英

使用@SpringConfigureMockMvc 和 Hibernate 方言的意外行为

[英]Unexpected behavior using @SpringConfigureMockMvc and Hibernate dialect

我有具有 @SpringBootTest 注释的冲突测试,其中一个具有额外的 @AutoConfigureMockMvc 注释。 我在使用 H2 db 并将休眠方言设置为 PostgreSQL 时犯了一个错误。 在此配置中观察到的行为:

  • 单独启动的每个测试都正常工作。
  • 启动所有测试时,无论顺序如何,都只通过第一次。 其余有:

无法加载 ApplicationContext java.lang.IllegalStateException:无法加载 ApplicationContext

引起:org.springframework.beans.factory.BeanCreationException:在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定义名称为“entityManagerFactory”的bean创建错误:初始化方法调用失败; 嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; 嵌套异常是 org.hibernate.exception.SQLGrammarException: Unable to build DatabaseInformation

  • 当我删除 @AutoConfigureMockMvc adnotationa 时,所有测试都在工作。
  • 当我将方言更改为 H2Dialect 时,一切正常。
  • 无论 Hibernate 方言配置如何,带有 @DataJpaTest 注释的真实存储库测试都可以正常工作

对我来说,错误的方言应该总是或永远不会产生冲突,而不是在第二次使用 Spring Context 进行测试时。 这看起来像是一个意外的行为,但也许我错过了一些东西......

重现行为的最小设置:

import javax.persistence.*;

@Entity
@Table(name = "user_")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
    public Integer id;

}
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@AutoConfigureMockMvc
public class MockTest {

    @Test
    public void test1() throws Exception {
    }

}
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ApplicationTests {

    @Test
    void contextLoads() {
    }

}

应用程序.yml

# ===============================
# = DATA SOURCE
# ===============================
spring:
  datasource:
    driver-class-name: org.h2.Driver
    password: sa
    url: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
    username: sa

# ===============================
# = JPA / HIBERNATE
# ===============================
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect

依赖:

ext.springBootVersion = '2.5.2'

dependencies {
    implementation "org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion"

    testCompileOnly('org.junit.jupiter:junit-jupiter:5.7.2')
    testImplementation "org.springframework.boot:spring-boot-starter-test:$springBootVersion"
    testImplementation 'com.h2database:h2:1.4.200'
    
}
test {
    useJUnitPlatform()
}

Git 示例

暂无
暂无

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

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