Posts

Showing posts from 2015

org.postgresql.util.PSQLException: This connection has been closed in Spring Boot

In Spring Boot data source connection pooling is auto-configured if a proper JDBC Driver is used (e.g. org.postgresql.Driver) and by default Tomcat pooling is used - although it can be easily changed). But the default configuration may not be enough in the production environment. One of the issues I had was org.postgresql.util.PSQLException: This connection has been closed exception after Postgres database was restarted while the application was running. In order to restore the connection application restart is needed.

5 reasons why you should consider upgrading your applications to Spring 4

Firstly released in 2004, Spring Framework is among top Java frameworks. Spring 4 has been released in December 2013 and it is the first version of framework to support Java 8. Learn why you should consider upgrading your applications to Spring 4.

Learn REST with Spring

The popularity of REST-style APIs is not decreasing so a good knowledge in this architecture style becomes essential in many projects. In Java, there are many frameworks that you as a developer can utilize in order to build a REST-style API, with Spring MVC as one of the most popular. If you wish to learn how to work effectively with REST in Spring you definitely need some good learning materials.

Spring Boot with Gradle and resource filtering

Recently, while working on a rather small Spring Boot web application, I encountered an issue: after the deployment of a WAR file the application did not serve fonts and and other binary files properly. And the issue was with the invalid configuration of resource filtering in Gradle.

HOW-TO: Register components using @Conditional and Condition in Spring

@Profile annotation in Spring can be used on any Spring components (e.g. @Component, @Service, @Configuration etc.) that are candidates for auto-detection. @Profile annotation accepts a single profile or a set of profiles that must be active in order to make the annotated component eligible for auto-detection. For a given @Profile({"p1", "!p2"}), registration will occur if profile p1 is active or if profile p2 is not active. The or is crucial here. But how to achieve this with @Profile: we want to activate a given component if profile p1 is active and if profiles p2andp3 are both inactive?

HOW-TO: Java 8 Date & Time with Thymeleaf and Spring Boot

If you happen to work with Spring Boot and Thymeleaf and you need to format Java 8 Date & Time objects in your views you may utilize thymeleaf-extras-java8time - Thymeleaf module for Java 8 Date & Time API.

Different ways of testing exceptions in Java and JUnit [Link]

Exception testing with Java and JUnit is one of the topics that got quite some attention on my blog. Therefore I gathered some of my previous articles in this topic, revised them and created a new article on my company’s blog. It is a compilation of practices for testing exceptions with Java and JUnit. Find out how to use @Test annotation with expected attribute, JUnit ExpectedException rule and AssertJ 3+ in one extensive article with lots of examplesPlease read the full article here: Different ways of testing exceptions in Java and JUnit.

${... } placeholders support in @Value annotations in Spring

${...} placeholders in @Value annotation are used to access properties registered with @PropertySource. This is extremely useful with @Configuration beans in Spring applications, but not only. To be sure that this is possible, PropertySourcesPlaceholderConfigurer must be present in all application contexts that placeholders resolution is required. In this blog post you will learn how to configure placeholders resolution in Spring 4 applications and how to inject different types of objects using @Value annotation, including JSR-310 Date-Time, JSR-354 Money & Currency or java.util.Optional.

AssertJ's SoftAssertions - do we need them?

Image
One of the rules of writing good unit test is that it should fail for one reason, so unit test should test one logical concept. Sometime it is quite tough to have a single assertion per test. To follow the rule, we may have having multiple assertions per object in a single test.The problem with multiple assertions in a single test though, is that if the first one fails for any reason we actually don’t know about other assertions as they will not be executed. And you know the drill: you check assertion failure reason, you fix it and re-run the test. Maybe you are lucky and the test will pass. But maybe it will fail with another assertion. With really fast unit tests this is not a big issue but when it comes to, for example, Selenium tests analysis and failure detection can become cumbersome and for sure time consuming.Fortunately, we can re-think the way we create assertions in our tests thanks to AssertJ’s SoftAssertions.

Override Spring Framework version in Spring Boot application built with Gradle

Image
If you want to use or just check the newest version of Spring with Spring Boot but the current Spring Boot version depends on an older Spring version you need to adjust your Gradle build configuration slightly.For example, as of time of writing this blog post, Spring 4.2.1 and Spring Boot 1.2.5 were current versions. Spring Boot 1.2.5 depends on Spring 4.1.7. So what to do to in order to use Spring 4.2.1 with Spring Boot 1.2.5? Have a look at two ways of achieving this: with and without Spring IO Platform.

