JDriven Blog

Awesome AssertJ: Check Base64 Encoded Strings

Posted on by  
Hubert Klein Ikkink

AssertJ has some nice methods to verify string values. If we want to verify a string value is Base64 encoded we can use the isBase64String() method. We can leave out the padding of the value as it is optional. With the method asBase64Decoded() we can decode the value and write our assertions for the decoded value. The method asBase64Decoded() returns a byte[] object and we can use the asString() to convert it into a string value again.

Continue reading →

Awesome AssertJ: Use String Template To Verify String Value

Posted on by  
Hubert Klein Ikkink

To compare string values we can use the isEqualTo(String) method in AssertJ. But if we want to verify that a string contains a certain variable value we can use string templates. This makes the assertion more readable as we can see what value we expect in the string. To use string templates we must the method isEqualTo(String, Object…​). The first argument is the string template and the following arguments will be the actual values that should be used in the template. Actually the String.format(String, Object…​) method is used behind the scenes to format the string template, but we don’t have to clutter our assertions with that call.

Continue reading →

Annotation based Dependency Injection: Breaking Down the Basics

Posted on by  
Jacob van Lingen

Jim has been coding for many years. Slowly he went from novice techie to battered veteran. The soft skin on his chin in now covered by a lush beard. The JVM does no longer hold it’s secrets like it did before. But one thing still bothers him: "Most Web-based frameworks use some kind of annotation-based Dependency Injection. How do they make it work? And could he do it himself?"

Continue reading →

Awesome AssertJ: Use returns To Verify An Object Using Functions

Posted on by  
Hubert Klein Ikkink

With the returns method in AssertJ we can verify an object using a function. This allows us to verify an object in a very flexible way. We can chain multiple returns method calls to verify multiple aspects of our object. The first argument of the returns method is the expected value of the function call. And the second argument is a function that calls a method on the object we want to verify. A simple function call would be a method reference using the class of the object. But we can also write our own function, where the argument of the function is actual object we are writing the assertion for. To verify the function doesn’t return an expected value we can use the method doesNotReturn.
We can also pass the function to the from method, available in the Assertions class. It can make the assertion more readeable as we can now read the code as: we expect the following value from calling this function.

Continue reading →

Awesome AssertJ: Check String Starts Or Ends With A Given Value

Posted on by  
Hubert Klein Ikkink

Writing assertions using the nice fluent API of AssertJ is a joy. Besides some of the basic assertions like isEqualTo AssertJ also has specific assertions for specific types. For example if we want write an assertion to check if a String value starts or ends with an expected value we can use the startsWith(String) or endsWith(String) methods. If we don’t care that a character is upper or lower case we can also use startsWithIgnoringCase(String) or endsWithIgnoringCase(String). Each of the methods also has a counterpart method to check the String value doesn’t start or end with an expected value. For example we can use doesNotStartWith(String) to assert a value does not start with the expected value.

Continue reading →

Awesome AssertJ: Assert toString Method of Object With hasToString Method

Posted on by  
Hubert Klein Ikkink

AssertJ has many useful methods to write assertions using a fluid API. If we want to test the toString() method implementation of an object we can of course invoke the toString() method inside an assertThat expression and check with the assert method isEqualTo(String) the value. But AssertJ can make that easier so we don’t have to invoke toString() method ourselves. We can use the assert method hasToString(String) on the object we defined in the assertThat expression and specify our expected value as argument to the method. If we want to assert the toString() method doesn’t return an expected value we can use the assert method doesNotHaveToString(String).

Continue reading →

Living in a changed world

Posted on by  
Jacob van Lingen

The living room was dimly lit, with the only source of light being the glow of the computer screen in front of a young woman. She sat cross-legged on the floor, headphones on, typing away furiously. Her grandfather, a wiry old man with a thick beard, sat on the couch behind her, watching her work with a sense of pride and wonder.

Continue reading →

Groovy Goodness: Sorting Data With GINQ

Posted on by  
Hubert Klein Ikkink

GINQ (Groovy-INtegerate Query) is part of Groovy since version 4. With GINQ we can use SQL-like queries to work with in-memory data collections. If we want to sort the data we can use orderby followed by the property of the data we want to sort just like in SQL we can use order by. By default the sort ordering is ascending and null values are put last. We can change the sort ordering by specifying in desc with the orderby clause. Or to make the ascending order explicitly we use the statement in asc. Each of asc and desc also can take an argument to specify how we want null values to be sorted. The default way is to keep null values last in the ordering. If we want to make this explicit we use nullslast as argument to asc or desc. To have null values in the sorted result first we use the argument nullsfirst.

Continue reading →

Groovy Goodness: Calculate The Median Of A Collection

Posted on by  
Hubert Klein Ikkink

Since Groovy 4 we can use SQL like queries on in-memory collections with GINQ (Groovy-Integrated Query). GINQ provides some built-in aggregate functions like min, max, sum and others. One of these functions is median. With median we can get the value that is in the middle of the sorted list of values we want to calculate the median for. If the list has an uneven number of elements the element in the middle is returned, but if the list has an even number of elements the average of the two numbers in the middle is returned.

In the following example we see the use of the median function with GINQ:

Continue reading →

Groovy Goodness: Using Tuples

Posted on by  
Hubert Klein Ikkink

Groovy supports a tuple type. A tuple is an immutable object to store elements of potentially different types. In Groovy there is a separate Tuple class based on how many elements we want to store in the tuple. The range starts at Tuple0 and ends with Tuple16. So we can store a maximum of 16 elements in a Groovy tuple.
Each of the classes has a constructor that takes all elements we want to store. But the Tuple class also has static factory methods to create those classes. We can use the tuple method and based on how many elements we provide to this method we get the corresponding Tuple object.

Continue reading →

shadow-left