Spring Boot Conditionals can Step Up Your Spring Application

spring boot applications

There are several kinds of Spring Framework available in the market. Among them, Spring Boot is a kind of Spring Framework module which gives the user an option for Rapid Application Development (RAD) and also to the framework.

Spring Boot tends to depend on the “Starter Templates”. These templates are very powerful and function flawlessly.

Starter Template is basically a collection of relevant transitive dependencies. It is needed when the user is going to start a particular functionality.

Benefits of Spring Boot

I hope now you got a clear idea regarding Spring Boot. In addition, it has been very beneficial in various aspects. To know more about it, let’s take a look into its benefits. Well, Spring Boot can benefit the user in several ways.

  • Firstly, you can work hassle-free while creating spring-powered production-grade applications using spring boot.
  • Spring Boot can be used to create stand-alone Java applications. Now, these Java applications are basically launched by using java -jar.
  • In spring boot there is no need to deploy WAR files while embedding Tomcat, Jetty or Undertow.

Testing the Spring Boot Conditionals

Spring Boot 1.5.x is widely among the Spring Framework users. But here I am going to use the 1.5.x version with Spring Boot 2.1.x version and will also include both JUnit 4.x and JUnit 5.x. 

Some of the regular or non-regular users of Spring Boot may face a situation where a particular bean or condition statement has to be applied conditionally. Sometimes testing these situations can get real messy. 

The strategies I am going to talk about today is applicable to both regular configuration or auto-configuration. Also, discussing some possible ways to tackle the conditions the user may face while working with Spring Boot.

Today will be using homemade logging as our example to show the Spring Boot conditions. Let us consider that a bean is assigned with the name of a dedicated logger as per the requirement of the Spring Boot. 

In some cases, the assigned logger has to be disabled to let the logging. Also, the enabled command works as a switch. Though Slf5j and Logback are not really that important, still I am going to use both of them in this example.

Example 1:

@Config

Public class LogConfig {

   @Config

   @ConditionalOnProperty(name = “log.enabled”, matchIfMissing = true)

   public static class Slf5jConfig {

       @Bean

       Log log() {

           return LogFactory.getLog(“sample”);

       }

   }

   @Bean

   @ConditionalOnMissingBean

   Log log() {

       return new NOPLogFactory().getLog(“sample”);

   }

}

An outstanding test scaffolding support is always offered by Spring Boot. To test the above example, the @SpringBootTest, and @TestProperlySource commands, allow the user to bootstrap the application by providing several customized properties.

However, there remains only one issue. In the command, they are applied as per test class level but not as per the test method. Here, you will need to create a test class as per the conditions.

Some of the users still use the JUnit 4.x. However to accomplish the Enclosed runner you might be able to use anyone tactic.

Example 2:

@RunWith(Enclosed.class)

public class LogConfigTest {

   @RunWith(SpringRunner.class)

   @SpringBootTest

   public static class LogEnabledTest {

       @Autowired private Log log;

       @Test

       public void logShouldBeSlf5j() {

           assertThat(log).isInstanceOf(ch.qos.logback.classic.Log.class);

       }

   }

   @RunWith(SpringRunner.class)

   @SpringBootTest

   @TestPropertySource(properties = “logging.enabled=false”)

   public static class LogDisabledTest {

       @Autowired private Log log;

       @Test

       public void logShouldBeNoop() {

           assertThat(log).isSameAs(NOPLog.NOP_LOG);

       }

   }

}

Here you still have the class per condition statement but the positive point is that they all lie under the same command now. JUnit 5.x makes things a lot easier for the user but unfortunately, JUnit 5.x is not compatible with Spring Boot 1.5.x. 

Therefore, the users depend on the extension which is provided by spring-test-junit5. In the bellow example, you may notice that JUnit is excluded from the dependence graph of spring-boot-starter-test.

Example 3:

<dependence>

   <groupsId>org.springframework.boot</groupsId>

   <artifactsId>spring-boot-starter-test</artifactsId>

   <scope>test</scope>

   <exclusions>

       <exclusion>

           <groupsId>junit</groupsId>

           <artifactsId>junit</artifactsId>

       </exclusion>

   </exclusions>

</dependence>

<dependence>

   <groupsId>com.github.sbrannen</groupsId>

   <artifactsId>spring-test-junit5</artifactsId>

   <version>1.5.0</version>

   <scope>test</scope>

