JUNIT如何将文件属性导入测试中的服务

发布于 2025-01-29 14:54:04 字数 1592 浏览 4 评论 0原文

我正在编写单元测试,并具有来自application.yaml属性文件的一些值。在我的测试中,我可以从测试/资源中导入服务方法值,但是当进入服务时,我在这里获取零值属性,这里是一些代码: 测试:

@ActiveProfiles("test")
@SpringBootTest
@TestPropertySource("classpath:application-test.yaml")
public class AmadeusClientUnitTest {

    @Value("${clientId}")
    public String clientId;

    @Value("${clientSecret}")
    public String clientSecret;

    @Spy
    @InjectMocks
    private AmadeusClient amadeusClient;

    @Test
    void shouldGetAmadeusObj() {
        amadeusClient.getAmadeusObj();

        verify(Amadeus.builder(any(), any()).build());
    }

application-test.yaml:

spring:
  config:
    activate:
      on-profile: test
clientId: "clientId"
clientSecret: "secret"

这是服务:

@Service
public class AmadeusClient {

    private final static Logger LOGGER = Logger.getLogger(AmadeusClient.class.getName());

    @Value("${clientId}")
    private String clientId;
    @Value("${clientSecret}")
    private String clientSecret;

    public Amadeus getAmadeusObj() {
        return Amadeus.builder(clientId, clientSecret).setLogger(LOGGER).build();
    }
}

“

“

我从调试测试中放了一些IMG。我缺少任何属性吗? 感谢您的任何帮助,我觉得我在某处缺少“点”。感谢这里的任何提示。祝你今天过得愉快

I am writting unit test and have some values from application.yaml property file. In my test before i can go into service method values are imported from test/resources, but when go into service im getting null value properties here is some code :
test :

@ActiveProfiles("test")
@SpringBootTest
@TestPropertySource("classpath:application-test.yaml")
public class AmadeusClientUnitTest {

    @Value("${clientId}")
    public String clientId;

    @Value("${clientSecret}")
    public String clientSecret;

    @Spy
    @InjectMocks
    private AmadeusClient amadeusClient;

    @Test
    void shouldGetAmadeusObj() {
        amadeusClient.getAmadeusObj();

        verify(Amadeus.builder(any(), any()).build());
    }

application-test.yaml :

spring:
  config:
    activate:
      on-profile: test
clientId: "clientId"
clientSecret: "secret"

and here is service :

@Service
public class AmadeusClient {

    private final static Logger LOGGER = Logger.getLogger(AmadeusClient.class.getName());

    @Value("${clientId}")
    private String clientId;
    @Value("${clientSecret}")
    private String clientSecret;

    public Amadeus getAmadeusObj() {
        return Amadeus.builder(clientId, clientSecret).setLogger(LOGGER).build();
    }
}

debug

debug

I put some imgs from debugging test. Am I missing any properties ?
Thanks for any help i feel like i am missing "dot" somewhere. Appreciate for any tip here. Have a nice day

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文