In our Groovy scripts we can use the
@Grab annotation. With this annotation we define dependencies for our script and they will be automatically downloaded and added to the class path when we run our script. When we use IntelliJ IDEA we can use a nice intention that is part of the IntelliJ IDEA Groovy support to download the dependencies from our IDE editor. IDEA downloads the dependencies and add it to the project module dependencies. This is useful, because this will also adds code completion for the classes in the dependency to our editor.
If a class implements the
Closeable interface Groovy adds the
withCloseable method to the class. The
withCloseable method has a closure as argument. The code in the closure is executed and then the implementation of the
close method of the
Closeable interface is invoked. The
Closeable object is passed as argument to the closure, so we can refer to it inside the closure.
Writing a parameterized specification in Spock is easy. We need to add the
where: block and use data providers to specify different values. For each set of values from the data providers our specifications is run, so we can test for example very effectively multiple input arguments for a method and the expected outcome. A data provider can be anything that implements the
Iterable interface. Spock also adds support for a data table. In the data table we define columns for each variable and in the rows values for each variable. Since Spock 1.1 we can reuse the value of the variables inside the data provider or data table. The value of the variable can only be reused in variables that are defined after the variable we want to reuse is defined.
We all know writing tests or specifications with Spock is fun. We can run our specifications and when one of our assertions in a feature method invalid, the feature method is marked as failed. If we have more than one assertion in our feature method, only the first assertion that fails is returned as an error. Any other assertion after a failing assertion are not checked. To let Spock execute all assertions and return all failing assertions at once we must use the
verifyAll method. We pass a closure with all our assertions to the method. All assertions will be checked when use the
verifyAll and if one or more of the assertions is invalid the feature method will fail.
Writing unit tests for our handlers in Ratpack is easy with
RequestFixture. We invoke the
handle method and use a
Chain we want to test as argument. We can provide extra details on the fixture instance with a second argument, for example adding objects to the registry or setting the request method. The
handle method returns a
HandlingResult object. This object has the method
exception that we can use to see if an exception occurred in our code under test. The method throws a
HandlerExceptionNotThrownException if the expected exception doesn’t occurr.
Since the introduction of the Jenkins Declarative Pipeline syntax (as opposed to the Scripted Pipeline syntax) the concept of a shared library has become more important as we are otherwise restricted to the sections of the pipeline model. So we’ll make a basic set-up with Continue reading
To run external Groovy scripts in our Java or Groovy application is easy to do. For example we can use
GroovyShell to evaluate Groovy code in our applications. If our script contains print methods like
println we can redirect the output of these methods. The
Script class, which is a base class to run script code, has an implementation for the
println methods. The implementation of the method is to look for a property
out, either as part of a
Script subclass or in the binding added to a
Script class. If the property
out is available than all calls to
println methods are delegated to the object assigned to the
out property. When we use a
PrintWriter instance we have such an object, but we could also write our own class with an implementation for the
print methods. Without an assignment to the
out property the fallback is to print on
In Groovy we can add a method named
call to a class and then invoke the method without using the name
call. We would simply just type the parentheses and optional arguments on an object instance. Groovy calls this the call operator:
(). This can be especially useful in for example a DSL written with Groovy. We can add multiple
call methods to our class each with different arguments. The correct method is invoked at runtime based on the arguments.
To create JSON output with Groovy is easy using JsonBuilder and StreamingJsonBuilder. In the samples mentioned in the links we create a JSON object with a key and values. But what if we want to create JSON with a root JSON array using
StreamingJsonBuilder? It turns out to be very simple by passing a list of values using the constructor or using the implicit method
Since Groovy 2.4.8 we can use the
uncapitalize method on
CharSequence objects. The
capitalize method was already available for a long time, but now we have the opposite as well.
In the following example we see that the
uncapitalize method only replaces the first letter of a
String value to lower case:
assert 'Groovy'.uncapitalize() == 'groovy'
assert 'MrHaki'.uncapitalize() == 'mrHaki'
String message = 'Groovy Rocks!'
assert message.uncapitalize() == 'groovy Rocks!'
Written with Groovy 2.4.8.
Original blog post