Grails集成测试不会回滚

编程入门 行业动态 更新时间:2024-10-25 18:25:40
本文介绍了Grails集成测试不会回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我从书中学习Grails Grails In Action 并且我试图从示例中运行集成测试。在本书中,它表示每个集成测试功能都应该在每次测试结束时回滚操作。它不回滚每个事务(因为当我完成数据库很脏)。我试图找出原因并找到了一个名为事务性的属性。据称你将这个属性设置为true,它会使测试用例成为事务性的,但它似乎没有改变行为。我已经包含了下面的单元测试代码。

我使用grails 1.3.7并连接到MySql数据库。测试成功运行,它不会回滚。

UserIntegrationTests.groovy:

$我在这个集成测试中做错了什么,它会跳过回滚吗? b $ b

package com.grailsinaction import framework.TestTools $ b $ class UserIntegrationTests扩展GroovyTestCase { static transactional = true protected void setUp(){ super.setUp()} 保护void tearDown(){ super。 tearDown()} testCreateUser(){ TestTools.banner(log,testCreateUser()) def user = new User (userId:joe,密码:secret) assertNotNull user.save() assertNotNull user.id $ b $ def findUser = User.get(user.id) assertEquals'joe',foundUser.userId } void testSaveAndUpdate(){ TestTools.banner(log,testSaveAndUpdate()) def user = new User(userI d:joe2,密码:secret) assertNotNull user.save() def foundUser = User.get(user.id) foundUser.password =' sesame' foundUser.save() def editedUser = User.get(user.id) assertEquals'sesame',editedUser.password } void testSaveThenDelete(){ TestTools.banner(log,testSaveThenDelete()) def user = new User(userId:'joe3',password:'secret ') assertNotNull user.save() def foundUser = User.get(user.id) foundUser.delete() assertFalse User.exists(foundUser .id)} void testValidation(){ TestTools.banner(log,testValidation()) def user = new User (userId:'chuck-norris',密码:'tiny') assertFalse user.validate() assertTrue user.hasErrors() def errors = user.erro rs assertNotNull错误 errors.allErrors.each { log.info(field:$ {it.field},code = $ {it.code},rejected = $ {it.rejectedValue})} } }

User.groovy

package com.grailsinaction class User {字符串userId 字符串密码日期日期创建概要文件概要 静态约束= { userId(size:3..20, unique:true)密码(size:6..8,验证者:{password,user - > return(password!= user.userId)}) dateCreated() profile(可空值:true)} static mapping = { profile lazy:false } static hasMany = [posts:Post] } pre>

测试执行日志

