简体   繁体   English

无法从 JUnit 中的 Spring Boot 读取应用程序属性?

[英]Not able to read application properties from Spring Boot in JUnit?

I have a JUnit test that I would like to run against a REST service which is already running on my machine at the specified port.我有一个JUnit测试,我想针对REST服务运行该测试,该服务已在我的机器上的指定端口上运行。 Already tested the REST service with Postman and it works fine.已经用Postman测试了REST服务,它工作正常。

The plan here is to make the REST URLs configurable by externalizing them in a properties file.这里的计划是通过在属性文件中将REST URL 外部化来使它们可配置。 Hence I tried following and the JUnit class is not able to read the properties file value.因此,我尝试了以下操作,但JUnit类无法读取属性文件值。

StoreClientTest.java商店客户端测试.java

@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource("classpath:application.properties")
public class StoreClientTest {

    private static final String STORE_URI = "http://localhost:8084/hpdas/store";
    private RestTemplate restTemplate;

    @Value("${name}")
    private String name;


    @Before
    public void setUp() {
        restTemplate = new RestTemplate();
    }


    @Test
    public void testStore_NotNull() {
        //PRINTS ${name} instead of abc ?????
        System.out.println("name = " + name);
        assertNotNull(restTemplate.getForObject(STORE_URI, Map.class));
    }


    @After
    public void tearDown() {
        restTemplate = null;
    }

}

src\\test\\main\\resources\\application.properties src\\test\\main\\resources\\application.properties

name=abc

First, you need to create an application context configuration for the test首先,您需要为测试创建一个应用程序上下文配置

@SpringBootApplication
@PropertySource(value = "classpath:application.properties")
public class TestContextConfiguration {

}

Second, let your test class use this configuration其次,让你的测试类使用这个配置

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestContextConfiguration.class)
public class StoreClientTest {

    @Value("${name}")
    private String name;
    // test cases ..
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot从应用程序属性读取值 - Spring Boot read values from application properties "'application.yml' 中的 Spring Boot 属性未从 JUnit 测试加载" - Spring Boot properties in 'application.yml' not loading from JUnit Test 不读取junit spring引导配置文件属性 - junit spring boot profile properties not being read Junit 5 mockito 无法读取 spring 启动应用程序中用 @Value 注释的属性文件 - Junit 5 mockito unable to read properties file annotated with @Value in spring boot application 无法从 Spring Boot 应用程序中的 yaml 读取复杂的属性 - Couldn't read complex properties from yaml in Spring boot application 春季启动:从“ application.properties”读取文件路径 - Spring boot: Read a file PATH from “application.properties” 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties? 在界面中读取 Spring 引导应用程序属性 - Read Spring Boot application properties in interface 无法从 API 响应(Spring Boot Service)读取 Angular 中的 cookies(正在读取)- 无法读取 Z37A6259CC0C1DAE299A 的属性(正在读取) - Not able to read cookies in Angular from API response (Spring Boot Service) - Cannot read properties of null (reading 'headers') Spring Boot 访问 application.properties 以进行 JUnit 测试 - Spring Boot accessing application.properties for JUnit Test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM