1

我正在使用 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
4

1 回答 1

1

您传递给的参数urlPathEqualTo(...)应该是相对 URL,而不是绝对 URL。

我不知道这是否是导致您粘贴的异常的原因,但是 WireMock 会根据您当前的设置返回 404,我怀疑这不是您想要的。

于 2016-06-23T09:12:10.787 回答