测试在8:28 PM开始... 欢迎使用Grails 1.3.7 - grails/ 根据Apache标准许可证2.0获得许可$ Grails home设置为:C :\ Users \jmquigley\workspace\apps\Grails\grails-1.3.7 基本目录:C:\ Users \jmquigley\workspace\samples\lang-grails\\ \\ hubbub 正在解析依赖项... 在963ms中解决的依赖项。 运行脚本C:\ Users \jmquigley\workspace\apps\Grails\grails-1.3.7\scripts\TestApp.groovy 设置为测试的环境[groovyc]将1个源文件编译为C:\ Users \jmquigley\workspace\samples\lang-grails\hubbub\target\classes [mkdir]创建目录:C:\ Users \jmquigley\workspace\samples\lang-grails\hubbub\target\test-reports\html [mkdir]创建目录:C:\ Users \jmquigley\workspace \samples\lang-grails\hubbub\target\test-reports\plain 开始集成测试阶段... [groovyc]将1个源文件编译为C:\ Users \jmquigley\workspace\samples\lang-grails\hubbub\target\classes [groovyc]将1个源文件编译为C:\ Users \jmquigley\workspace\samples\\ \\ lang-grails \hubbub\target\classes [信息] 201 10417 @ 20:28:42,959:grails.spring.BeanBuilder:[RuntimeConfiguration]为环境配置数据源:TEST [groovyc]将1个源文件编译为C:\ Users \ jmquigley \workspace\samples \ lang-grails \hubbub\target\test-classes\integration --------------------------- ---------------------------- 运行4个集成测试... 运行测试com.grailsinaction.UserIntegrationTests ... - 从testCreateUser输出 - [INFO] 20110417 @ 20:28:46,897:groovy.util.GroovyTestCase:测试用例:testCreateUser() - 从testSaveAndUpdate - [INFO] 20110417 @ 20:28:47,534:groovy.util.GroovyTestCase:测试用例:testSaveAndUpdate() - 从testSaveThenDelete - 输出[INFO] 20110417 @ 20 :28:47,568:groovy.util.GroovyTestCase:测试用例:testSaveThenDelete() - 从testValidation输出 - [INFO] 20110417 @ 20:28:47,642:groovy.util.GroovyTestCase:Test案例:testValidation() [INFO] 20110417 @ 20:28:47,66 8:groovy.util.GroovyTestCase:字段:密码,代码= size.toosmall,被拒绝=小空 PASSED 1173ms完成测试... ---- -------------------------------------------------- - 测试通过:4 测试失败:0 ----------------------------- -------------------------- [junitreport]正在处理C:\ Users \jmquigley\workspace\samples\ lang-grails \hubbub\target\test-reports\TESTS-TestSuites.xml to C:\Users\JMQUIG~1\AppData\Local\Temp\\\ull90011239 [junitreport ]加载样式表C:\ Users \jmquigley\workspace\apps\Grails\grails-1.3.7\lib\junit-frames.xsl [junitreport]转换时间:415ms [junitreport]删除:C:\ Users \JMQUIG〜1 \AppData\Local\Temp\\\ull90011239 测试已通过 - 在target \test-reports中查看报告应用程序上下文关闭... 应用程序上下文关闭。 处理完成退出码0

解决方案

测试(和服务)在默认情况下是事务性的,所以您通常只在 false 时指定静态事务性 。如果你没有指定方言,它可能是自动检测MySQL,但是这些表是使用可能是MyISAM的默认引擎创建的。 MyISAM表不是事务性的。请确保在使用MySQL时指定InnoDB方言,例如

test { dataSource { dialect = org.hibernate.dialect.MySQLInnoDBDialect driverClassName ='com.mysql.jdbc.Driver' username ='...' password ='...' url ='...' dbCreate ='update'} }

$ b $如果你在所有的环境中使用MySQL,你可以将它移动到顶层,例如

dataSource { pooled = true dialect = org.hibernate.dialect.MySQLInnoDBDialect driverClassName ='com.mysql.jdbc.Driver'}

I'm learning grails from the book "Grails In Action" and I'm trying to run integration tests from the examples. In the book it says that each integration test function should roll back its operations as each test finishes. It is NOT rolling back each transaction (as when I finish the database is dirty). I tried to find out why and found found a property called "transactional". Allegedly you set this property to true and it will make the test case transactional, but it doesn't appear to change the behavior. I have included the code for the unit tests below.

I'm using grails 1.3.7 and connecting to a MySql database. The tests run successfully, it just doesn't rollback. Am I doing something wrong in this integration test that it skips the rollback?

UserIntegrationTests.groovy:

package com.grailsinaction import framework.TestTools class UserIntegrationTests extends GroovyTestCase { static transactional = true protected void setUp() { super.setUp() } protected void tearDown() { super.tearDown() } void testCreateUser() { TestTools.banner(log, "testCreateUser()") def user = new User(userId:"joe", password:"secret") assertNotNull user.save() assertNotNull user.id def foundUser = User.get(user.id) assertEquals 'joe', foundUser.userId } void testSaveAndUpdate() { TestTools.banner(log, "testSaveAndUpdate()") def user = new User(userId:"joe2", password:"secret") assertNotNull user.save() def foundUser = User.get(user.id) foundUser.password = 'sesame' foundUser.save() def editedUser = User.get(user.id) assertEquals 'sesame', editedUser.password } void testSaveThenDelete() { TestTools.banner(log, "testSaveThenDelete()") def user = new User(userId: 'joe3', password: 'secret') assertNotNull user.save() def foundUser = User.get(user.id) foundUser.delete() assertFalse User.exists(foundUser.id) } void testValidation() { TestTools.banner(log, "testValidation()") def user = new User(userId: 'chuck-norris', password: 'tiny') assertFalse user.validate() assertTrue user.hasErrors() def errors = user.errors assertNotNull errors errors.allErrors.each { log.info("field: ${it.field}, code=${it.code}, rejected=${it.rejectedValue}") } } }

