Mar 1, 2016

Unit Testing URI-Based Grails Filters

This week while implementing a simplified version of Spring MVC in a Grails 2.x project, I ran into an interesting Grails bug (OK, more than one; it’s been a rough week).  Specifically, “GRAILS-8702: Unit Testing of URI based Filters seems not to work“. There’s even a discussion about it, but the answers there do not seem to work consistently, or may simply be incomplete for my purposes. While playing around with this issue, I believe I’ve found a workaround.

To begin with, let’s say we have three classes: a simple controller, our filter, and the currently-broken unit test for our filter (in this case using Spock). For the sake of simplicity, our filter will simply set two non-functional headers on the response to the controllerName and actionName.



If you run the test, you’ll see that it fails with the errors that the response headers are not equal to the values we’ve put in. We had expected this syntax to work because it’s similar to how we would specify tests for a filter using the “controller:” and “action:” notation. So how can we resolve this?

First we need to set the request’s requestURI field so that the filter actually intercepts our request. The specific value doesn’t matter, as long as it matches the URI:

This alone, however, is not sufficient to make the tests pass, as the values we need (the controllerName and actionName) are still not set, and an exception may be thrown (depending on how they’re used). For this, we simply change our invocation of the filter, and move away from the uri: call. Thus the complete, passing test will look like:

That’s it! Not the most complicated of solutions, but certainly befuddling the first time you run into it.

Igor Shults

About the Author

Object Partners profile.

One thought on “Unit Testing URI-Based Grails Filters

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
An Exploration in Rust: Musings From a Java/C++ Developer
Why Rust? It’s fast (runtime performance) It’s small (binary size) It’s safe (no memory leaks) It’s modern (build system, language features, etc) When Is It Worth It? Embedded systems (where it is implied that interpreted […]
Getting Started with CSS Container Queries
For as long as I’ve been working full-time on the front-end, I’ve heard about the promise of container queries and their potential to solve the majority of our responsive web design needs. And, for as […]
Simple improvements to making decisions in teams
Software development teams need to make a lot of decisions. Functional requirements, non-functional requirements, user experience, API contracts, tech stack, architecture, database schemas, cloud providers, deployment strategy, test strategy, security, and the list goes on. […]
JavaScript Bundle Optimization – Polyfills
If you are lucky enough to only support a small subset of browsers (for example, you are targeting a controlled set of users), feel free to move along. However, if your website is open to […]