spring测试从application.properties获取自定义属性

 

一、首先引入spring test依赖

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.4.4</version>
    </dependency>

二、application.properties

zookeeper.master.key=/master
zookeeper.worker.key=/workers

三、测试类

package org.example;

import org.example.master.init.ZKPath;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Author : wangbin
 * @Date : 2021/4/28 11:55
 */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ZKPath.class)
public class Test1 {
    @Autowired
    private ZKPath zkPath;

    @Test
    public void test1() {
        System.out.println(zkPath.getMasterKey());
        System.out.println(zkPath.getWorkerKey());
    }
}

 

上一篇:maven单继承问题


下一篇:JavaWeb-1.1【基础加强:Junit单元测试】