I’m embarking, balls-out, on a new project using Grails. I’ll be leveraging a significant portion of our Java codebase and the fact that I can do that,is just plain stellar.

It quickly came time to test out some of the integration. The very first question was how to load resources for testing. E.g., I have a service that handles file uploads. How do I pass it a test file?

The documentation isn’t straightforward on this. In fact, I found the solution attached to a bug report for grails 1.1. It’s pretty simple, once you pull in the spring packages:

import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.Resource

And then in your testXXX() function:

Resource resource = new ClassPathResource("resources/crying-baby.jpg")
def file = resource.getFile()
assert file.exists()

And you are good to go. Note the assert at the end to verify things are working.

About the Author:

Learned something? Great! Need help on your development project? I can help @ Brilliant Chemistry or get in touch direct:

  • Ruby on Rails
  • iOS Development
  • System Architecture & Performance

Get in touch:


  1. Matt Passell says:

    When I first saw the title of this post, I read it as “load test” files as opposed to load “test files”. 😉 (“Huh… I didn’t see anything about load testing in this post… Oh, I get it!”)


    • mike says:

      Yeah, Sorry! I see that now. I changed the title to be more clear.

      • phaggood says:

        > Resource resource = new ClassPathResource(“resources/crying-baby.jpg”)

        what’s the full path for ./resources when running an integration ? Is it under ./test or web-apps/WEB-INF?

  2. sean owen hayes says:

    if you leave it in the unit test folder or integration test folder that is fine. However, I imagine you have either figured it out or given up by now 😉

Leave a Comment