0
0
Spring Bootframework~30 mins

Test profiles and configuration in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Test profiles and configuration
📖 Scenario: You are building a Spring Boot application that needs to behave differently in testing and production environments. You want to use Spring profiles to separate configuration for tests and for normal running.
🎯 Goal: Create a Spring Boot application with two profiles: test and default. Configure a property app.message differently for each profile. Write a simple test that uses the test profile and checks the message.
📋 What You'll Learn
Create an application property app.message with value Production Message in the default profile
Create a test profile property app.message with value Test Message
Write a Spring Boot test class that activates the test profile
Write a test method that asserts the app.message property value is Test Message
💡 Why This Matters
🌍 Real World
Many applications need different settings for testing, development, and production. Spring profiles help manage these differences cleanly.
💼 Career
Understanding Spring profiles and configuration is essential for backend developers working with Spring Boot to write maintainable and testable applications.
Progress0 / 4 steps
1
Create default application properties
Create a file named src/main/resources/application.properties and add the line app.message=Production Message to set the default message.
Spring Boot
Need a hint?

This file sets the default configuration for your Spring Boot app.

2
Create test profile properties
Create a file named src/test/resources/application-test.properties and add the line app.message=Test Message to set the message for the test profile.
Spring Boot
Need a hint?

This file overrides properties when the test profile is active.

3
Write a Spring Boot test class with test profile
Create a test class named MessageServiceTest in package com.example.demo. Annotate it with @SpringBootTest and @ActiveProfiles("test"). Inject Environment and write a test method testAppMessage() that asserts environment.getProperty("app.message") equals Test Message.
Spring Boot
Need a hint?

Use @ActiveProfiles("test") to activate the test profile during this test.

4
Add main application class
Create a main Spring Boot application class named DemoApplication in package com.example.demo with @SpringBootApplication annotation and a main method that runs SpringApplication.run(DemoApplication.class, args).
Spring Boot
Need a hint?

This is the entry point of your Spring Boot application.