Posts

Showing posts from 2014

Spring MVC 4 Quickstart Maven Archetype Improved

Spring Boot allows getting started with Spring extremely easy. But there are still people interested in not using Spring Boot and bootstrap the application in a more classical way. Several years ago, I created an archetype (long before Spring Boot) that simplifies bootstrapping Spring web applications. Although Spring Boot is already some time on the market, Spring MVC 4 Quickstart Maven Archetype is still quite popular project on GitHub . With some recent additions I hope it is even better.

Greenshot - productive screenshot tool for Windows

Image
If you are looking for a screenshot software tool for Windows or you are not happy with your current one just give it a try to Greenshot . In this short blog post, I share how I work with Greenshot in Windows 8.1 and hopefully encourage the reader to give it a try.

Unit Testing exercise with FizzBuzz and Mockito

I sometimes use FizzBuzz to demonstrate the basics of unit testing to newbies. Although FizzBuzz is really simple problem, it can also be used to demonstrate more advanced unit testing techniques like mocking .

Unit Testing exercise with FizzBuzz and JUnitParams

I sometimes use FizzBuzz to demonstrate the basics of unit testing to newbies. Although FizzBuzz is really simple problem, it can also be used to demonstrate more advanced unit testing techniques like implementing parametrized tests .

Spring Boot Actuator: custom endpoint with MVC layer on top of it

Image
Spring Boot Actuator endpoints allow you to monitor and interact with your application. Spring Boot includes a number of built-in endpoints and you can also add your own. Adding custom endpoints is as easy as creating a class that extends from org.springframework.boot.actuate.endpoint.AbstractEndpoint . But Spring Boot Actuator offers also possibility to decorate endpoints with MVC layer.

ConEmu - Windows console emulator with tabs

Image
After switching to Git some time ago, I started working more and more with Git Bash on Windows. Git Bash is pretty cool as it provides (apart from Git) Bash supported with basic Unix tools including curl or ssh . Git Bash in Windows has some limitation though including limited customization options and lack of good copy & paste options supported with keyboard shortcuts. Fortunately, there is ConEmu that not only fills that gap but adds various features that make working with console applications more productive and more enjoyable for me.

Spring Boot / Java 8 / Tomcat 8 on Openshift with DIY

DIY cartridge is an experimental cartridge that provides a way to test unsupported languages on OpenShift. It provides a minimal, free-form scaffolding which leaves all details of the cartridge to the application developer . This blog post illustrates the use of Spring Boot / Java 8 / Tomcat 8 application with PostgreSQL service bound to it.

Spring Boot and Spring Data REST - exposing repositories over REST

Image
Exposing Spring Data repositories over REST is pretty easy with Spring Boot and Spring Data REST. With minimal code one can create REST representations of JPA entities that follow the HATEOAS principle. I decided to re-use Spring PetClinic ’s JPA entities (business layer) as the foundation for this article.

Using @ConfigurationProperties in Spring Boot

In my latest blog post I described shortly how one can configure mail in Spring Boot application . To inject properties into the configuration I used Spring’s @Value annotation. But Spring Boot provides an alternative method of working with properties that allows strongly typed beans to govern and validate the configuration of your application. In this post I will demonstrate how to utilize @ConfigurationProperties while configuring the application.

Testing mail code in Spring Boot application

Image
Whilst building a Spring Boot application you may encounter a need of adding a mail configuration. Actually, configuring the mail in Spring Boot does not differ much from configuring it in Spring Bootless application. But how to test that mail configuration and submission is working fine? Let’s have a look.

Validation groups in Spring MVC

