msgbartop
Just a little about development! Think before you write!
msgbarbottom

22 Apr 08 easyb-test - Grails Tests Made Easy (in a way your boss can read)

Have you ever read about easyb? and about Grails? May be you answer yes to one or both previous questions, but this is the first time you will find them together I guarantee, at least in a plugin form.

This is the first post I’m writing about my latest toy project (at least for now it is only a toy project).

The project is hosted at GitHub, and any help will be very welcome since I do not work every day with Grails, but I did a presentation about grails in the last weekend and wanted to use BDD instead of the traditional GUnit based unit tests, I already use Easyb for some of my Java projects, then I wanted to make Easyb and Grails to work together.

It was not an easy task, because of some craziness of Grails class loader, but it is working fine now.

You can write a test with the easyb sintaxe, and the test will look line this:

scenario "Project creation", {
	given "A new project", {
	  project = new Project()
	}
	when "the project name is supplied", {
		project.name = 'My test project'
	}
	and "there is no other project with that name", {
		validateproject = {
			project.validate().shouldBe true
		}
	}
	then "a new project must be created", {
		validateproject()
		project.save(flush:true).shouldNotBe null
	}
}
scenario "User association with a project", {
	given "Any existing project", {
		project = Project.findAll()[0]
	}
	when "a list of users is supplied", {
		users = [new User(login:'testUser1',password:'testUser1',email:'test1@test.com').save(),new User(login:'testUser2',password:'testUser2',email:'test2@test.com').save()]
	}
	then "the users must be associated with that project", {
		users.each{
			ProjectMembership.link(it,project)
		}
	}
}

You can even show it to your boss, because the output of this test is some thing like this:

Story: project management
  scenario Project creation
    given A new project
    when the project name is supplied
    when there is no other project with that name
    then a new project must be created

  scenario User association with a project
    given Any existing project
    when a list of users is supplied
    then the users must be associated with that project

And the best part of it, is that you can use your tests as the specifications of your next grails project!
If you want to know more about the easyb sintax, take a look at the project site.
If you want to know more about grails, take a look at the project site.
If you want a new functionality in this plugin, I’ll probably accept all patches and push requests you send-me :D
And finally, if you want to install the plugin, download it from here and run:
grails install-plugin grails-easybtest-latest.zip

You will need to place the tests you write in the folder tests/behavior
And to run the tests, just type: grails easyb-test
Any doubts or suggestions leave a comment here :D
I hope it can be useful to others.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Tags: , , , , ,

