spring-boot - Springboot ConstraintValidator not reading configurationProperties with MockitoJUnitRunner or SpringBoot
问题描述
I have written a ConstraintValidator which will read keyvalue pair from yaml file. Th code works fine during runtime but not working while testing. I tried with both MockitoJUnitRunner and SpringRunner classes.
I have used the same logic in CustomConverters implementation which is able to read the keyvalue pair during runtime and testing.
Could you please advise how to test ConstraintValidator with configurationproperties.
Code:
@ConfigurationProperties(prefix="gender")
public class GenderValidator implements ConstraintValidator<Gender, String> {
private Map<String, String> type = new HashMap<String, String>();
@Override
public void initialize(Gender gender) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext ctx) {
return getType().containsKey(value);
}
public Map<String, String> getType() {return type; }
}
Configuration
gender:
type:
M: male
F: female
TestCode:
Set<ConstraintViolation<GenderDetails>> constraintViolations =
validator.validate(payload);
assertThat (constraintViolations.isEmpty(), is(true) );
AssertThat always returns error.
Note: I have configured the PropertySourcesPlaceholderConfigurer under configuration class which is working fine when I test the CustomConverter.
Thanks for your help.`
解决方案
推荐阅读
- linux - Fluent Bit 不在文件系统上保存任何数据
- python - 将 python 脚本转换为气流 dag
- ios - AVCaptureVideoPreviewLayer 与相机纵横比的大小容器
- python - 如何通过 Python 将 blob 数据移动到 Snowflake
- flutter - Flutter 2.0 pushAndRemoveUntil 不工作
- c# - 在 .NET Core Web API 中运行并行异步任务并返回结果
- c# - 如何通过更改 datagridview 选定索引行中的值来更新列表?
- docker - 如何解决有关无法计算缓存键的Docker错误:“/requirements.txt?
- python - 如何按顺序从 Dataframe 连接中插入删除的连接键列
- maven - Ubuntu 20.04 中的 Maven Spark 源代码构建失败