Selenium + Firefox on Windows 10 - get rid of the Windows 10 intro URL being loaded

Image
Recently I moved to Windows 10 and upgraded to the newest Firefox. When I executed my integration tests on my PC with Selenium and Firefox for the first time I saw an extra page was loaded: “Firefox + Windows 10. Perfect together.” Maybe for you - I said to myself - and I immediately looked how to disable it.

Parameterized integration tests with Spring JUnit Rules

Image
Spring 4.2 comes with brand new JUnit rules: SpringClassRule and SpringMethodRule. The main advantage of using JUnit rules is to let developers get rid of SpringJUnit4ClassRunner and utilize different JUnit runners in Spring integration tests. I think the biggest opportunity with Spring JUnit Rules is the ease of creating parameterized integration tests.

Sahi Script Samples from my presentation at TrojQA

Sahi is an web automation tool that can be used to test modern web applications in a variety of browsers. Sahi comes in two flavors: open-source and commercial. Some of the key Sahi features include:recording on most modern browsers - including Internet Explorer, automatic AJAX waitseasy to learn and robust scripting - Sahi Scriptbuilt-in tools like reporting, logging etc.This blog post contains some code snippets of Sahi Script that I presented during my talk at TrojQA in 2015. TrojQA is a Tricity (Poland) community focused on software testing and quality assurance.If you are interested in learning more about Sahi, please visit its official website here: http://sahipro.com/.Sahi ScriptSahi Script is a scripting language based on JavaScript. The main difference from JavaScript is that in Sahi Script variables must be prefixed with $._include("StringUtils.js") var _system = (function () { var getenv = function ($name, $default) { var $env = java.lang.System.geten…

[Angular2]="{{'Hello, World!'}}"

Image
As I mentioned in my recent blog post Angular 2 is a complete rewrite. Many concepts that are known from Angular 1.x are gone or changed dramatically. Angular 2 looks like a completely different framework: no DDO, no $scope, no angular.module and completely rewritten binding and change detection (no ng-model).Update (1/4/2016) - This blog post is not maintained. The concepts are still valid, but the code may be outdated. If you want to learn how to use up-to-date Angular2 with TypeScript and Gulp, please read this blog post (regularly updated): Quickstart: Angular2 with TypeScript and GulpAlthough Angular 2 is still work in progress many new features have already been revealed. In this blog post I will share some basic concepts of the framework via code samples that I presented during my lecture at InfoShare 2015.Please note the code is created with Angular 2 Alpha 26.Angular 2 is built with… TypeScript (1.5)TypeScript is a typed superset of JavaScript that compiles to plain JavaScrip…

Angular2 a gentle introduction

Image
Angular 1.xAngular 1.x is the most popular Front-end framework these days. Last year we decided to adopt it in some of Goyello new projects. Today we have some experience to share. At the same time we are closely looking at the next generation of the framework.Update (1/4/2016) - If you want to learn how to use up-to-date Angular2 with TypeScript and Gulp, please read my this blog post (regularly updated): Quickstart: Angular2 with TypeScript and GulpGame changerIt is extremely easy to get started with Angular 1.x. It provides clear separation of presentation, data and application logic which is greatly appreciated especially by back-end developers as myself. Two-way data binding is one of the most appreciated features of the framework. The dependency injection makes it possible to modularize the code and test it in isolation. Directives are great way of creating reusable components and use them across the application with ease. As a result, people love and admire the framework since …

Groovier Spring Boot Integration Testing

Recently I had a chance to use Groovy’s groovy.json.JsonSlurper in a soapUI REST project. The usage scenario in soapUI (in particular, soapUI Open Source) was very simple: in a Groovy assertion, load the content of a response into a variable and than verify it like this:import groovy.json.JsonSlurper; def slurper = new JsonSlurper() def result = slurper.parseText(messageExchange.responseContent) assert result.incidentType == 'Lorem ipsum'This works well in soapUI. But what about Spring Boot integration tests? Can I make integration testing of REST API groovier?

JUnit: Testing Exceptions with Java 8 and AssertJ 3.0.0

