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コマンド「クリーンインストール」を使用して上記のコードを実行すると、次の例外が発生します
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