2010-09-10 132 views
4

我试图将一些测试数据插入到我的数据库中,为此,一个名为BootStrapTest的类可以完成这项工作。集成测试中的Grails引导

在我BootStrap.groovy文件其所谓像这样

environments { 
      test { 
       println "Test environment" 
       println "Executing BootStrapTest" 
       new BootStrapTest().init() 
       println "Finished BootStrapTest" 
      } 

     } 

然而,当我运行我的集成测试,这段代码犯规执行。我读过集成测试应该引导,所以我很困惑。

我看到了一些有创性的解决方案,比如modifying the TestApp.groovy script,但我会想象通过conf来实现这一目标的道路。也读this SO questionthis one as well,但并没有完全明白。

也许我误解了一些东西,我在使用Grails测试时遇到了很多麻烦。如果它带来了任何东西,即时通讯使用Intelli JIdea作为IDE。

任何想法将不胜感激。

在此先感谢BootStrap.groovy中

回答

0

,你可以尝试这样的事情

if (!grails.util.GrailsUtil.environment.contains('test')) { 
    log.info "In test env" 
    println "Test environment" 
    println "Executing BootStrapTest" 
    new BootStrapTest().init() 
    println "Finished BootStrapTest" 
} else { 
    log.info "not in test env" 
} 
+0

我不认为'正在所有执行BootStrap.groovy'。 – Tom 2010-09-10 17:49:32

+0

您正在做其他不正确的事情,bootstrap必须在集成测试过程中运行 – 2010-09-10 18:52:21

0

这个工作对我来说在1.3.4:

def init = { servletContext -> 
     println 'bootstrap' 
     switch (GrailsUtil.environment) { 
      case "test": 
      println 'test' 
      Person p=new Person(name:'made in bootstrap') 
      assert p.save(); 
      break 
      } 
    } 
    def destroy = { 
    } 
} 

这种集成测试通过:

@Test 
void testBootStrapDataGotLoaded() { 
    assertNotNull Person.findByName('made in bootstrap') 
} 
9

必须从Init关闭调用所有引导代码。所以,这个版本应该工作:

import grails.util.Environment 

class BootStrap { 
    def init = { servletContext -> 
     // init app 
     if (Environment.current == Environment.TEST) { 
      println "Test environment" 
      println "Executing BootStrapTest" 
      new BootStrapTest().init() 
      println "Finished BootStrapTest" 

     } 
    } 

    def destroy = { 
     // destroy app 
    } 

} 

或者,你可以有插入的测试数据,而不是调用BootStrapTest.init一个单独的引导文件()。在bootstrap阶段运行名为* BootStrap.groovy(例如TestBootStrap.groovy)的grails-app/conf文件夹中的任何类。见http://www.grails.org/Bootstrap+Classes

2

2.0 documentation

每环境中启动

它经常需要在你的应用程序在每个环境基础上启动运行代码。要做到这一点,你可以使用的grails-app/conf目录/ BootStrap.groovy中文件的支持,每个环境执行:

def init = { ServletContext ctx -> 
    environments { 
     production { 
      ctx.setAttribute("env", "prod") 
     } 
     development { 
      ctx.setAttribute("env", "dev") 
     } 
    } 
    ctx.setAttribute("foo", "bar") 
}