java - 使用junit5 + springboot 2.5.4进行单元测试
问题描述
我有一个 REST 应用程序,它正在使用 SOAP 服务并返回响应。我创建了一个配置类,如下所示,它从 yml 文件加载属性。
@Configuration
public class AppConfig{
@Value("${data.value1}")
String value1;
@Value("${data.value2}")
String value2;
}
服务等级
@Service
public class AppService{
public String getDetails(){
RetriveClient retriveClient = new SoapClient().getRetriveEndPoint();
// create request and call the retriveClient.getData method
// AppConfig details used here and inside SoapClient
return response;
}
在我的测试课中:
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes ={AppService.class, RetriveClient.class, SoapClient.class})
@ContextConfiguration(classes =AppConfig.class)
@ActiveProfiles("test")
public class AgentServiceTest{
@Autowired
AppService service;
public void getDetailsTeat(){
assertNotNull(service.getDetails());
}
}
现在,每种方法都有以下场景和错误:
- 使用上述方法,我收到一个异常,其中从 AppConfig 检索的详细信息为空
- 如果我从@SpringBootTest 中删除 RetriveClient.class、SoapClient.class,然后得到一个支持 Content-Type application/xml 支持的异常是 text/xml,我尝试在服务类中使用下面的代码设置它但没有运气
((BindingProvider)retriveClient).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, Collections.singletonMap("Content-Type",Collections.singletonList("text/xml")));
- 我不能像它一样使用 Mocking;它会影响代码覆盖率
有人可以说明我们如何实现这一目标吗?我需要测试 SOAP 客户端调用,它应该反映在代码覆盖率中
解决方案
推荐阅读
- java - Java中2个进程的生产者-消费者问题
- .htaccess - 如果重写规则匹配重定向,.htaccess 在新服务器上不起作用
- node.js - Angular 构建抛出错误 (JSON.parse(stripJsonComments(content)))
- mysql - 我可以仅从名称在另一个表中具有特定属性的列中检索数据吗
- automation - 如何禁用 Ansible 的密码混淆功能
- python - 如何使用 numba 更改数组中的索引
- django - 调用 super() 时 Django 模型覆盖 save() 方法引发错误
- java - Dockerize rmi 应用程序
- emacs - 进程前哨错误:无法启动 nREPL 服务器:java.lang.NumberFormatException:无效号码
- sql - Oracle SQL Pivot with Rollup group by