我有一个使用 Spring Boot 构建的项目。在这个项目中,我包含了 gorm-hibernate4-spring-boot插件以便使用 Grails 的 GORM 对象映射糖。运行项目时,GORM 毫无问题地完成任务,一切都很棒。另一方面,测试则是另一回事。在 Grails 项目中,我需要使用 @Mock([DomainOne, DomainTwo]) 注释我的测试用例。 Spring Boot 的 GORM 插件的工作方式有所不同。
有关 Spring Boot 和 GORM 的另一个问题的答案链接到 HibernateMixin来自 grails-datastore-test-support
插件。为了使用这个 mixin,项目还必须具有 grails @TestMixin
注释,该注释包含在提取 Grails 其余部分的工件中。同样的答案还建议使用 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 相关代码保持在最低限度。提前致谢!
最佳答案
如果不拖入 Grails 核心,我就无法让模拟工作。我刚刚删除了 GORM,并使用基于 Spring JPA/Hibernate 的实体和存储库。这些很容易与 Spock 一起使用,并为持久化、更新等提供更可见的代码流。对不起 GORM。
关于grails - Spring Boot、GORM 和单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34194440/