Spring boot 如何对没有请求正文的post请求使用testRestTemplate

Spring boot 如何对没有请求正文的post请求使用testRestTemplate,spring-boot,post,uri,integration-testing,resttemplate,Spring Boot,Post,Uri,Integration Testing,Resttemplate,我有一个rest端点,如下所示: @PostMapping(value = "/customers/{customerId}") public SomeResponse manageCustomers(@PathVariable String customerId){ ... } 此端点从一个系统中为给定的customerId拾取客户数据,并将其保存到另一个系统中。因此,它不需要任何请求主体 我需要为此编写一个集成测试。当我为此使用testRestTemplate时,我找不到一个可以将req

我有一个rest端点,如下所示:

@PostMapping(value = "/customers/{customerId}")
public SomeResponse manageCustomers(@PathVariable  String customerId){
...
}
此端点从一个系统中为给定的customerId拾取客户数据,并将其保存到另一个系统中。因此,它不需要任何请求主体

我需要为此编写一个集成测试。当我为此使用testRestTemplate时,我找不到一个可以将requestEntity作为null传递的足够好的方法。每当我这样做时,我都会得到一个异常,说“uriTemplate不能为null”

我曾尝试使用“postForObject”、“exchange”方法,但不起作用。有什么想法吗

以下是我的个人信息:

@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
}
}
使用
postForEntity(url,null,ResponseType.class)
对我来说很有用

除了响应类型外,我的postmapping与您的相同。我使用了
Map
作为示例

@PostMapping(value = "/customers/{customerId}")
public Map<String, String> manageCustomers(@PathVariable String customerId){
    return new HashMap<String, String>(){{ put("customerId", customerId); }};
}
在maven中运行此测试

$ mvn -Dtest=CustomerControllerTest test

    (...removed unnecessary output...)

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.137 s - in com.ins.example.demo.rest.CustomerControllerTest
10:04:54.683 [Thread-3] INFO  o.s.s.c.ThreadPoolTaskExecutor - Shutting down ExecutorService 'applicationTaskExecutor'
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.940 s
[INFO] Finished at: 2019-04-07T10:04:55+02:00
[INFO] ------------------------------------------------------------------------
使用
postForEntity(url,null,ResponseType.class)
对我来说很有用

除了响应类型外,我的postmapping与您的相同。我使用了
Map
作为示例

@PostMapping(value = "/customers/{customerId}")
public Map<String, String> manageCustomers(@PathVariable String customerId){
    return new HashMap<String, String>(){{ put("customerId", customerId); }};
}
在maven中运行此测试

$ mvn -Dtest=CustomerControllerTest test

    (...removed unnecessary output...)

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.137 s - in com.ins.example.demo.rest.CustomerControllerTest
10:04:54.683 [Thread-3] INFO  o.s.s.c.ThreadPoolTaskExecutor - Shutting down ExecutorService 'applicationTaskExecutor'
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.940 s
[INFO] Finished at: 2019-04-07T10:04:55+02:00
[INFO] ------------------------------------------------------------------------

我仍然得到同样的例外。我已经用集成测试更新了我的问题。你能看一下吗?我还是有同样的例外。我已经用集成测试更新了我的问题。你能看一下吗?如果
expected
secondSystemMockUrl
uriTemplate
null
,我首先要弄清楚为什么会这样。你能不能创建一个第二个,模仿我写的最小的一个,而不需要所有额外的东西?即使你不存根第二个系统,你可能会得到一个不同的异常?我删除了所有wiremock的东西,并试图再次运行它,看看它是否至少到达控制器,但我仍然得到相同的异常uriTemplate为null。如果你有什么想法,请告诉我,但无论如何,谢谢你的关注。也许你可以发布精简版?用精简版编辑我的文章。只需做一些尝试。1) 删除
@Autowired
,只需
新建
restTemplate
。2)
customerUrl
URI
String
,3)删除
@DirtiesContext
——甚至可能删除
@ActiveProfiles(“测试”)
。我真的不指望这些会有什么不同,只是想把一切都去掉。也许可以看看您正在使用的任何测试框架的作用域如何-我不熟悉它。非常牵强,但可能
给定:
范围在
中不可用,当:
范围如果
预期的
secondSystemMockUrl
uriTemplate
null
时,我首先要弄清楚为什么会出现这种情况。你能不能创建一个第二个,模仿我写的最小的一个,而不需要所有额外的东西?即使你不存根第二个系统,你可能会得到一个不同的异常?我删除了所有wiremock的东西,并试图再次运行它,看看它是否至少到达控制器,但我仍然得到相同的异常uriTemplate为null。如果你有什么想法,请告诉我,但无论如何,谢谢你的关注。也许你可以发布精简版?用精简版编辑我的文章。只需做一些尝试。1) 删除
@Autowired
,只需
新建
restTemplate
。2)
customerUrl
URI
String
,3)删除
@DirtiesContext
——甚至可能删除
@ActiveProfiles(“测试”)
。我真的不指望这些会有什么不同,只是想把一切都去掉。也许可以看看您正在使用的任何测试框架的作用域如何-我不熟悉它。非常牵强,但可能
给定:
作用域在
时:
作用域中不可用