Java 8 StringJoiner

At the release of Java 8 the most attention went to the Lamda’s, the new Date API and the Nashorn Javascript engine. In the shade of these, there are smaller but also interesting changes. Amongst them is the introduction of a StringJoiner. The StringJoiner is a utility to delimit a list of characters or strings. You may recognize the code below:

Continue reading

Clone Hibernate Objects

When we want to clone an object there are several ways to do this
For instance we can implement Clonable, which makes it possible to duplicate an object. We also can create a new object manually by calling each setter or use a parameterised constructor.

In case we want to clone a Hibernate object, there is an extra option available which is more elegant: the Hibernate3BeanReplicator. The Hibernate3BeanReplicator is provided by Beanlib (http://beanlib.sourceforge.net/) and it supports deep clones, so we can also clone related one-to-one objects easily.

For example we want to clone the Student object, including all child (one-to-one) objects.

When we use auto increment id’s or unique required fields, we manually have to set these values to null or use an unique value. Please note that we also set the Id of the RelatedObject to null.

As can be seen cloning an object can be very simple! More information can be found on http://beanlib.svn.sourceforge.net/viewvc/beanlib/trunk/beanlib-doc/hibernate-bean-replicator.html

Wicket quick tips: create a download link

Apache Wicket is a populair web framework. There are a many reasons why I like to use Wicket, for instance: it offers a great mark-up/logic separation and using Wicket it’s very easy to implement AJAX functionality without writing one line of Javascript.

To provide you with simple and short tips and tricks for Wicket I write this series of blogs. In this first blog of the series I will show you how to create a download link in several ways.

Continue reading

Auto inject dependencies in JUnit using Mockito

While writing unit tests, you often have to mock dependencies like services or controllers. Often  a constructor is used to autowire the dependencies as shown in the example below. In the Test class I instantiated the ContactService using a contactRepository Mock object

The test class:

In the previous example you always have to Autowire the constructor and bind the dependencies to the class variables. In the next example the code is cleaner by autowiring the mocking objects, so you don’t have to create a custom constructor or setters to set the dependencies, which makes your code more concise and easier to read.

The test class:

Take a note of the “RunWith(MockitoJUnitRunner.class)” annotation. This annotation will initialize the annotated mock objects. An alternative is to use “MockitoAnnotations.initMocks(this)” in a setup method to initalize the annotated mock objects.

More tips can be found on the Mockito RefCard: http://refcardz.dzone.com/refcardz/mockito or take a look at the mockito site: http://www.mockito.org.

Consume REST JSON webservices easily using Spring Web!

Spring made it very easy to consume JSON webservices. In this article I describe how to receive and parse JSON and how to send your Java objects as JSON.

First we need to include the required dependencies. If you use maven, include the following dependencies:

Spring web has a RestTemplate class which can be used to call the REST webservices. The Jackson dependency supplies a message converter class which can be used to send and receive Java objects which are automatically converted to JSON en reversed from JSON.

Continue reading