This time I will show you how create a unit testing using Spock, Spock is a testing and specification framework for Java and Groovy applications it is highly expressive and easy to use. First you need to add Spock dependency in your build.gradle
testCompile 'org.spockframework:spock-spring' This is the class we are going to test:
package com.jos.dem.vetlog.validator import org.springframework.validation.Validator import org.springframework.validation.Errors import org.springframework.stereotype.Component import org.springframework.beans.factory.annotation.Autowired import com.jos.dem.vetlog.service.LocaleService import com.jos.dem.vetlog.command.UserCommand @Component class UserValidator implements Validator { @Autowired LocaleService localeService @Override boolean supports(Class<?
[Read More]
Spring Boot Validation
Spring has a Validation interface that we can use to create custom object validation; if some errors occur in the validation process, it stores them in a BindingResult object so you can test for and retrieve validation errors. Let’s consider the following validator implementation:
package com.josdem.springboot.validation.validator; import com.josdem.springboot.validation.command.PersonCommand; import org.springframework.validation.Validator; import org.springframework.validation.Errors; import org.springframework.stereotype.Component; @Component public class PersonValidator implements Validator { private final String REGEX = "[0-9]+"; @Override public boolean supports(Class<?
[Read More]
Spring Boot Bootstrap
Boostrap was originally created by a designer and a developer at Twitter, Bootstrap has become one of the most popular front-end frameworks and open source projects in the world. In this technical post I will show you how to add Bootstrap to your Spring Boot project. In order to get the setup for this example, please refer my previous posts Spring Boot JPA and Spring Boot Externalization. First you need to add Bootstrap dependency to your project, the way you should do it is add it as a Bower plugin to your build.
[Read More]
Spring Boot Testing
We can test our application using Junit or Spock, and we will see both. Also you can make unit, integration and functional testing.
Integration testing Spring Boot provides a @SpringApplicationConfiguration annotation as an alternative to the standard @ContextConfiguration annotation.
package com.jos.dem.jmailer import static org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith import org.springframework.boot.test.SpringApplicationConfiguration import org.springframework.test.context.junit4.SpringJUnit4ClassRunner import org.springframework.beans.factory.annotation.Autowired import org.springframework.mail.javamail.JavaMailSenderImpl @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = JmailerApplication.class) class JmailerApplicationTests { @Autowired JavaMailSenderImpl javaMailSender @Test void shouldLoadContext() { assertEquals('smtp.
[Read More]
Spring Boot Externalization
In this example we are going to externalize MySQL database connection using a yaml file. In order to get the setup for this example, please refer my previous post Spring Boot JPA
First we need to create our yml file in ${USER_HOME}/.spring-boot-jpa/application-development.yaml
spring: datasource: url: jdbc:mysql://localhost/spring_boot_jpa username: username password: password driverClassName: com.mysql.jdbc.Driver jpa: generateDdl: true Next we need to add bootRun closure and specify a system properties in our build.gradle file
[Read More]
Spring Boot JPA
The key goal of Spring Data repository abstraction is to significantly reduce the amount of code required to implement data access layers for various persistence stores. In this example I will show you how to create a JPA repository to save and retrieve all Person model. NOTE: If you need to know what tools you need to have installed in yout computer in order to create a Spring Boot basic project, please refer my previous post: Spring Boot
[Read More]
Spring Boot Internationalization
In this technical post, we will see how to manage different languages at your Spring Boot application aka. internationalization. NOTE: If you need to know what tools you need to have installed in your computer in order to create a Spring Boot basic project, please refer my previous post: Spring Boot. Spring Boot uses MessageSource configured with a MessageSourceAutoConfiguration. These settings can be easily changed in the application.properties file:
spring.messages.basename=i18n/messages Once we create that line, you can define your application messages in the file resources/i18n/messages.
[Read More]
Spring Boot Handler Exception
There are several reasons why we should manage exceptions such as:
Customize an error page Business logic Reduce logic The common way to manage exceptions in Spring is using an HandlerExceptionResolver class, in this example I will show you how to manage an generic exception in our code.
package com.jos.dem.jmailer.controller import org.springframework.web.servlet.HandlerExceptionResolver import org.springframework.http.HttpStatus import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse import org.springframework.stereotype.Component import org.springframework.http.ResponseEntity import org.springframework.web.servlet.ModelAndView import org.apache.commons.logging.Log import org.apache.commons.logging.LogFactory @Component class HandlerException implements HandlerExceptionResolver { Log log = LogFactory.
[Read More]
Spring Boot RESTFul
The main idea behind creating a RESTful application is that you will create logical resources (GET, POST, UPDATE and DELETE) consumed by clients usign HTTP request.
This resources should be nouns no verbs. In this example we will create a RESTful application using a person resource.
So we can have:
GET /persons - Retrieves a list of persons GET /persons/1234 - Retrieves a specific person POST /persons - Creates a new person PUT /persons/1234 - Updates #1234 person DELETE /persons/1234 - Deletes #1234 person NOTE: If you need to know what tools you need to have installed in your computer in order to create a Spring Boot basic project, please refer my previous post: Spring Boot
[Read More]
Spring Boot
What is Spring Boot? Open source Java framework used to create micro services but also to create easy web or stand-alone applications. It provides the next features:
No XML configuration is required Netty (Starter Webflux) or Tomcat (Starter Web) Convention over configuration Easy setup Very lightweight Easy to test Topics Spring Boot AOP Spring Boot RESTful Spring Boot Handler Exception Spring Boot Internationalization Spring Boot JPA Spring Boot Externalization Spring Boot Bootstrap Spring Boot Testing Spring Boot Validation Spring Boot Testing Validation Spring Boot Security Spring Boot Security using Database Spring Boot Rest Client Spring Boot Actuator Spring Boot JDBC Template Spring Boot Flyway Spring Boot Liquibase Spring Boot Logback Spring Boot Thymeleaf Layouts Spring Boot Swagger Spring Boot Oauth2 with Google Spring Boot Ehcache Spring Boot Appium Cucumber Spring Boot Parameters Spring Boot Profiles Spring Boot Retrofit2 Cucumber & Junit5 Spring Boot Testing Web Layer Spring Boot Publishing an Artifactory Library Spring Webflux Basics Spring Webflux Server Spring Webflux Client Spring Webflux Security Spring Webflux Security Database Spring Webflux with Thymeleaf Spring Boot JMS Spring Webflux Cucumber Spring Boot WebClient Spring Boot WebClient Cucumber and Junit5 Spring Webflux Multi-Module Spring Weblux Internationalization Spring Weblux Testing Web Layer Spring Boot Hazelcast Spring Boot JsonNode Spring Boot Server-sent Event Spring Boot Server-sent Event Client Spring Boot H2 Spring Webflux URI Validator Spring Webflux Webclient Headers Spring Boot XML Schema Spring Webflux JAXB Spring Webflux Constructor Injection Spring Webflux WebSockets This post shows you how to create a simple Spring Boot project with this features:
[Read More]