JUNIT如何将文件属性导入测试中的服务
我正在编写单元测试,并具有来自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();
}
}
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论