Код: Выделить всё
package com.test.config;
import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.PostConstruct;
import java.util.Map;
@Getter
@Configuration
@ConfigurationProperties(prefix = "saxon", ignoreInvalidFields = true)
public class SaxonOmsConfig {
private String url;
private String clientId;
private String clientSecret;
private Map retailerCredentialsMap;
private Map retailerMap;
private String retailerCredentials;
private String retailerMapping;
public Map getRetailerCredentials() {
return retailerCredentialsMap;
}
public Map getRetailerMapping() {
return retailerMap;
}
@PostConstruct
private void parseRetailerCredentials() {
if (retailerCredentials != null && !retailerCredentials.isBlank()) {
try {
ObjectMapper mapper = new ObjectMapper();
retailerCredentialsMap = mapper.readValue(retailerCredentials, Map.class);
} catch (Exception e) {
throw new RuntimeException("Failed to parse retailer_credentials JSON", e);
}
}
}
@PostConstruct
private void parseRetailerMapping() {
if (retailerMapping != null && !retailerMapping.isBlank()) {
try {
ObjectMapper mapper = new ObjectMapper();
retailerMap = mapper.readValue(retailerMapping, Map.class);
} catch (Exception e) {
throw new RuntimeException("Failed to parse retailer_mapping JSON", e);
}
}
}
}
< /code>
Я добавил тест, как ниже: < /p>
package com.test.config;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SaxonOmsConfig.class)
@EnableConfigurationProperties(SaxonOmsConfig.class)
@TestPropertySource(properties = {
"saxon.url=https://test-url",
"saxon.client-id=test-client-id",
"saxon.client-secret=test-client-secret",
"saxon.retailer-credentials={\"saxon_au\":{\"id\":1,\"account_id\":\"acc1\",\"username\":\"user1\",\"password\":\"pass1\"}}"
})
class SaxonOmsConfigTest {
@Autowired
private SaxonOmsConfig config;
@Test
void testPropertiesLoaded() {
assertEquals("https://test-url", config.getUrl());
assertEquals("test-client-id", config.getClientId());
assertEquals("test-client-secret", config.getClientSecret());
}
}
Как я могу проверить этот класс конфигурации весны? Я также попробовал эту комбинацию аннотаций: < /p>
Код: Выделить всё
@SpringBootTest(classes = SaxonOmsConfig.class)
@TestPropertySource(properties = { .. }
Подробнее здесь: https://stackoverflow.com/questions/796 ... tion-class