【发布时间】:2023-03-30 16:17:02
【问题描述】:
我需要针对 REST API 创建集成测试。我的服务使用 Resttemplate 作为 HTTP 客户端。客户端代码由 swagger 文件生成。
运行测试会产生错误java.lang.AssertionError: No further requests expected: HTTP GET
似乎测试是针对模拟服务器运行的。如何让测试在真实服务器上运行?
这是我当前的测试设置(想剪掉一个最小的测试框架来进行快速测试 - 启动完整的上下文太慢了):
@RunWith(SpringRunner.class)
@Import(value = { TpzConfig.class, TpzServiceRestImpl.class, ManufacturingPlantPhPmMapperImpl.class,
ProductHierarchyMapperImpl.class, PlantMapperImpl.class })
@ActiveProfiles(profiles = { "tpz" })
@RestClientTest
public class TpzServiceRestImplTest {
@Autowired
private TpzService to;
@MockBean
private ProductionPlantService ppService;
@MockBean
private ProductHierarchyService phService;
@Test
public void test() {
List<ProductManufacturer> pmByProductHierarchy = to.pmByProductHierarchy("001100909100100388");
}
}
我需要@RestClientTest 来拥有一个 RestTemplateBuilder 的 bean。
有没有办法配置@RestClientTest 使用真实服务器(类似于@DataJpaTest 我可以配置不使用h2)?
【问题讨论】:
标签: spring-boot integration-testing