Reader's Comments

  1. |

    Can you post this in the Grails mailing list and on http://svn.grails-plugins.codehaus.org/ ? The Grails community needs this amazing work desperately ;)

    Reply to this comment
  2. |

    Hi Sakuraba,
    doing it now :D

    Reply to this comment
  3. |

    Are you planning to update the plugin to use easyb 0.9?

    Reply to this comment
  4. |

    Am thinking of working on this a bit to update it to use grails 1.0.3 and the latest easyb, unless you are already doing this.

    Is there a public subversion repo?

    j

    Reply to this comment
  5. |

    I have not started working with it yet.
    You can do it, I’ll thank you a lot!
    There is not a public SVN repo, but there is a public GIT repo (a very better SCM :D )
    You can find more info here: http://github.com/urubatan/easybtest/
    Just create a user in github, clone the repo, and send-me a push request with your changes :D

    Reply to this comment
  6. |

    Yes had started playing with it, but rather discouraged by the fact that a) I cant find tests anywhere in Easyb source code, so were I to change it how would i know if things are broken. b) seeing how you have had to copy paste re-purposed sections of the internal workings of the easyb source is also putting me off.. As soon as it changes again the plugin will stop working.

    It would be better if the core code were properly tested, and then could be altered to allow the injection of the grails classloader.

    Have you ever spoken to the author.. I have not heard any replies from the list

    Reply to this comment
  7. |

    in fact I have spoken with them, and in the current version of easyb, it has protected methods to be overwritten allowing the plugin to extend an easyb class, instead of copying the source and reimplementing it, I just had no time to implement the change in the plugin :D

    Reply to this comment
  8. |

    Interesting.. Still, im still confused as to how such a body of code is developed without tests? Or am i missing something completely?

    Reply to this comment
  9. |

    Ah just heard from Andy and in fact its easyb tests to test itself.. fair enough.. not familiar with how to run them, but hey! sighs relief

    Reply to this comment
  10. |

    I have updated the plugin and its available at the EasyB source tree :)

    Cheers

    Reply to this comment
  11. |

    Wow! Excellent work. Can’t wait to try some BDD on our grails projects.

    Reply to this comment
  12. |

    Hi,

    When I run “grails easyb-test”, it doesn’t load the Bootstrap.groovy.

    Do you know how to load the bootstrap.groovy when run “grails easyb-test”, so that I can have some database table entries preloaded to test with.

    Thanks

    Reply to this comment
  13. |

    OK - i give up - what is the naming convention for putting easyb tests in the tests/behaviour directory? I can’t get any tests to run when I run grails easyb-test

    Thanks.

    Reply to this comment
  14. |

    You need the place your files under

    $PROJECT_NAME/test/behavior

    They need to be called either

    XXyyScenario.groovy / XXyySpecification.groovy

    or

    XXyy.scenario / XXyy.specification

    J

    Reply to this comment
  15. |

    Hello there, I’m having some problems with the jars of easyb.

    I don’t know where can i put the jars and what can i do to ‘register’ that on my app’s classpath.

    Thanks

    Reply to this comment
  16. |

    Did you add easyb’s jars on yours system’s classpath before install grails’s plugin?

    Reply to this comment
  17. |

    Hi,

    I had some problems installing until i renamed the file. downloading the file as ‘grails-easybtest-latest.zip’ gave me this this error ->

    at gant.Gant.this$2$dispatch(Gant.groovy)
    at gant.Gant.invokeMethod(Gant.groovy)
    at gant.Gant.processTargets(Gant.groovy:495)
    at gant.Gant.processTargets(Gant.groovy:480)
    Caused by: java.io.FileNotFoundException: C:\Users\Ryden\.grails\1.1\plugins\grails-easybtest-0.3.zip (The system cannot find the file specified)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.(RandomAccessFile.java:212)
    at org.apache.tools.zip.ZipFile.(ZipFile.java:141)
    at org.apache.tools.ant.taskdefs.Expand.expandFile(Expand.java:136)
    … 22 more
    — Nested Exception —
    java.io.FileNotFoundException: C:\Users\Ryden\.grails\1.1\plugins\grails-easybtest-0.3.zip (The system cannot find the file specified)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.(RandomAccessFile.java:212)
    at org.apache.tools.zip.ZipFile.(ZipFile.java:141)
    at org.apache.tools.ant.taskdefs.Expand.expandFile(Expand.java:136)
    at org.apache.tools.ant.taskdefs.Expand.execute(Expand.java:106)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:105)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at _PluginDependencies_groovy$_run_closure26.doCall(_PluginDependencies_groovy:731)

    I renamed the plugin to -> /tmp/grails-easybtest-0.3.zip
    then i ran -> grails install-plugin /tmp/grails-easybtest-0.3.zip

    at this point the install succeeded.

    It looks quite interesting.. I will try it out. thanks !

    - chris

    Reply to this comment
  18. |

    Hi.. I think I installed the plugin successfully (at least i got no errors). But when i stuck two scenarios under the test/behavior folder I got some confusing
    behavior.

    First

    i ran grails easyb-test

    and i got this output ->

    1 behavior run with no failures

    The odd thing is that I deliberately added code that would make both the tests fail. I would have expected a message indicating that two scenarios ran and 2 of them (both of them) failed.. But I’m only seeing an indication that one scenario was executed (successfully.. which is a surprise).

    Reply to this comment

Leave a Comment