我试图为我的一个微服务编写一个集成测试,该测试在将对象保存到数据库之前,先调用另一个微服务以执行一些验证。
由于第二个微服务未运行,我想模拟对外部服务的请求,但测试失败并出现错误:
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();
}
}
任何建议将不胜感激。谢谢!
检查下面的链接。看看是否对您有帮助。