【发布时间】:2019-04-06 16:47:15
【问题描述】:
我有一个休息端点如下:
@PostMapping(value = "/customers/{customerId}")
public SomeResponse manageCustomers(@PathVariable String customerId){
...
}
此端点从一个系统中为给定的 customerId 挑选客户数据并将其保存在另一个系统中。因此,它不需要任何请求正文。
我需要为此编写一个集成测试。当我为此使用 testRestTemplate 时,我找不到一个足够好的方法可以将 requestEntity 作为空值传递。每当我这样做时,我都会收到一个异常说“uriTemplate 不能为空”。
我曾尝试使用'postForObject'、'exchange' 方法但不起作用。有什么想法吗?
下面是我的 IT:
@SpringBootTest(webEnvironmentSpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
@ActiveProfiles("test")
class CustomerIT extends Specification{
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate
def "should get customer from first system and save in second system"() {
given:
def customerUrl = new URI("http://localhost:" + port + "/customers/1234")
def expected = new SomeObject(1)
when:
def someObject =
restTemplate.postForEntity(customerUrl, null, SomeObject.class)
then:
someObject != null
someObject == expected
}
}
【问题讨论】:
-
如果
expected、secondSystemMockUrl和uriTemplate是null,我首先要弄清楚为什么会这样。您能否创建第二个 IT 来模仿我写的最小的 IT,而不需要所有额外的东西?即使您不存根 secondSystem,您也可能会遇到不同的异常? -
我删除了所有的wiremock东西并尝试再次运行它以查看它是否至少到达控制器但我仍然遇到uriTemplate为空的相同异常。如果您有什么想法,请告诉我,但无论如何感谢您的关注。
-
或许您可以发布精简后的 IT?
-
用最少的 IT 编辑了我的帖子。
-
只需尝试几件事。 1) 删除
@Autowired和newrestTemplate。 2)customerUrl从URI到String和 3) 删除@DirtiesContext- 甚至可能是@ActiveProfiles("test")。我真的不指望这会有所作为,而只是把所有东西都去掉。也许看看你正在使用的任何测试框架的范围 - 我不熟悉它。非常牵强,但given:范围可能在when:范围中不可用
标签: spring-boot post uri integration-testing resttemplate