It’s well known that auto-configuration is one of the key features in Spring Boot, but testing auto-configuration scenarios can be tricky.
In the following sections, we’ll show how ApplicationContextRunner simplifies auto-configuration testing.
ApplicationContextRunner is a utility class which runs the ApplicationContext and provides AssertJ style assertions. It’s best used as a field in test class for shared configuration and we make customizations in each test afterward:
Let’s move on to show its magic by testing a few cases.
In this section, we’re going to test some auto-configuration classes which use @ConditionalOnClass and @ConditionalOnMissingClass annotations:
We’d like to test…
As of Spring Boot 1.3, we’re able to use the EnvironmentPostProcessor to customize the application’s Environment before application context is refreshed.
In this tutorial, let’s take a look at how to load and transform the custom properties into the Environment, and then access those properties.
The Environment abstraction in Spring represents the environment in which the current application is running. In the meanwhile, it tends to unify the ways to access properties in a variety of property sources, such as properties files, JVM system properties, system environment variables, and servlet context parameters.
In Java, the garbage collector automatically deletes the unused objects to free up the memory. Developers have no need to mark the objects for deletion, which is error-prone and vulnerable to the memory leak. So it’s sensible Java has no destructors available.
In case the objects hold open sockets, open files, or database connections, the garbage collector is not able to reclaim those resources. We can release the resources in close method and use try-finally syntax to call the method afterward before Java 7, such as the I/O classes FileInputStream and FileOutputStream. As of Java 7, we can implement interface…
In this article, we’re going to walk through the differences between BufferedReader, Console, and Scanner classes in Java. To have a deep dive on each topic, we suggest having a look at our individual articles on Java Scanner, Console I/O in Java, and BufferedReader.
Given the underlying stream passed to the constructors, both BufferedReader and Scanner classes are able to handle a wider range of user input, such as a string, file, system console (which is typically connected to…
Spring Boot is built on top of Spring framework. With zero or minimum customization, it significantly reduces the efforts to develop Spring-based and production-grade applications by doing the heavy lifting of auto-configuration and dependency management under the hood. As a result, developers are able to genuinely focus on business-centric features.
This story is going to demonstrate how we build the RESTful services with Spring Boot in few steps. We will create a simple customer service to CRUD(a.k.a. create, read, update, delete) the customer records and banking accounts each customer has.
Have you ever come across the tangled up code, which mixes the business logic with UI presentation or another way around in N-tier architecture? Have you ever found yourself in trouble with testing the business logic in isolation from run-time devices and database? Fortunately, Hexagonal Architecture (a.k.a. Ports & Adapters Architecture, this name will be used hereafter through this tutorial) comes to rescue.
Now, let’s unfold the journey of Ports & Adapters with an example. We are building a customer loyalty application which is able to register a customer, upgrade and downgrade a customer status.
We are going to have…
Optional class was introduced in JDK 8 to represent the presence or absence of value wrapped inside. while knowing it probably saves developers from annoying NullPointerException
, are you aware of what Optional
class was intended for when added in JDK 8 and in what situation its use not recommended?
Optional
is primarily intended for use as a method return type where there is a clear need to represent "no result," and where usingnull
is likely to cause errors. A variable whose type isOptional
should never itself benull
; it should always point to anOptional
instance. …
What changes will happen when Generics comes to live with array? Let's find out together.
The limitation over generic array creation
With JDK 5 or newer version, when you write the following code snippet in your Java IDE, the error on compile time will be immediately highlighted
List<Integer>[] arrayOfIntegerList = new List<Integer>[2]; // compile-time error
The error shows generic array of List<Integer>
can’t be created.
Why the compile-error happens
To understand this, we need to know Array in Java is covarient which means T[] is the subtype of S[] whenever T is subtype of S. …
Living in Melbourne Australia. More than 10 years experience in Java development. Find me @ www.linkedin.com/in/lihui-matt-zhang-48408a37/