Grails Integration Test Downloads

I am trying to insert some test data into my database for which the BootStrapTest class works.

In my BootStrap.groovy file it was named like this

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

However, when I run my integration tests, this code does not execute. I read that integration tests should load, so I'm pretty confused.

I saw some invasive solutions, such as modifying the TestApp.groovy script , but I would suggest that there is a way through conf for this. Also read this SO question and this one as well , but he did not quite understand.

Maybe I'm misunderstanding something, I have a lot of problems with testing Grails. If it brings something to the table, im uses Intelli JIdea as the IDE.

Any thoughts would be appreciated.

Thank you in advance

+4
source share
4 answers

All bootstrap code must be called from closure Init. Therefore this version should work:

 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 } } 

In addition, you may have a separate bootstrap file for inserting test data, rather than calling BootStrapTest.init (). Any class in the grails-app / conf folder called * BootStrap.groovy (for example, TestBootStrap.groovy) starts at the bootstrap stage. See http://www.grails.org/Bootstrap+Classes

+9
source

From 2.0 documentation :

Migrating to Wednesday

Its often advisable to run code when your application starts based on the environment. To do this, you can use the grails-app / conf / BootStrap.groovy file support to execute for each environment:

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

in BootStrap.groovy you can try something like this

 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
source

this works for me on 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 = { } } 

This integration test passes:

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

All Articles