我试图在 Spring Boot 中使用 @Value 从我的 application.properties 中获取一个值,但无论我做什么,它始终为 null。
我在我的 HTTPClient 测试类中这样做。我已经尝试使用环境变量、propertySource、PostConstruct、使用 getter 和 setter 以及我可以在网上找到的任何其他东西,但它似乎根本没有填充……我的测试类在 src/test/java 和应用程序中。属性在 src/test/resources 中。我的 src/main/java 中也有一个 application.properties,但它位于一个完全不同的文件夹中,因此它不应该影响它。
这是我的代码:
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import com.google.gson.Gson;
import com.nulogix.billing.configuration.EndPointTestConfiguration;
import com.nulogix.billing.mockserver.MockServerApp;
@SpringBootTest(classes = EndPointTestConfiguration.class)
public class HttpClientTest {
@Value("${billing.engine.address}")
private String billingEngineAddress;
@Value("${billing.engine.port}")
private String billingEnginePort;
@PostConstruct
private void customInit() {
System.out.print(billingEngineAddress);
System.out.print(billingEnginePort);
}
public static final String request_bad = "ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|";
public static final String request_good = "ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0|0.0|0.0";
@Test
public void test_bad() throws ClientProtocolException, IOException {
// missing parameter
String result = Request.Post("http://" + billingEngineAddress + ":" + billingEnginePort)
.connectTimeout(2000)
.socketTimeout(2000)
.bodyString(request_bad, ContentType.TEXT_PLAIN)
.execute().returnContent().asString();
Map<?, ?> resultJsonObj = new Gson().fromJson(result, Map.class);
// ensure the key exists
assertEquals(resultJsonObj.containsKey("status"), true);
assertEquals(resultJsonObj.containsKey("errorMessage"), true);
// validate values
Boolean status = (Boolean) resultJsonObj.get("status");
assertEquals(status, false);
String errorMessage = (String) resultJsonObj.get("errorMessage");
assertEquals(errorMessage.contains("Payload has incorrect amount of parts"), true);
}
@Test
public void test_good() throws ClientProtocolException, IOException {
String result = Request.Post("http://" + billingEngineAddress + ":" + billingEnginePort)
.connectTimeout(2000)
.socketTimeout(2000)
.bodyString(request_good, ContentType.TEXT_PLAIN)
.execute().returnContent().asString();
Map<?, ?> resultJsonObj = new Gson().fromJson(result, Map.class);
// ensure the key exists
assertEquals(resultJsonObj.containsKey("status"), true);
assertEquals(resultJsonObj.containsKey("errorMessage"), false);
assertEquals(resultJsonObj.containsKey("HasCopay"), true);
assertEquals(resultJsonObj.containsKey("CopayAmount"), true);
assertEquals(resultJsonObj.containsKey("HasCoinsurance"), true);
assertEquals(resultJsonObj.containsKey("CoinsuranceAmount"), true);
assertEquals(resultJsonObj.containsKey("version"), true);
// validate values
Boolean status = (Boolean) resultJsonObj.get("status");
assertEquals(status, true);
String version = (String) resultJsonObj.get("version");
assertEquals(version, "0.97");
}
}
我正在从我的 application.properties 中获取值以获取 IP 地址和端口并测试我的 Request.post。
这是我的 application.properties
server.port=9119
server.ssl.enabled=false
logging.config=classpath:logback-spring.xml
logging.file=messages
logging.file.max-size=50MB
logging.level.com.nulogix=DEBUG
billing.engine.address=127.0.0.1
billing.engine.port=9119
billing.engine.api.version=0.97
billing.engine.core.name=Patient_Responsibility
最佳答案
所以解决我的问题的方法是将 application.properties 从我的 main 复制到我的测试/资源。然后我使用 @PropertySource 将值更改为 test.properties,将其与 main 中的 application.properties 分开。我在我的端点配置中创建了一个值 bean,然后将它自动连接到我的 httpclient 测试类中。
我还在我的 SpringBootTest 注释中添加了 Web 环境,以使用 test.properties 中定义的端口并使用我的端点配置类和主应用程序类运行测试。这导致 @value 字符串被填充而不是 null。
@Configuration
public class EndPointTestConfiguration {
@Value("${billing.engine.address}")
private String mockServerIP;
@Value("${billing.engine.port}")
private String mockServerPort;
@Bean
public String mockAddress() {
String mockServerAddress = "http://" + mockServerIP + ":" + mockServerPort;
return mockServerAddress;
}
@Bean
public GetVersionEndPoint getVersionEndPoint() {
return new GetVersionEndPoint();
}
@Bean
public AnalyzeEndPoint analyzeEndPoint() throws JAXBException {
return new AnalyzeEndPoint();
}
@Bean
public PredictionEngineService predictionEngineService() {
return new PredictionEngineService();
}
@Bean
public String studyDetailDemo() throws IOException {
File file = ResourceUtils.getFile("src/test/resources/study-details-demo.txt");
String content = new String(Files.readAllBytes(file.toPath()));
return content;
}
@Bean
public String studyDetailSampleNormal() throws IOException {
File file = ResourceUtils.getFile("src/test/resources/study-details-normal.txt");
String content = new String(Files.readAllBytes(file.toPath()));
return content;
}
@Bean
public String studyDetailSampleCms() throws IOException {
File file = ResourceUtils.getFile("src/test/resources/study-details-cms.txt");
String content = new String(Files.readAllBytes(file.toPath()));
return content;
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,classes = {EndPointTestConfiguration.class,MockServerApp.class
})
@PropertySource(value={"classpath:test.properties"}, ignoreResourceNotFound = true)
public class HttpClientTest {
@Autowired
private String mockAddress;
public static final String request_bad = "ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|";
public static final String request_good = "ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0|0.0|0.0";
@Test
public void test_bad() throws ClientProtocolException, IOException {
// missing parameter
String result = Request.Post(mockAddress)
.connectTimeout(2000)
.socketTimeout(2000)
.bodyString(request_bad, ContentType.TEXT_PLAIN)
.execute().returnContent().asString();
Map<?, ?> resultJsonObj = new Gson().fromJson(result, Map.class);
// ensure the key exists
assertEquals(resultJsonObj.containsKey("status"), true);
assertEquals(resultJsonObj.containsKey("errorMessage"), true);
// validate values
Boolean status = (Boolean) resultJsonObj.get("status");
assertEquals(status, false);
String errorMessage = (String) resultJsonObj.get("errorMessage");
assertEquals(errorMessage.contains("Payload has incorrect amount of parts"), true);
}
@Test
public void test_good() throws ClientProtocolException, IOException {
String result = Request.Post(mockAddress)
.connectTimeout(2000)
.socketTimeout(2000)
.bodyString(request_good, ContentType.TEXT_PLAIN)
.execute().returnContent().asString();
Map<?, ?> resultJsonObj = new Gson().fromJson(result, Map.class);
// ensure the key exists
assertEquals(resultJsonObj.containsKey("status"), true);
assertEquals(resultJsonObj.containsKey("errorMessage"), false);
assertEquals(resultJsonObj.containsKey("HasCopay"), true);
assertEquals(resultJsonObj.containsKey("CopayAmount"), true);
assertEquals(resultJsonObj.containsKey("HasCoinsurance"), true);
assertEquals(resultJsonObj.containsKey("CoinsuranceAmount"), true);
assertEquals(resultJsonObj.containsKey("version"), true);
// validate values
Boolean status = (Boolean) resultJsonObj.get("status");
assertEquals(status, true);
String version = (String) resultJsonObj.get("version");
assertEquals(version, "0.97");
}
}
关于java - Spring Boot @Value 没有被填充..为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57454530/