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!

11 comments so far

  1. Sakuraba May 4, 2008 5:22 am

    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 ;)

  2. Urubatan May 6, 2008 5:10 pm

    Hi Sakuraba,
    doing it now :D

  3. Nadeem July 5, 2008 3:30 am

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

  4. j pimmel August 13, 2008 8:50 pm

    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

  5. Rodrigo Urubatan August 14, 2008 10:38 am

    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

  6. j pimmel August 14, 2008 4:27 pm

    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

  7. Rodrigo Urubatan August 14, 2008 4:49 pm

    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

  8. j pimmel August 14, 2008 4:51 pm

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

  9. j pimmel August 14, 2008 5:52 pm

    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

  10. j pimmel September 17, 2008 12:40 pm

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

    Cheers

  11. Hates_ September 17, 2008 8:42 pm

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

Leave a comment

Please be polite and on topic. Your e-mail will never be published.