An Introduction to Kotlin
February 23rd, 2016
An overview of the Kotlin programming language which keeps the good parts of Java while reducing boilerplate to promote better Object-Oriented code.
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.
class MyController {
def index() {
render 'success'
}
}
package controller
class UriFilters {
def filters = {
myFilter(uri: '/some/path/**') {
before = {
response.setHeader('controllerName', controllerName)
response.setHeader('actionName', actionName)
// Do something
}
}
}
}
@Mock(UriFilters)
@TestFor(MyController)// Need a controller to test filters
class UriFiltersSpec extends Specification {
void "myFilter() should set the names of the controller and action"() {
when:
withFilters(uri: '/some/path/**') { controller.index() }
then:
response.getHeader('controllerName') == 'my'
response.getHeader('actionName') == 'index'
response.contentAsString == 'success'
}
}
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:
request.requestURI = '/some/path/here'
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:
@Mock(UriFilters)
@TestFor(MyController)// Need a controller to test filters
class UriFiltersSpec extends Specification {
void "myFilter() should set the names of the controller and action"() {
setup:
request.requestURI = '/some/path/here'
when:
withFilters(controller: 'my', action: 'index') { controller.index() }
then:
response.getHeader('controllerName') == 'my'
response.getHeader('actionName') == 'index'
response.contentAsString == 'success'
}
}
That’s it! Not the most complicated of solutions, but certainly befuddling the first time you run into it.
Igor Shults
An overview of the Kotlin programming language which keeps the good parts of Java while reducing boilerplate to promote better Object-Oriented code.
This Spring Boot example shows how you can map JPA entities to REST endpoints and embed them in the HATEOAS output via spring-data-rest and annotations.
Automatically JUnit Test DTO and Transfer Objects
Igor is a self-driven developer with a desire to apply and expand his current skill set. He has experience working with companies of all sizes on the whole application stack, from the database to the front-end. He enjoys working in collaborative environments involving discussion and planning around data modeling and product development.