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.
application.properties
api.mealinfo.mealNameLength=40
api.mealinfo.calorieLength=5
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?)
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.
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.
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