Spring 引导、GORM 和单元测试

Spring boot, GORM, and unit tests

我有一个使用 Spring Boot 构建的项目。在这个项目中,我包含了 gorm-hibernate4-spring-boot 插件,以便使用 Grails 的 GORM 对象映射糖。当 运行 启动项目时,GORM 毫无问题地完成了它的工作,一切都很好。另一方面,测试是另一回事。在 Grails 项目中,我需要用 @Mock([DomainOne, DomainTwo]) 注释我的测试用例。 spring 引导的 GORM 插件工作方式不同。

关于 Spring 另一个问题的答案链接到 HibernateMixin from the grails-datastore-test-support plugin. In order to use this mixin, a project must also have the grails @TestMixin annotation which is included in an artifact that pulls in the rest of Grails. This same answer also suggested use of HibernateDatastoreSpringInitializer 以便在规范的 setup() 方法中初始化 GORM。不幸的是,尽管这似乎是最好的选择,但我无法让它发挥作用。

这是我的 build.gradle:

buildscript { // Defines dependencies and repos for the build script itself
  repositories { mavenCentral() }
  dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE") }
}

apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'spring-boot'

version = 1.0
mainClassName='foo.bar.Baz'

repositories {
  mavenCentral()
  maven { url 'http://repo.spring.io/libs-milestone' }
  maven { url 'http://repo.spring.io/libs-snapshots' }
  flatDir { dirs 'lib' }
}

dependencies {
  compile("org.springframework.boot:spring-boot-starter-jdbc") {
      exclude group: "org.springframework.boot", module: "spring-boot-starter-logging"
  }
  compile("org.apache.activemq:activemq-broker") // ActiveMQ
  compile('commons-io:commons-io:2.4') // Commons IO
  compile("org.springframework.boot:spring-boot-starter-log4j") // log 4j
  compile('org.codehaus.groovy:groovy-all')
  compile('org.postgresql:postgresql:9.4-1201-jdbc41') // JDBC
  compile('log4j:log4j:1.2.17') // Default logging provider
  compile("org.grails:gorm-hibernate4-spring-boot:5.0.0.RC2") // I summon thee, gorm
  testCompile("org.spockframework:spock-core:0.7-groovy-2.0")
  testCompile("org.springframework.boot:spring-boot-starter-test")
  testCompile('com.h2database:h2:1.3.148') // h2 for GORM use during tests
}

task wrapper(type: Wrapper) {
  gradleVersion = '2.3'
}

这是我的测试用例:

class DataTypeValidatorSpec extends Specification {

    def setup() {
        // Fails with "org.hibernate.HibernateException: No Session found for current thread"
        def datastoreInitializer = new HibernateDatastoreSpringInitializer(MyDomainClass)
        def applicationContext = new GenericApplicationContext()
        def dataSource = new DriverManagerDataSource(
            "jdbc:h2:mem:grailsDb1;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_DELAY=-1",
            'sa', ''
        )
        dataSource.driverClassName = Driver.name
        applicationContext.beanFactory.registerSingleton("dataSource", dataSource)
        datastoreInitializer.configureForBeanDefinitionRegistry(applicationContext)
        applicationContext.refresh() // is this needed?
    }

    void "should do gorm stuff"() {
        given:
            MyDomainClass instance = new MyDomainClass(name: 'foo').save()
        when:
            MyDomainClass locatedInstance = MyDomainClass.findByName('foo')
        then:
            instance == locatedInstance
    }

}

我 运行 使用 gradle test 进行测试。我的规范的设置方法中的代码是对上面链接的 HibernateDatastoreSpringInitializerSpec 中使用的设置的非常直接的翻译。

请注意,这不是 Grails 项目,如果可能的话,我希望将与 Grails 相关的代码保持在最低限度。提前致谢!

这是一个 Spring 引导应用程序,因此请使用引导集成测试注释:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

如果您使用这些 GORM 将使用 gorm-hibernate4-spring-boot

提供的机制加载

如果不拖入 Grails 核心,我无法让模拟工作。我刚刚删除了 GORM 并使用了 Spring 基于 JPA/Hibernate 的实体和存储库。这些很容易与 Spock 一起使用,并为持久化、更新等提供更可见的代码流。抱歉 GORM。