grails springboot_grails – Spring boot,GORM和单元测试

我有一个使用

Spring Boot构建的项目.在这个项目中,我已经包含了

gorm-hibernate4-spring-boot插件,以便使用Grails的GORM对象映射糖.在运行项目时,GORM可以毫无问题地完成任务,一切都很棒.另一方面,测试是另一回事.在Grails项目中,我需要使用@Mock([DomainOne,DomainTwo])注释我的测试用例.弹簧启动的GORM插件的工作方式不同.

关于Spring Boot和GORM的另一个问题的答案,该问题与grails-datastore-test-support插件链接到HibernateMixin.为了使用这个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测试运行测试.我的规范的setup方法中的代码是上面链接的HibernateDatastoreSpringInitializerSpec中使用的设置的非常直接的转换.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值