Archive: July 2023

Error handling

Posted on by  
Ties van de Ven

A lot of applications seem to either only use runtime exceptions or only use error monads like the Optional for error handling. In this blog I will try to dive a bit deeper into when to use one over the other (tldr: you probably need both)

Continue reading →

Java Joy: Using mapMulti Method Of The Stream API

Posted on by  
Hubert Klein Ikkink

Since Java 16 we can use the method mapMulti(BiConsumer) of the Stream API. This method allows us to map each element of the stream to multiple elements. We can also do that with the flatMap(Function) method, but if we want to map a limited set of elements, mapMulti is more convenient. Internally a shared stream is used and we don’t have the cost of creating a new stream for each element. Another use case is if the logic to map an element to multiple elements is complex and is hard to implement by returning a stream. Then mapMulti allows us to write that logic in a BiConsumer instead of a Function.

Continue reading →

Awesome AssertJ: Writing Assertions For Optional

Posted on by  
Hubert Klein Ikkink

For a lot of types AssertJ has special assertion methods. Also for the type Optional. If we want to assert an Optional value we can use several methods that AssertJ provides. For example to check if an Optional is present we can use isPresent() or the alias isNotEmpty(). To check if the Optional is empty we can use isEmpty() or the alias isNotPresent(). Checking the value of an Optional (if it is indeed set) can be done with hasValue() or contains(). For more fine grained assertions on the value we can use hasValueSatisfying(Condition) or hasValueSatisfying(Consumer). With the map(Function) and flatMap(Function) methods we can map the Optional, if not empty, to another value and assert that value.

Continue reading →

Awesome AssertJ: Assertions For An URL Object

Posted on by  
Hubert Klein Ikkink

AssertJ has a lot of custom assertion methods for different types. For example to assert an URL object AssertJ gives us some specific methods. We can check for different components of the URL instance with different methods. For example we can check if the protocol is equal to the protocol we expect with hasProtocol(String). Similarly we can write assertions for the host, port, authority, path and anchor. To assert query parameters we can use hasQueryParameter(String) to check if query parameter is set and with hasQueryParameter(String, String) we can check if the query parameter has an expected value. To check the whole query string we can use hasQueryString(String).

Each of the assertion methods also has version to assert a component is not present. For example hasNoQuery() to assert a query is not defined for an URL.

Continue reading →

Using Diffblue Cover to Write Tests for Existing Code

Posted on by  
Quinton Weenink

In a previous blog post we looked into how to get started with Diffblue Cover. In this post, we will focus on utilizing Diffblue Cover to generate tests for a pre-existing project that contains untested code.

I find the best time to write tests to be as you write the code but, sometimes we discover untested code in our projects. Fully understanding someone else’s code that does not have tests written for it can be difficult and quite time-consuming. Let’s try out Diffblue Cover to see if it can help us.

Continue reading →

Team Topologies: Building High-Performance Software Teams through culture

Posted on by  
Erik Pronk

Michael Jordan once said, "Talent wins games, but teamwork and intelligence win championships."

This quote perfectly illustrates the importance of team culture in building high-performance software development teams that can work together effectively to achieve a common goal. In software engineering, this means understanding the concept of team topologies and how they can shape and support team culture.

Continue reading →

Getting Started with Diffblue Cover

Posted on by  
Quinton Weenink

Diffblue Cover is an AI-powered software testing tool that generates unit tests for Java code. Tests can easily be generated directly from your code using the Diffblue Cover IntelliJ plugin. Let’s get started by setting up Diffblue Cover and start generating tests.

Continue reading →

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 →

shadow-left