Grails不使用DataSource.groovy文件进行测试环境

时间:2013-02-21 13:19:24

标签: grails

我想将数据库从inbuild H2更改为Grails中的Mysql数据库,Grails文档说Datasource.groovy文件中的更改将更改数据库,但它仅适用于我的开发环境但不适用于测试环境,即使我已经尝试过删除完整的Datasource.groovy文件并从mysql中删除数据库并尝试运行测试它只是成功,所以任何正文可以帮助我正在做的错误。

我已经在stackoverflow中提出了几乎所有问题,建议在DataSource.groovy文件中进行更改,但它对我不起作用。

dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
username = "root"
logSql =true
password = "root"

properties {
    maxActive = -1
    minEvictableIdleTimeMillis = 1800000
    timeBetweenEvictionRunsMillis = 1800000
    numTestsPerEvictionRun = 3
    testOnBorrow = true
    testWhileIdle = true
    connectionProperties = "[autoReconnectForPools=true]"
    testOnReturn = true
    validationQuery = "SELECT 1"
}
}

hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}

environments {
development {
    dataSource {
        dbCreate = "update" // one of 'create', 'create-drop','update'
        url = "jdbc:mysql://127.0.0.1/devenvi"
    }
}
test {
    dataSource {
        dbCreate = "update" // one of 'create', 'create-drop','update'
        url = "jdbc:mysql://127.0.0.1/testenvi"
    }
}
production {
    dataSource {
        dbCreate = "update"
        url = "jdbc:mysql://127.0.0.1/prodenvi"
    }
}
}

1 个答案:

答案 0 :(得分:2)

1)确保您的测试环境中有一个DataSource.groovy部分,例如:

dataSource {
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    dialect = org.hibernate.dialect.MySQL5InnoDBDialect
}
environments {
    test {
        dataSource {
            url = "jdbc:mysql://liveip.com/liveDb"
            // other environment-specific settings here
        }
    }
}

2)确保您正在进行指定测试环境的构建:

/path/to/grails -Dgrails.env=test war

将生成将使用DataSource.groovy文件中相应环境设置的project.war文件