I'm facing a weird issue in spring boot test case. I've tried everything which I could find but no luck. Can somebody help me.
When I run the spring boot app everything works fine. But test case doesn't load partnerConfig properties (It loads other properties but not partnerConfig). If you have any hint please ping me.
config:
MH:
partner.id: "441"
tiny.url.domain: "http://mh.local.upgrd.co/"
EY:
partner.id: "11"
tiny.url.domain: "http://ey.local.upgrd.co/"
@Component
@EnableConfigurationProperties
@ConfigurationProperties()
public class PartnerConfig {
private Map<String, Map<String, String>> config = new HashMap<>();
}
@EnableAutoConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {MainMethodRunner.class})
public class PartnerConfigTestIT {
@Autowired
PartnerConfig partnerConfig;
@Test
public void should_get_carrierCode_from_partnerId() {
//partnerConfig.config is not available in tests
}
}
When I run the spring boot app everything works fine. But test case doesn't load partnerConfig properties (It loads other properties but not partnerConfig). If you have any hint please ping me.
答案 0 :(得分:0)
为HashMap添加getter和setter传递了测试
答案 1 :(得分:0)
这最终对我有用:
yaml文件
dynamodb:
endpoint: https://localhost:4311
accesskey: dummy
secretkey: dummy
tinyurl:
key.length: 12
partnerConfig:
DH:
partner.id: "056"
tiny.url.domain: "dh.test.upgrd.co"
HY:
partner.id: "2001"
tiny.url.domain: "hy.test.upgrd.co"
我想要一个动态映射parterConfig属性的类(没有使用@Value硬编码键)因为我想要能够添加更多partnerConfigs的功能,而且不需要更改代码。
将属性映射到类
的PartnerConfig类@Component
@EnableConfigurationProperties
@ConfigurationProperties
public class PartnerConfig {
private static final String TINY_URL_DOMAIN = "tiny.url.domain";
private static final String PARTNER_ID = "partner.id";
private Map<String, Map<String, String>> partnerConfig = new HashMap<>();
private BiMap<String, String> partnerIdToTinyUrlDomain = HashBiMap.create();
@PostConstruct
private void initialize() {
partnerConfig.forEach((carrierCode, partnerConf) ->
partnerIdToTinyUrlDomain.put(partnerConf.get(PARTNER_ID), partnerConf.get(TINY_URL_DOMAIN)));
}
public String getPartnerId(String tinyUrlDomain) {
if (isEmpty(tinyUrlDomain))
throwTinyUrlNotFoundError(tinyUrlDomain);
String partnerId = partnerIdToTinyUrlDomain.inverse().get(tinyUrlDomain);
if (isEmpty(partnerId))
throwTinyUrlNotFoundError(tinyUrlDomain);
return partnerId;
}
public String getTinyUrlDomain(String partnerId) {
if (isEmpty(partnerId))
throwPartnerNotFoundError(partnerId);
String tinyUrlDomain = partnerIdToTinyUrlDomain.get(partnerId);
if (isEmpty(tinyUrlDomain))
throwPartnerNotFoundError(partnerId);
return tinyUrlDomain;
}
public void setPartnerConfig(Map<String, Map<String, String>> partnerConfig) {
this.partnerConfig = partnerConfig;
}
public Map<String, Map<String, String>> getPartnerConfig() {
return partnerConfig;
}
private String throwTinyUrlNotFoundError(String tinyUrlDomain) {
String msg = String.format("tiny url mapping not found for domain %s", tinyUrlDomain);
throw new TinyUrlNotFoundException(msg);
}
private String throwPartnerNotFoundError(String partnerId) {
String msg = String.format("partner %s is not configured", partnerId);
throw new PartnerConfigurationException(msg);
}
}
和单元测试看起来像
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {MainMethodRunnerTest.class})
@IntegrationTest
public class PartnerConfigTestIT {
@Autowired
PartnerConfig partnerConfig;
@Test
public void getTinyUrlDomain_should_return_tinyUrlDomain_from_partnerId() {
assertEquals("hy.test.upgrd.co", partnerConfig.getTinyUrlDomain("2001"));
}
}