How to test a class that handles application.properties with SpringBoot (request: pointed out)

I wanted to test the confirmation of the setting value of application.properties

It's just a test to check if the settings in application.properties are bound, but I'm having trouble creating a test class.

Transcribe it as a memo for reference.

Settings in the target application.properties

application.properties



api.mealinfo.mealNameLength=40
api.mealinfo.calorieLength=5

Class that handles the above settings

APIMealInfoProperties.java


@Component
@ConfigurationProperties(prefix = "api.mealinfo")
public class APIMealInfoProperties {
	
	private int mealNameLength;
	private int calorieLength;

	public int getMealNameLength() {
		return mealNameLength;
	}

	public void setMealNameLength(int mealNameLength) {
		this.mealNameLength = mealNameLength;
	}

	public int getCalorieLength() {
		return calorieLength;
	}

	public void setCalorieLength(int calorieLength) {
		this.calorieLength = calorieLength;
	}
}

As in the example, register it in the bean with @Component so that the setting value is bound to the variable with @ConfigurationProperties. (Since there is a setter in the class with @ConfigurationProperties, variables can be replaced, but is it okay as a class that handles setting values?)

Test class of the above class (bad pattern)

ApplicateionPropertiesTest.java


@SpringBootTest
@ContextConfiguration(classes = APIMealInfoProperties.class)
class ApplicateionPropertiesTest {

	//Test target
	@Autowired
	APIMealInfoProperties aPIMealInfoProperties;
	
	@Test	
public void Is the set value returned?() {
		int expected = 40;
		assertEquals(expected, aPIMealInfoProperties.getMealNameLength()); ///org.opentest4j.AssertionFailedError: expected: <40> but was: <0>
		
		expected = 5;
		assertEquals(expected, aPIMealInfoProperties.getCalorieLength());
		
	}
}

Even if I ran the test with JUnit, ** aPIMealInfoProperties.getMealNameLength () ** returned "0", so it failed.

The setting value is not bound to the instance variable of ** APIMealInfoProperties ** that is @Autowired. (The default value of an int type instance variable is 0) In other words, ** APIMealInfoProperties ** may be missing the process of reading ** application.properties ** and binding it to a variable.

solution

I managed to solve it by referring to various articles while fighting with Google teacher.

ApplicateionPropertiesTest.java


@SpringBootTest
@ContextConfiguration(classes = ApplicateionPropertiesTest.class)・ ・ ・ ①
@EnableConfigurationProperties(APIMealInfoProperties.class)・ ・ ・ ②
class ApplicateionPropertiesTest {

	//Test target
	@Autowired
	APIMealInfoProperties aPIMealInfoProperties;
	
	@Test	
public void Is the set value returned?() {
		int expected = 40;
		assertEquals(expected, aPIMealInfoProperties.getMealNameLength());
		
		expected = 5;
		assertEquals(expected, aPIMealInfoProperties.getCalorieLength());
		
	}
}

① ・ ・ ・ Modified to specify your own class with @ContextConfiguration ② ・ ・ ・ Specify the test target class (APIMealInfoProperties in this case) with @EnableConfigurationProperties

However, although the test was OK, the reason is not well understood. .. .. I would like to ask you to teach.

Summary

Give the test class the appropriate settings for @ContextConfiguration and @EnableConfigurationProperties.

I won the game, but I still feel like I lost the game. (It may be subtle if you win the game ...) It's new to SpringBoot, but it's convenient, but it's been danced by annotations. I want to dance with you soon.

Recommended Posts

How to test a class that handles application.properties with SpringBoot (request: pointed out)
How to create a class that inherits class information
How to get resource files out with spring-boot
A memo that handles a class created independently with ArrayList
How to request a CSV file as JSON with jMeter
How to test a private method with RSpec for yourself
Mapping to a class with a value object in How to MyBatis
How to quickly create a reverse proxy that supports HTTPS with Docker
How to find out the Java version of a compiled class file
How to test private scope with JUnit
How to convert a solidity contract to a Java contract class
How to write test code with Basic authentication
(Java) How to implement equals () for a class with value elements added by inheritance
How to make a factory with a model with polymorphic association
How to run the SpringBoot app as a service
How to write a unit test for Spring Boot 2
How to unit test with JVM with source using RxAndroid
How to delete a new_record object built with Rails
How to deal with No template for interactive request
How to manually generate a JWT with Rails Knock
[Java] How to get a request by HTTP communication
How to efficiently carry out automatic test browser startup
[Java] How to cut out a character string character by character
How to get started with creating a Rails app
[Java] How to start a new line with StringBuilder
Let's find out how to receive in Request Body with REST API of Spring Boot
How to take a screenshot with the Android Studio emulator
Let's write how to make API with SpringBoot + Docker from 0
I wrote a CRUD test with SpringBoot + MyBatis + DBUnit (Part 1)
How to divide a two-dimensional array into four with ruby
How to use a foreign key with FactoryBot ~ Another solution
How to run only specific files with gem's rake test
Learning Ruby with AtCoder 13 How to make a two-dimensional array
[Java] How to use substring to cut out a character string
How to mock some methods of the class under test
How to erase test image after running Rspec test with CarrierWave
Test a class that takes a Context and reads / writes a file
How to set up a proxy with authentication in Feign
How to register as a customer with Square using Tomcat
How to deal with the event that Committee :: InvalidRequest occurs in committee during Rspec file upload test
How to deal with the type that I thought about writing a Java program for 2 years