</dependence>

<dependence>

   <groupsId>org.junit.jupiter</groupsId>

   <artifactsId>junit-jupiter-api</artifactsId>

   <version>5.5.0</version>

   <scope>test</scope>

</dependence>

<dependence>

   <groupsId>org.junit.jupiter</groupsId>

   <artifactsId>junit-jupiter-engine</artifactsId>

   <version>5.5.0</version>

   <scope>test</scope>

</dependence>

In Apache Maven and Maven Surefire plugin, all the nested classes are excluded and that’s why the user might get surprised that none of the commands were executed during the build. Therefore, for these kinds of conditions, the user needs to use another workaround.

Example 4: 

<plugin>

   <groupsId>org.apache.maven.plugins</groupsId>

   <artifactsId>maven-surefire-plugin</artifactsId>

   <version>2.22.2</version>

   <config>

       <excludes>

           <exclude />

       </excludes>

   </config>

</plugin>

With the Spring Boot 1.5.x, things can run smoothly. However, Spring Boot 2.1x is the real deal. The set of context runners, ApplicationContexRunner, ReactiveWebApplicationContextRunner, and WebApplicationContextRunner gives a comfortable and easy way to edit the context keeping the executions very fast.

Example 5:

public class LogConfigTest {

  private final ApplicationContextRunner runner = new ApplicationContextRunner().withConfig(UserConfig.of(LogConfig.class));

   @Test

   public void loggerShouldBeSlf5j() {

       runner

           .run(ctx ->

               assertThat(ctx.getBean(Log.class)).isInstanceOf(Log.class)

           );

   }

   @Test

   public void logShouldBeNoop() {

       runner

           .withPropertyValues(“log.enabled=false”)

           .run(ctx ->

               assertThat(ctx.getBean(Log.class)).isSameAs(NOPLog.NOP_LOG)

           );

   }

}

JUnit 5.x is the default engine for Spring Boot 2.1.x and will be there for the upcoming 2.2 version. But do not worry as JUnit 4.x will be compatible as well.  

Example 6:

<dependence>

   <groupsId>org.springframework.boot</groupsId>

   <artifactsId>spring-boot-starter-test</artifactsId>

   <scope>test</scope>

   <exclusions>

       <exclusion>

           <groupsId>junit</groupsId>

           <artifactsId>junit</artifactsId>

       </exclusion>

   </exclusions>

</dependence>

<dependence>

   <groupsId>org.junit.jupiter</groupsId>

   <artifactsId>junit-jupiter-api</artifactsId>

   <scope>test</scope>

</dependence>

<dependence>

   <groupsId>org.junit.jupiter</groupsId>

   <artifactsId>junit-jupiter-engine</artifactsId>

   <scope>test</scope>

</dependence>

The above shown configuration is quite simple and easy to execute. Although some real-world applications might be more complex. 

Spring Boot Applications

There are distinct ways to create a spring boot application. Here in this section, the creation of an application is shown using STS ( Spring Tool Suite) IDE. For better information, make sure to follow the steps mentioned below:

Step1: First of all, make a Maven Project by choosing the spring starter project wizard.

Step 2: Select the project type and set a project name.

Step 3: After that, check for accessible options and select Spring Boot version and project dependencies.

Step 4: In this step make sure to make a java class file so that you can create a controller. 

Step 5: Now after that in the next step, provide both the package as well as the controller name.

Step 6: After that, you have to create a method inside the controller. This will be later called during an HTTP request.

Step 7: In this step, you need to execute SpringBootExampleApplication.java as a Java application.

To Conclude

As from the above topic, you can obtain a sane way to test Spring Boot Conditionals and a stepwise guide to create a spring boot application. I had also discussed various platform versions of Spring Boot that are currently running.

Including that, it can rather create stand-alone Spring applications, and provide “starter” dependencies to ease the user’s build configuration. However, it does not require any code generation and XML configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *

Next Post

Java Collections Framework: A Basic Overview

Thu Aug 29 , 2019
The Collection is a framework in Java that enables architecture to manipulate and store a group of objects. Java Collection can do all those operations120 and help to achieve data execution, like sorting, manipulation, searching, insertion, and deletion. The Collection framework offers a variety of interfaces (List, Set, Deque, Queue), […]