gpt4 book ai didi

java - Spring Cloud Eureka - 如何模拟 RestTemplate 以避免请求第二个服务

转载 作者:行者123 更新时间:2023-12-02 09:08:06 25 4
gpt4 key购买 nike

我正在尝试为我的一个微服务编写集成测试,在将对象保存到数据库中之前,调用另一个微服务以执行一些验证。

由于第二个微服务未运行,我想模拟对外部服务的请求,但测试失败并出现错误:

Condition failed with Exception:

mockServer.verify()
| |
| java.lang.AssertionError: Further request(s) expected leaving 1 unsatisfied expectation(s).
| 0 request(s) executed.
|
| at org.springframework.test.web.client.AbstractRequestExpectationManager.verify(AbstractRequestExpectationManager.java:159)
| at org.springframework.test.web.client.MockRestServiceServer.verify(MockRestServiceServer.java:116)

下面是测试逻辑:

@SpringBootTest(classes = PropertyApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = ["eureka.client.enabled:false"])
class TestPropertyListingServiceDemo extends IntegrationTestsSetup {
@Autowired
private PropertyListingService listingService
@Autowired
private RestTemplate restTemplate

private static MockRestServiceServer mockServer


def setup() {
mockServer = MockRestServiceServer.createServer(restTemplate)
}


def "test: save listing for in-existent User"() {

setup: "building listing with invalid user id"
def listing = generatePropertyListing()

mockServer.expect(once(), requestTo("http://user-service/rest/users/exists/trackingId=" + listing.getUserTID()))
.andExpect(method(GET))
.andRespond(withStatus(NOT_FOUND).body("No such user."))


when: "saving listing"
listingService.save(listing)

then: "exception is thrown"
mockServer.verify() // <------------- here I am getting the error

BizItemBusinessValidationException e = thrown()
e.getMessage() == "Listing could not be saved. User not found."
}

}

我正在使用我试图模拟的请求来测试服务:

@Service
public class PropertyListingService {
private BizItemService itemService;
private PropertyService propertyService;
private RestTemplate restTemplate;

public PropertyListingService(BizItemService itemService,PropertyService propertyService, RestTemplate restTemplate) {
this.propertyService = propertyService;
this.restTemplate = restTemplate;
this.itemService=itemService;
}


public PropertyListing save(PropertyListing listing) {

if (listing == null) {
throw new BizItemBusinessValidationException("Listing could not be saved. Invalid Listing.");
}

if (propertyService.findByTrackingId(listing.getPropertyTID()) == null) {
throw new BizItemBusinessValidationException("Listing could not be saved. Property not found.");
}

if (userExists(listing.getUserTID())) {
throw new BizItemBusinessValidationException("Listing Could not be saved. User not found, UserTID = " + listing.getUserTID());
}

return (PropertyListing) itemService.save(listing);
}


/**------------------------------------------------------------
* THIS IS THE CALL TO EXTERNAL SERVICE I AM TRYING TO MOCK
* ------------------------------------------------------------
*/

private boolean userExists(String userTID) {
URI uri = URI.create("http://user-service/rest/users/exists/trackingId=" + userTID);
ResponseEntity response = (ResponseEntity) restTemplate.getForObject(uri, Object.class);

return response != null && response.getStatusCode() == HttpStatus.OK;
}

}

RestTemplate 配置:

@Configuration
public class BeanConfiguration {

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

如有任何建议,我们将不胜感激。谢谢!

最佳答案

正如 @spencergibb 正确建议的那样,您可以模拟您的restTemplate作为测试配置的一部分。

第二个选项,您可以尝试使用 MockRestServiceServer。

检查下面的链接。看看它是否对您的情况有帮助。

https://www.baeldung.com/spring-mock-rest-template

关于java - Spring Cloud Eureka - 如何模拟 RestTemplate 以避免请求第二个服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59638960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com