Image
All constraints in Bean Validation may be added to one or more groups via groups attribute. This allows you to restrict the set of constraints applied during validation. It can be handy in cases where some groups should be validated before others like e.g. in wizards. As of Spring MVC 3.1, automatic validation utilizing validation groups is possible with org.springframework.validation.annotation.Validated annotation. In this article I will use simple Spring MVC application to demonstrate how easily you can use validation groups to validate Spring’s MVC model attributes. Form Let’s start with the form class that will be validated in steps. Firstly, we define interfaces that represents constraint groups: public class Account implements PasswordAware { interface ValidationStepOne { // validation group marker interface } interface ValidationStepTwo { // validation group marker interface } } Validation contraints Next we assign constra

Spring MVC Integration Testing: Assert the given model attribute(s) have global errors

Image
In order to report a global error in Spring MVC using Bean Validation we can create a custom class level constraint annotation. Global errors are not associated with any specific fields in the validated bean. In this article I will show how to write a test with Spring Test that verifies if the given model attribute has global validation errors.

Spring 4.1 and Java 8: java.util.Optional as a @RequestParam, @RequestHeader and @MatrixVariable in Spring MVC

Image
As of Spring 4.1 Java 8’s java.util.Optional , a container object which may or may not contain a non-null value, is supported with @RequestParam , @RequestHeader and @MatrixVariable . While using Java 8’s java.util.Optional you make sure your parameters are never null .

JUnit: testing exception with Java 8 and Lambda Expressions

Image
In JUnit there are many ways of testing exceptions in test code, including try-catch idiom , JUnit @Rule , with catch-exception library. As of Java 8 we have another way of dealing with exceptions: with lambda expressions. In this short blog post I will demonstrate a simple example how one can utilize the power of Java 8 and lambda expressions to test exceptions in JUnit.

Spring 4: CGLIB-based proxy classes with no default constructor

Image
In Spring, if the class of a target object that is to be proxied doesn’t implement any interfaces, then a CGLIB-based proxy will be created. Prior to Spring 4, CGLIB-based proxy classes require a default constructor. And this is not the limitation of CGLIB library, but Spring itself. Fortunately, as of Spring 4 this is no longer an issue. CGLIB-based proxy classes no longer require a default constructor. How can this impact your code? Let’s see. One of the idioms of dependency injection is constructor injection. It can be generally used when the injected dependencies are required and must not change after the object is initiated. In this article I am not going to discuss why and when you should use constructor dependency injection. I assume you use this idiom in your code or you consider using it. If you are interested in learning more, see the resources section in the bottom of this article. Contructor injection with no-proxied beans Having the following collaborator: packa

HOW-TO: Surround text with quote in IntelliJ

Image
A quick tip on how to surround a selection with quote or brace in IntelliJ.

Better error messages with Bean Validation 1.1 in Spring MVC application

Image
Bean Validation 1.1 , among many new features, introduced error message interpolation using Unified Expression Language (EL) expressions. This allows to define error messages based on conditional logic and also enables advanced formatting options . Added to a Spring MVC application let you display more friendly error messages quite simply. In the first part of this article I will shortly describe message interpolation with EL expressions, in the second part we will build a simple web application with Spring Boot and Thymeleaf that runs on Tomcat 8.

Lambda Expressions and Stream API: basic examples

This blog post contains a list of basic Lambda expressions and Stream API examples I used in a live coding presentation I gave in June 2014 at Java User Group - Politechnica Gedanensis (Technical University of Gdańsk) and at Goyello .

Test Data Builders and Object Mother: another look

Constructing objects in tests is usually a painstaking work and usually it produces a lot of repeatable and hard to read code. There are two common solutions for working with complex test data: Object Mother and Test Data Builder . Both has advantages and disadvantages, but (smartly) combined can bring new quality to your tests.

Listing a ZIP file contents with Stream API in Java 8

In Java 8 java.util.zip.ZipFile was equipped with a stream method that allows navigating over a ZIP file entries very easily. In this blog post I will show a bunch of examples showing how quickly we can navigate over ZIP file entries.

Spring 4: @DateTimeFormat with Java 8 Date-Time API

Image
@DateTimeFormat annotation that was introduced in Spring 3.0 as a part of Formatter SPI can be used to to parse and print localized field values in web applications. In Spring 4.0, @DateTimeFormat annotation can be used with Java 8 Date-Time API ( java.time ) out-of-the-box, without extra effort.

Parsing a file with Stream API in Java 8

Streams are everywhere in Java 8. Just look around and for sure you will find them. It also applies to java.io.BufferedReader . Parsing a file in Java 8 with Stream API is extremely easy.

Spice up your test code with custom assertions

Inspired by the @tkaczanowski talk during GeeCON conference I decided to have a closer look at custom assertions with AssertJ library.

HOW-TO: Quartz Scheduler with Clustering in JEE application with MySQL

Image
Quartz Scheduler is one of the most popular scheduling library in Java world. I had worked with Quartz mostly in Spring applications in the past. Recently, I have been investigating scheduling in JEE 6 application running on JBoss 7.1.1 that is going to be deployed in the cloud. As one of the options I consider is Quartz Scheduler as it offers clustering with database. In this article I will show how easy is to configure Quartz in JEE application and run it either on JBoss 7.1.1 or WildFly 8.0.0, use MySQL as job store and utilize CDI to use dependency injection in jobs. All will be done in IntelliJ. Let's get started.

Spring MVC and Thymeleaf: how to acess data from templates

Spring MVC and Thymeleaf: how to acess data from templates I wrote this article for thymeleaf.org, with a great help of Daniel Fernández. You can find it here: http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html ” 12/11/2016: Updated to Thymeleaf 3.0. Find the examples used in this article in this repository: https://github.com/kolorobot/spring-boot-thymeleaf ( http://blog.codeleak.pl/2014/04/how-to-spring-boot-and-thymeleaf-with-maven.html ) In a typical Spring MVC application, @Controller classes are responsible for preparing a model map with data and selecting a view to be rendered. This model map allows for the complete abstraction of the view technology and, in the case of Thymeleaf, it is transformed into a Thymeleaf context object (part of the Thymeleaf template execution context ) that makes all the defined variables available to expressions executed in templates. Spring model attributes Spring MVC calls the pieces of data that can be accessed during

HOW-TO: Spring Boot and Thymeleaf with Maven

Spring Boot is a great piece of software allowing you to bootstrap Spring application within a few seconds. And it really works. As little configuration as possible to get started. And still possible to change the defaults. Let's see how easily is to bootstrap Spring MVC with Thymeleaf and Maven and work with it in IntelliJ.

CSRF protection in Spring MVC, Thymeleaf, Spring Security application

Cross-Site Request Forgery (CSRF) is an attack which forces an end user to execute unwanted actions on a web application in which he/she is currently authenticated. Preventing CSRF attacks in Spring MVC / Thymeleaf application is fairly easy if you use Spring Security 3.2 and above.

Yet another way to handle exceptions in JUnit: catch-exception

There are many ways of handling exceptions in JUnit ( 3 ways of handling exceptions in JUnit. Which one to choose? , JUnit ExpectedException rule: beyond basics ). In this post I will introduce catch-exception library that I was recommended to give a try. In short, catch-exceptions is a library that catches exceptions in a single line of code and makes them available for further analysis.

HOW-TO: Test dependencies in a Maven project (JUnit, Mocito, Hamcrest, AssertJ)

Image
JUnit itself is not enough for most of today's Java projects. You also need a mocking library, maybe something else. In this mini HOW-TO I present the test dependencies you can start with in a new Java project.

JUnit ExpectedException rule: beyond basics

Image
There are different ways of handling exceptions in JUnit tests. As I wrote in one of my previous posts , my preferable way is using org.junit.rules.ExpectedException rule. Basically, rules are used as an alternative (or an addition) to methods annotated with org.junit.Before , org.junit.After , org.junit.BeforeClass , or org.junit.AfterClass , but they are more powerful, and more easily shared between projects and classes. In this post I will show more advanced usage of org.junit.rules.ExpectedException rule.

Teacher's notes about reporting issues: theory and practice

Image
In 2014, for the second time, I am a lecturer at Postgraduate Studies on Software Testing. I mainly run lectures, actually workshops, about tools and automation. The object of one of the workshops was to present and practice reporting issues in Jira . During the first day of workshop, students were testing a simple web application ( https://github.com/kolorobot/spring-mvc-icm-demo ) based on the test cases they created. As a result of testing they were asked to report their findings.

Colored logs in a console (ANSI styling)

Image
We had recently a big debate about logging in applications at my company. Inspired by my colleague, I decided to check how coloring will work on my Windows 8 64-bit machine . The idea is to have the logs colored in the console. Maybe a small detail, but while going through huge amounts of logs - can be handy. So I gave it a try.

Configure favicon.ico in Spring MVC based application

Favicon is an icon (favicon.ico) associated with your website. Not every website is using favicon though. But most browsers do not care about it and they make request for it anyways. When favicon is not in place the server will return unnecessary 404 Not Found error.

Git bash in IntelliJ IDEA on Windows

Image
One of the top features of the recent release of IntelliJ IDEA 13 is definitively a built-in command-line interface. For me, this is really great feature - especially local terminal . I don't need to abandon the IDE to work with command-line interface anymore, e.g. while working with source code management systems like Git.

Thymeleaf Page Layouts

Usually websites share common page components like the header, footer, menu and possibly many more. These page components can be used by the same or different layouts. There are two main styles of organizing layouts in projects: include style and hierarchical style. Both styles can be easily utilized with Thymeleaf without losing its biggest value: natural templating .