提问人:schooner_101 提问时间:8/10/2019 更新时间:8/12/2019 访问量:11034
@Value在单元测试中返回 null
@Value returning null in unit test
问:
我有一个带有端点测试配置类的 spring boot 应用程序和一个单元测试来测试我的 http 客户端。我正在尝试从位于我的 src/test 中的 application.properties 中获取我的服务器地址和端口。(所有类都在我的 src/test 中。
这是我的配置类代码:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.xml.bind.JAXBException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import com.nulogix.billing.service.PredictionEngineService;
import com.nulogix.billing.ws.endpoint.AnalyzeEndPoint;
import com.nulogix.billing.ws.endpoint.GetVersionEndPoint;
@Configuration
public class EndPointTestConfiguration {
@Value("${billing.engine.address}")
private String mockAddress;
@Value("${billing.engine.port}")
private String mockPort;
@Bean
public String getAddress() {
String serverAddress = "http://" + mockAddress + ":" + mockPort;
return serverAddress;
}
@Bean
public GetVersionEndPoint getVersionEndPoint() {
return new GetVersionEndPoint();
}
我用 @value 注释了我的 .properties 中的值,然后创建了一个方法,我用 bean 实例化了该方法以返回我的服务器地址字符串。
然后,我在 HttpClientTest 类中使用该字符串值:
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.Map;
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.Autowired;
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 {
@Autowired
EndPointTestConfiguration endpoint;
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";
//gets application context
static ConfigurableApplicationContext context;
//call mock server before class
@BeforeClass
static public void setup(){
SpringApplication springApplication = new SpringApplicationBuilder()
.sources(MockServerApp.class)
.build();
context = springApplication.run();
}
//shutdown mock server after class
@AfterClass
static public void tearDown(){
SpringApplication.exit(context);
}
@Test
public void test_bad() throws ClientProtocolException, IOException {
// missing parameter
String result = Request.Post(endpoint.getAddress())
.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(endpoint.getAddress())
.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");
}
}
我在 request.post 中使用它,我不想在我的 IP 地址和端口号中硬编码。
当我运行测试时,它说
[ERROR] HttpClientTest.test_bad:63 NullPointer
[ERROR] HttpClientTest.test_good:86 NullPointer
但我不确定为什么它是空的?我很确定我已经实例化了所有内容并且字符串已明确填充。.
我的配置包结构是 com.billing.mockserver,单元测试的包结构是 com.billing.ws.endpoint。
这是我的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
答:
你缺少springboot的基本理解。 类是初始化其他 Spring Bean 和其他东西,是第一个初始化的类。你不应该上课。@Configuration
@Autowire
@configuration
在 Configuration 类中,您可以为用户名和密码创建 Spring bean,并在测试类中自动连接它,也可以直接在 Test 类中使用。@Value
示例:在配置类中,您正在创建 bean,您可以在 Test 类中创建 bean。GetVersionEndPoint
autowire
更新2:
对于测试类,您需要在application.properties
src\test\resource
评论
@PropertySource / @ConfigurationProperties