Grails Integration Test DOES NOT OPEN

I am learning Grails from the book " Grails in Action " and I am trying to run the integration tests from the examples. The book says that every integration testing function should roll back its operations as each test completes. This is NOT a rollback of each transaction (as at shutting down the database). I tried to find out why and found a property found called transactional. Presumably you set this property to true and this will make the test case transaction, but it does not seem to change the behavior. I have included code for unit tests below.

I am using grails 1.3.7 and connecting to the MySql database. Tests are successful, it just doesn't roll back. Am I doing something wrong in this integration test that it skips 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 Log

 Testing started at 8:28 PM ... Welcome to Grails 1.3.7 - http://grails.org/ 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 
+4
source share
2 answers

Tests (and services) are transactional by default, so you usually specify the static property transactional when it is false . If you did not specify Dialect, this is probably automatic MySQL detection, but then the tables are created using the default mechanism, which is probably MyISAM. MyISAM tables that are not transactional. Remember to specify InnoDB dialogs whenever you use MySQL, for example.

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

or if you use MySQL for all environments, you can transfer it to the upper level, for example

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

I had the same problem.

  • In my case

environment: STS (IDE), mysql (database)

Checking Grails integration should rollback, as you know (by default).

'Grails integration test' try rolling back when each test is complete.

if your database does not support transaction.?

I am changing the database creation table to create the table, with the option Type = InnoDB. So I can solve it.

create a table something (...) Type = InnoDB;

  • I am a Korean student. I speak English a little. Please try to understand me.
0
source

All Articles