EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Test Configuration
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring Boot Test Configuration

Spring Boot Test Configuration

Definition of Spring Boot Test Configuration

Spring boot test configuration is useful to test our spring boot application, using test configuration we can test the small part of our application. Spring boot configuration test is making the verification test is easy and fast, so the scope of our testing is narrowed into class methods. Spring boot @TestConfiguration classes are used only on selective test classes which explicitly imported the @import notation.

Overview of Spring Boot Test Configuration

  • In spring boot testing @TestConfiguration is the specialized form of @configuration which was used to define additional beans or test our spring boot application.
  • In the spring boot application, we can configure spring beans in top of class which was annotated with @TestConfiguration.
  • We need to explicitly register @TestConfiguration class using the project class which contains the test cases.
  • We can use different types of annotation to test the spring boot application.
  • Import annotation is indicating more than one configuration which was used to import the same into our application context file or spring boot context file.
  • We can use @TestConfiguration method to modify the context of the spring boot application during the runtime of the test.
  • We can also use @TestConfiguration method and bean definition. Example is to replace the original bean with fake bean and we can also change the bean configuration to make the bean stable for testing purposes.
  • The @TestConfiguration is an inherited property of @Configuration beans. @configuration is the parent annotation of @TestConfiguration bean.
  • While developing the project we need to add the dependency of the spring boot starter test.
  • The annotation of @TestConfiguration is excluded from component scanning of spring boot.
  • We can also exclude the configuration classes which was annotated by @TestConfiguration from component scanning.
  • We need to explicitly import annotation of @TestConfiguration in every testing when we need to automate them.
  • We can also annotate the @TestConfiguration annotation as @TestComponent.

Two test configuration additional

  • In spring boot basically, there are two ways to include the additional test in test configuration are as follows.

1) @Import annotation
2) Static nested classes

1) @Import annotation –

  • This annotation will indicate one or more configuration classes which was import into our application context file or spring test context file.
  • @Bean annotation is declared in a @TestConfiguration class which was accessed by using injection of @Autowired.
  • @import annotation bean is itself autowired or it will autowired at the time of declaring configuration class.
  • We can declare the @import annotation by using meta-annotation or by using class level.

Example

@TestConfiguration
Public class testconfiguration
{
@Bean
DataSource createDataSource () {
}
}

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

2) Static nested classes –

  • We can also define test configuration in nested classes inside into class of test. Nested classes are annotated by using @TestConfiguration and @configuration.
  • If the declared class is annotated by using @TestConfiguration it is known as a test class within a nested class. This class is registered automatically we have no need to register it again.

Example

@SpringBootTest (webEnv = webEnv.PORT)
public class testconfiguration
{
@ServerPort
int Port;
@Autowired
DataSource dsource;
@TestConfiguration
static class TestConfiguration {
@Bean
DataSource createDataSource() {
}
}
}

Code Example

Below is the code example of spring boot test configuration are as follows.

1) Create project template using spring initializer and give name to project –

In the below step, we have provided project group name as com. example, artifact name as springboottestconfiguration, project name as springboottestconfiguration, and selected java version as 8.

Group – com.example Artifact name – springboottestconfiguration
Name – springboottestconfiguration Spring boot – 2.6.0
Project – Maven project Description - Project for springboottestconfiguration
Java – 8 Dependencies – spring web, spring native
Package name - com.example.springboottestconfiguration

Spring Boot Test Configuration 22

2) After generating project extract files and open this project by using spring tool suite –

After generating the project by using spring initializer in this step we are extracting the jar file and opening the project by using the spring tool suite.

Spring Boot Test Configuration 23

3) After opening project using spring tool suite check the project and its files –

  • In this step, we are checking all the project template files. We also need to check the maven dependencies and system libraries.

Spring Boot Test Configuration 24

4) Add dependency packages –

  • In this step, we are adding the required dependency to our project.

Code:

<dependency> -- Start of dependency tag.
<groupId>org.springframework.boot</groupId> -- Start and end of groupId tag.
<artifactId>spring-boot-starter-test</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.

Spring Boot Test Configuration 26

5) Configure application. properties file –

  • In this step, we have configuring the application.properties file.

Code:

spring.main.allow-bean-definition-overriding = true

27

6) Test configuration by using static inner class –

In this example, we are using a static inner class to use test configuration. We can say that static inner class is a class-level annotation that allows in the static inner class itself.

Code:

@SpringBootTest
public class testconfiguration
{
@TestConfiguration
public static class ClientConfiguration {
@Bean
public client getWebClient(final Builder build) {
return build.baseUrl ("http://localhost")).build();
}
@Autowired
private DataService dService;
}
}

28

7) Run and test the application –

29

30

Example Project

The below example shows an example project of spring boot test configuration. We are using the template name springboottestconfiguration.

1) Use the @TestConfiguration annotation in our project –

Code:

@TestConfiguration
public class TestConfig
{
@Bean
public HService hService (){
return new Hservice () {
public String getMessage (String stud_name) {
return "Test configuration: "+stud_name;
}
};
}
}

31

2) Import test configuration in class –

Code:

@SpringBootTest
@Import (TestConfig.class)
public class ProcTest {
@Autowired
private ProcTest pTest;
@Test
public void testSayHi () {
String message = ProcTest.studName ("ABC");
System.out.println (message);
Assert.assertEquals(message, "Test configuration: ABC");
}
private static String studName (String string) {
return null;
}
}

32

3) Run and test the application –

run

run 1

Conclusion – Spring Boot Test Configuration

In spring boot there are two ways to include additional test in test configuration first one is @import annotation and the second is static nested classes. It is useful to test our spring boot application, using test configuration we can test a small part of our application.

Recommended Articles

This is a guide to Spring Boot Test Configuration. Here we discuss definition, overviews, Two test configuration additional, examples with code implementation. You may also have a look at the following articles to learn more –

  1. Spring Boot Docker
  2. Spring Boot Advantages
  3. Spring boot logging level
  4. Spring boot filter
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more