@SpringBootTest
public class RuleControllerTest {
@Value("${myUrl}")
private String myUrl;
private HttpClient httpClient = HttpClients.createDefault();
@Test
public void loadAllRules() throws IOException, URISyntaxException {
String target = myUrl + "/v2/rules";
String json = generateHTTPget(target);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Rule[] rules = objectMapper.readValue(json, Rule[].class);
boolean correctResponse = rules != null ? true : false;
int httpCode = getHTTPcode(target);
boolean correctStatus = httpCode >= 200 && httpCode <= 300 ? true : false;
assertTrue(correctStatus);
assertTrue(correctResponse);
}
我试图从 application.properties 文件中获取一个字符串,并将 @Value 插入到 Junit 测试的字段中。我以前在普通课堂上做过这个,但在测试中我的字段为空。我读过有关此问题的类似问题,到目前为止尝试创建 src/test/resources 包并在那里克隆 application.properties 。还尝试添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
并添加注释
@RunWith(SpringRunner.class)
我收到一条消息 Notestsfound with test runner Junit 5 并且在 Problems 选项卡中我无法读取 springboottest.jar 或者不是有效的 ZIP 文件
也尝试过
@PropertySource("classpath:application.properties")
作为类注释,但结果相同
我也尝试过:
@RunWith(SpringJUnit4ClassRunner.class)
如果我将字符串 myUrl 硬编码为“http://本地主机:8090 http://localhost:8090“测试有效,所以问题在于@Value 不起作用