2013-04-17 1 views
1

에서 "드롭을 만듭니다." 일부 테스트의 경우 통합 테스트를 위해 실제 데이터베이스가 필요합니다.Grails는 프로퍼티 파일을로드하고 사용할 때 dbCreate = 내가 환경 구성에 약간의 문제가있어 동시에

config-integration.properties :

# connection 
db.name=integration_test 
db.host=127.0.0.1 
db.port=3306 
dataSource.driverClassName=com.mysql.jdbc.Driver 
dataSource.url=jdbc:mysql://${db.host}:${db.port}/${db.name}?useUnicode=yes&characterEncoding=UTF-8&autoReconnect=true 
dataSource.dialect=org.hibernate.dialect.MySQL5InnoDBDialect 

# configuration 
dataSource.username=foo 
dataSource.password=bar 
dataSource.dbCreate=create-drop 
dataSource.pooled=true 
dataSource.properties.maxActive=50 
dataSource.properties.maxIdle=25 
dataSource.properties.minIdle=5 
dataSource.properties.initialSize=5 
dataSource.properties.minEvictableIdleTimeMillis=60000 
dataSource.properties.timeBetweenEvictionRunsMillis=60000 
dataSource.properties.maxWait=10000 
dataSource.properties.validationQuery=/* ping */ 

Config.groovy 파일 : 우리가 테스트를 시작 깨끗한 데이터베이스 매번를 위해, 우리는 우리가 이미로드 된 구성 파일에 때 dbCreate 속성을 변경 생각

environment { 
.... 
integration { 
    log4j = { 
     root { debug 'stdout' } 
     warn 'org.apache' 
     warn 'grails.spring' 
     info 'org.codehaus.groovy.grails' 
     warn 'net.sf.ehcache' 
     warn 'org.hibernate' 
    } 

    grails.serverURL = "http://www.foobar.com" 

    if (!grails.config.locations || !(grails.config.locations instanceof List)) { 
     grails.config.locations = [] 
    } 
    // loading default configuration 
    grails.config.locations << "classpath:app/config-integration.properties" 

    grails.gorm.failOnError = true 
} 
.... 
} 

문제 지금이 단순히 작동하지 않습니다. 테이블이 작성되지 않으므로 SQL 예외로 테스트가 종료됩니다. 잘못된 것으로 변경하면 잘못된 사용자 이름/비밀번호에 관한 메시지조차 없습니다. 그래서 grails/hibernate가 데이터베이스에 테이블을 생성해야하는 순간에 설정이로드되지 않은 것처럼 보입니다.

편집 : 나는 잊었다 무엇 : 내가 Config.groovy 파일 자체에 데이터 소스를 정의하는 경우, 그것은 작동합니다.

사람은 내가 잘못 여기서 뭘하는지 생각이 있습니까?

미리 감사드립니다.

+0

config-integration이 클래스 경로에 있는지 확인하십시오. groovy로 변환하고 println을 사용하여 파일이로드되었는지 확인할 수도 있습니다. –

+0

다른 환경 설정과 마찬가지로 grails-app/conf/app에 있으며, 환경에 관계없이 사용자 정의 환경에 상관없이 올바르게로드됩니다. create-drop 부분과 특정 링크가있는 것 같습니다. b/c 데이터베이스가 이미 설정되어있는 경우에도 작동합니다 (테스트의 옵션 b/c는 아닙니다). – Moritz

답변

0

그래서, 이유를 알 수는 없지만, grails 앱이 실제로 시작되고 configs가로드되기 전에 & 작성 물건이 Hibernate에 의해 완료되는 것처럼 보입니다. 그러므로 나는 다른 접근법을 사용하고 configs를 "수동으로"로드하고 속성을 설정합니다. 지금이 모양이 잘 작동합니다.

integration { 
    log4j = { 
     root { debug 'stdout' } 
     warn 'org.apache' 
     warn 'grails.spring' 
     info 'org.codehaus.groovy.grails' 
     warn 'net.sf.ehcache' 
     warn 'org.hibernate' 
    } 

    def props = new Properties() 

    new File("grails-app/conf/app/appconfig-integration-tests.properties").withInputStream { 
     stream -> props.load(stream) 
    } 

    def user_file = new File("grails-app/conf/app/appconfig-${System.getProperty("user.name")}.properties") 
    if(user_file.exists()) { 
     user_file.withInputStream { 
      stream -> props.load(stream) 
     } 
    } 

    dataSource { 
     username = props.get("dataSource.username") 
     password = props.get("dataSource.password") 
     dbCreate = props.get("dataSource.dbCreate") 
     driverClassName = props.get("dataSource.driverClassName") 
     dialect = props.get("dataSource.dialect") 
     url = props.get("dataSource.url") 
    } 

    grails.serverURL = "http://www.foobar.com" 

    grails.gorm.failOnError = true 
} 
0

이것은 내가 찾고 있어요 절름발이 설명 될 수있다, 그러나 당신은

environment{ 
    test{ 
    .... 
    } 
} 

또는 설정 파일에

environment{ 
    integration{ 
    .... 
    } 
} 

DSL을 사용하고 있습니까?

내가 잘못 생각하지 않으면 우리는 development, testproduction 개의 환경 DSL 만 사용할 수 있습니다.

+1

아니요, 원하는만큼 많은 환경을 정의 할 수 있습니다. 시작시 -Dgrails.env = $ env 매개 변수를 사용해야합니다. 또한 http://www.grails.org/Environments – Moritz

+0

@Moritz 당신이 -Dgrails.env = 통합 테스트 - 응용 프로그램 Grails는 테스트를 실행하는보기? –

+0

실례합니다. 나는'와'Grails의 -Dgrails.env = 통합 테스트 응용 프로그램을'시도 *'conf의/app'와의 .properties''*'SRC/groovy'에서 .groovy'. 전자의 경우, mysql 드라이버는 발견되지 않았지만 ('ClassNotFoundException'), 나중의 경우에 config를로드 할 수있었습니다. 나는 그것을 더 자세히 살펴볼 것이다. (Grails 2.2.0) – dmahapatro

관련 문제