首页 > 解决方案 > 使用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());
    }
}

现在,每种方法都有以下场景和错误:

  1. 使用上述方法,我收到一个异常,其中从 AppConfig 检索的详细信息为空
  2. 如果我从@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")));

  1. 我不能像它一样使用 Mocking;它会影响代码覆盖率

有人可以说明我们如何实现这一目标吗?我需要测试 SOAP 客户端调用,它应该反映在代码覆盖率中

标签: javaspring-bootjunitjunit5soap-client

解决方案


推荐阅读