我正在使用groovy文件
编写集成测试的测试类我的Groovy文件:
@UseModules(value = [MiddleModule])
class MiddleServiceIT extends Specification{
@Inject
MiddleService middleService
@Rule
public WireMockRule Server = new WireMockRule(wireMockConfig().port(9090));
def 'validate config'() {
expect:
middleService != null
}
def 'validate get'() {
/* String urlPath = 'http://localhost:8889/middle/?acctID=80873000101&vodSource=ais&headendID=louis&activeOnly=true&limit=50&startDate=2014-10-09&inclNonSyncData=test' */
String urlPath = 'http://localhost:8889/middle/'
given:
Server.stubFor(get(urlPathEqualTo(urlPath)).willReturn(MiddleServiceStub.buildSuccess()))
when:
MiddleMessage response = middleService.get( '80873000101','ais', 'louis', 'true', '50', '2014-10-09', 'test')
then:
response != null
}
}
这里我的要求是当它到达该URL时,它必须给出我在stubFor调用中提到的响应。
我的原创方法:
@Override
public MiddleMessage get(String acctID, String vodSource, String headendID, String activeOnly, String limit, String startDate, String inclNonSyncData){
String url = rentalsMiddleBaseUrl + "/?acctID=" + acctID + "&vodSource=" + vodSource + "&headendID=" + headendID + "&activeOnly=" + activeOnly + "&limit=" + limit + "&startDate=" + startDate + "&inclNonSyncData=" + inclNonSyncData;
System.out.println("***** For Test URL "+url);
return (MiddleMessage) commandBuilder.build(url,MiddleMessage.class).execute();
}
MiddleServiceStub类中的我的存根方法:
public static ResponseDefinitionBuilder buildSuccess() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
MiddleSet middleSet = new MiddleSet();
MiddleMessage middleMessage = new MiddleMessage();
middleMessage.setMiddleSet(middleSet);
String json = mapper.writeValueAsString(middleMessage);
ResponseDefinitionBuilder responseDefinitionBuilder = ResponseDefinitionBuilder.like(ResponseDefinition.created()).withBody(json);
return responseDefinitionBuilder;
}
当我使用maven命令“clean install”运行上面的代码时,它给出了以下异常
validate get(com.config.MiddleServiceTest) Time elapsed: 1.918 sec <<< ERROR!
com.netflix.hystrix.exception.HystrixRuntimeException: execute timed-out and no fallback available.
at com.netflix.hystrix.HystrixCommand.getFallbackOrThrowException(HystrixCommand.java:1646)
at com.netflix.hystrix.HystrixCommand.access$1900(HystrixCommand.java:103)
at com.netflix.hystrix.HystrixCommand$TimeoutObservable$1$1.run(HystrixCommand.java:1023)
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable$1.call(HystrixContextRunnable.java:41)
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable$1.call(HystrixContextRunnable.java:37)
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable.run(HystrixContextRunnable.java:57)
at com.netflix.hystrix.HystrixCommand$TimeoutObservable$1$2.tick(HystrixCommand.java:1047)
at com.netflix.hystrix.HystrixCommand$1.performBlockingGetWithTimeout(HystrixCommand.java:627)
at com.netflix.hystrix.HystrixCommand$1.get(HystrixCommand.java:522)
at com.netflix.hystrix.HystrixCommand.execute(HystrixCommand.java:431)
at com.client.MiddleService.get(MiddleService.java:72)
at com.config.MiddleServiceTest.validate get(MiddleServiceTest.groovy:22)
Caused by: java.util.concurrent.TimeoutException
答案 0 :(得分:1)
您传递给urlPathEqualTo(...)
的参数应该是相对网址,而不是绝对网址。
我不知道这是否是导致您已经粘贴的异常的原因,但WireMock会根据您当前的设置返回404,我怀疑这不是您想要的