AssertJ 3.0.0 release for Java 8 makes testing exceptions much easier than before. In one of my previous blog post I described how to utilize plain Java 8 to achieve this, but with AssertJ 3.0.0 much of the code I created may be removed. Warning: this blog post contains mostly the code examples.

JAX-RS 2.x vs Spring MVC: Returning an XML representation of a list of objects

JSON is King as it goes to all kinds of REST* APIs, but still you may need to expose multiple representations, including XML. With both JAX-RS and Spring MVC this is very simple. Actually, the only thing to do is to annotate POJOs returned from the API call with JAXB annotation and that’s it. But when it goes to serializing a list of objects, JAX-RS will do a bit better than Spring MVC, in my opinion. Let’s see.

Jenkins HOW-TO: build a project from multiple (Git) repositories

Image
Recently I have been working on a project that has two main modules: Backend and Frontend. The Backend is a Spring Boot application and the Frontend is an AngularJS 1.3 application. While preparing the build we are using Grunt for building AngularJS code and Gradle for building the Spring Boot application. Grunt is creating a JAR file containing all resources and copies it to Spring Boot application. Then Gradle takes the JAR and adds it to the resulting WAR. Long story made short. Both Backend and Frontend are different Git repositories, initially managed by different teams. Both needs to be checked to the same root folder, so the building process will properly run. In order to do the it on Jenkins, Multiple SCMs Plugin Jenkins plugin can be used. The plugin simplifies the configuration of such a build.

Spring Boot Integration Testing with Selenium

Image
Web integration tests allow integration testing of Spring Boot application without any mocking. By using @WebIntegrationTest and @SpringApplicationConfiguration we can create tests that loads the application and listen on normal ports. This small addition to Spring Boot makes much easier to create integration tests with Selenium WebDriver.

Quick Start: Spring Boot and WildfFly 8.2 on OpenShift

A really “Quick Start” with Spring Boot, WildFly and OpenShift as opposed to my last, more descriptive article.

Openshift: Build Spring Boot application on Wildfly 8.2.0 with Java 8

Image
OpenShift DIY cartridge is a great way to test unsupported languages on OpenShift. But it is not scalable (you can vote for Scalable DIY cartridge here) which makes it hard to use with production grade Spring Boot applications. But what if we deployed Spring Boot application to WildFly Application Server? Spring Boot can run with embedded servlet container like Tomcat or much faster Undertow, but it also can be deployed to a standalone application server. This would mean that it can be also deployed to WildFly application server that is supported by OpenShift. Let’s see how easy is to get started with creating a Spring Boot application from scratch and deploy it to WildFly 8.2 on OpenShift.

OpenShift DIY: Build Spring Boot / Undertow application with Gradle

Image
Gradle 1.6 was the last supported Gradle version to run on OpenShift due to this bug. But as of Gradle 2.2 this is no more an issue, so running newest Gradle on OpenShift should not be a problem anymore with Do It Yourself cartridge. 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 1.2 and Java 8 running on Undertow, that is supported as a lightweight alternative to Tomcat. It should not take more than 10 minutes to get up and running.

Testing with files and directories in JUnit with @Rule

Image
Testing with Files and directories in JUnit is easy thanks to TemporaryFolder@Rule.
In JUnit rules (@Rule) can be used as an alternative or an addition to fixture setup and
cleanup methods (org.junit.Before, org.junit.After, org.junit.BeforeClass, and org.junit.AfterClass), but they are more powerful, and can be more easily shared between projects and classes.

Building a HATEOAS API with JAX-RS and Spring

Image
In my previous blog post I showed how easy Jersey can be configured with Spring Boot. My exploration on Spring Boot and Jersey did not end and I investigated the possibility of using Spring HATEOAS along with Jersey in Spring Boot application. Spring HATEOS allows creating REST representations that follow the HATEOAS principle and (as of writing this article) has basic JAX-RS support for working with links. In this blog post I will share some examples of how I integrated Spring HATEOAS with Jersey in a Spring Boot application.

Getting started with Jersey and Spring Boot

Image
Along many new features, Spring Boot 1.2 brings Jersey support. This is great step to attract those developers who like the standard approach as they can now build RESTful APIs using JAX-RS specification and easily deploy it to Tomcat or any other Spring’s Boot supported container. Jersey with Spring platform can play an important role in the development of mico services. In this article I will demonstrate how one can quickly build an application using Spring Boot (including: Spring Data, Spring Test, Spring Security) and Jersey.