代码之家  ›  专栏  ›  技术社区  ›  ooyen

Spring测试:数据库在测试中初始化了两次

  •  1
  • ooyen  · 技术社区  · 6 年前

    添加附加测试后,应使用 @ActiveProfiles ,我得到了异常,这些异常在数据库架构更新时出现。sql脚本在同一数据库上运行了两次。

    我使用什么框架

    • spring:3.2.13.Release & spring-data-jpa:1.6.5.RELEASE hsqldb:2.3.2 & hibernate-entitymanager:4.3.1.FINAL
    • junit:4.12 spring-test:3.2.13.RELEASE

    我在代码/测试中使用什么

    • 创建我使用的数据源 <jdbc:embedded-database id="dataSource"> .../test/resources/spring/testConfig.xml
    • @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring/testConfig.xml"}) 为了我的两个测试
    • 测试 @活动配置文件 扩展了其他测试<-我查过了,如果这是问题所在 here ,但事实并非如此。

    在添加 @活动配置文件

    我不想要的

    create table exampleModel if not exists 
    

    我想要什么

    繁殖方式

    1. 结账 my github-repo

    2. mvn test 在里面 ./test/

    例外

    java.lang.IllegalStateException: Failed to load ApplicationContext
    ...
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring/testConfig.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to execute database script; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource class path resource [testDB/schema.sql]: CREATE TYPE TEXT AS VARCHAR(1000000)
    ...
    Caused by: java.sql.SQLSyntaxErrorException: object name already exists: TEXT in statement [CREATE TYPE TEXT AS VARCHAR(1000000)]
    ...
    Caused by: org.hsqldb.HsqlException: object name already exists: TEXT
    
    1 回复  |  直到 4 年前
        1
  •  0
  •   Mehmet Özdemir    6 年前

    为了重用数据库架构,您可以更改 hibernate.hbm2ddl.auto testConfig.xml )例如,价值观 update . 见: this

        2
  •  0
  •   Pino    5 年前

    修改您的 jdbc:embedded-database 标签如下:

    <jdbc:embedded-database id="dataSource" generate-name="true">
    

    参见Spring文档中与 embedded database support 更多细节。