User.groovy

package com.grailsinaction class User { String userId String password Date dateCreated Profile profile static constraints = { userId(size: 3..20, unique: true) password(size: 6..8, validator: {password, user -> return (password != user.userId) }) dateCreated() profile(nullable: true) } static mapping = { profile lazy: false } static hasMany = [posts : Post] }

Test Execution Log

Testing started at 8:28 PM ... Welcome to Grails 1.3.7 - grails/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Users\jmquigley\workspace\apps\Grails\grails-1.3.7 Base Directory: C:\Users\jmquigley\workspace\samples\lang-grails\hubbub Resolving dependencies... Dependencies resolved in 963ms. Running script C:\Users\jmquigley\workspace\apps\Grails\grails-1.3.7\scripts\TestApp.groovy Environment set to test [groovyc] Compiling 1 source file to C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\classes [mkdir] Created dir: C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\test-reports\html [mkdir] Created dir: C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\test-reports\plain Starting integration test phase ... [groovyc] Compiling 1 source file to C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\classes [groovyc] Compiling 1 source file to C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\classes [INFO ]20110417@20:28:42,959:grails.spring.BeanBuilder: [RuntimeConfiguration] Configuring data source for environment: TEST [groovyc] Compiling 1 source file to C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\test-classes\integration ------------------------------------------------------- Running 4 integration tests... Running test com.grailsinaction.UserIntegrationTests... --Output from testCreateUser-- [INFO ]20110417@20:28:46,897:groovy.util.GroovyTestCase: Test Case: testCreateUser() --Output from testSaveAndUpdate-- [INFO ]20110417@20:28:47,534:groovy.util.GroovyTestCase: Test Case: testSaveAndUpdate() --Output from testSaveThenDelete-- [INFO ]20110417@20:28:47,568:groovy.util.GroovyTestCase: Test Case: testSaveThenDelete() --Output from testValidation-- [INFO ]20110417@20:28:47,642:groovy.util.GroovyTestCase: Test Case: testValidation() [INFO ]20110417@20:28:47,668:groovy.util.GroovyTestCase: field: password, code=size.toosmall, rejected=tiny null PASSED Tests Completed in 1173ms ... ------------------------------------------------------- Tests passed: 4 Tests failed: 0 ------------------------------------------------------- [junitreport] Processing C:\Users\jmquigley\workspace\samples\lang-grails\hubbub\target\test-reports\TESTS-TestSuites.xml to C:\Users\JMQUIG~1\AppData\Local\Temp\null90011239 [junitreport] Loading stylesheet C:\Users\jmquigley\workspace\apps\Grails\grails-1.3.7\lib\junit-frames.xsl [junitreport] Transform time: 415ms [junitreport] Deleting: C:\Users\JMQUIG~1\AppData\Local\Temp\null90011239 Tests PASSED - view reports in target\test-reports Application context shutting down... Application context shutdown. Process finished with exit code 0

解决方案

Tests (and services) are transactional by default so you only typically specify the static transactional property when it's false. If you haven't specified the Dialect it's probably auto-detecting MySQL but then the tables are created using the default engine which is probably MyISAM. MyISAM tables which are not transactional. Be sure to specify the InnoDB dialect whenever you use MySQL, e.g.

test { dataSource { dialect= org.hibernate.dialect.MySQLInnoDBDialect driverClassName = 'com.mysql.jdbc.Driver' username = '...' password = '...' url = '...' dbCreate = 'update' } }

or if you're using MySQL for all environments, you can move it to the top-level, e.g.

dataSource { pooled = true dialect = org.hibernate.dialect.MySQLInnoDBDialect driverClassName = 'com.mysql.jdbc.Driver' }

更多推荐

Grails集成测试不会回滚

本文发布于:2023-11-07 00:44:06,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   Grails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!