TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Spring Boot Starter Test

Spring Boot Starter Test with Introduction, Features, Project, Starter Project Wizard, CLI, Application, Annotations, DM, Properties, Actuator, Thymeleaf View, JPA, JDBC etc

<< Back to SPRING

Spring Boot Starter Test

The spring-boot-starter-test is the primary dependency for the test. It contains the majority of elements required for our tests.

There are several different types of tests that we can write to help test and automate the health of an application. Before starting any testing, we need to integrate the testing framework.

With Spring Boot, we need to add starter to our project, for testing we only need to add the spring-boot-starter-test dependency.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.2.2.RELEASE</version>
<scope>test</scope>
</dependency>

It pulls all the dependencies related to test. After adding it, we can build up a simple unit test. We can either create the Spring Boot project through IDE or generate it using Spring Initializr.

Note: If you are adding test dependency manually, add it to the bottom of the pom.xml file.

In the above dependency, one thing to be noticed that it includes the scope of test <scope>test</scope>. It means when the application is bundled and packaged for deployment, any dependency that is declared with the test scopes is ignored. The test scope dependencies are only available when running in the development and Maven test modes.

When we create a simple Spring Boot application, by default, it contains the test dependency in the pom.xml file and ApplicationNameTest.java file under in the folder src/test/java.

Let's create a simple maven project.

Spring Boot Starter Test Example

Step 1: Open Spring Initializr https://start.spring.io/.

Step 2: Provide the Group name and Artifact Id. We have provided Group name com.TheDeveloperBlog and Artifact spring-boot-test-example.

Step 3: Add the Spring Web dependency.

Spring Boot Starter Test

Step 4: Click on the Generate button. When we click on the Generate button, it wraps all the specifications related to the project and downloads a Jar file to our local system.

Step 5: Extract the downloaded Jar file.

Step 6: Import the folder to STS. It takes some time to import.

File -> Import -> Existing Maven Projects -> Browse -> Select the folder spring-boot-test-example -> Finish

After importing the project, we can see the following project directory in the Package Explorer section of the STS.

Spring Boot Starter Test

We can see in the above directory that it contains a test file named SpringBootTestExampleApplicationTest.java in the folder src/test/java.

SpringBootTestExampleApplicationTest.java

package com.TheDeveloperBlog.springboottestexample;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBootTestExampleApplicationTests 
{
@Test
void contextLoads() 
{
}
}

The above code implements two annotation by default: @SpringBootTest, and @Test.

  • @SpringBootTest: It applies on a Test Class that runs Spring Boot based tests. It provides the following features over and above the regular Spring TestContext Framework:
    • It uses SpringBootContextLoader as the default ContextLoader if no specific @ContextConfiguration(loader=...) is defined.
    • It automatically searches for a @SpringBootConfiguration when nested @Configuartion is not used, and no explicit classes are specified.
    • It provides support for different WebEnvironment modes.
    • It registers a TestRestTemplate or WebTestClient bean for use in web tests that are using the webserver.
    • It allows application arguments to be defined using the args attribute.

Step 7: Open the SpringBootTestExampleApplicationTest.java file and run it as Junit Test.

Spring Boot Starter Test

When we run the above code, it displays the following:

Spring Boot Starter Test





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf