Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用spock为restfulwebservice调用开发模拟服务_Java_Junit_Spock - Fatal编程技术网

Java 如何使用spock为restfulwebservice调用开发模拟服务

Java 如何使用spock为restfulwebservice调用开发模拟服务,java,junit,spock,Java,Junit,Spock,我正在尝试为RESTful API客户端开发一个模拟服务(PricingClaimRestServiceClient)。在下面的JUnit类中,在一些嵌套方法调用之后调用RESTful API客户机。实际上,我不想模拟所有东西(所有嵌套的东西),我只想在调用这个RESTful API时,它应该调用模拟服务,而不是原始的RESTful API客户端(PricingClaimRestServiceClient)。请建议我的JUnit和Spock代码应该如何使用 其次,我需要在.groovy文件中编写

我正在尝试为RESTful API客户端开发一个模拟服务(
PricingClaimRestServiceClient
)。在下面的
JUnit
类中,在一些嵌套方法调用之后调用RESTful API客户机。实际上,我不想模拟所有东西(所有嵌套的东西),我只想在调用这个RESTful API时,它应该调用模拟服务,而不是原始的RESTful API客户端(
PricingClaimRestServiceClient
)。请建议我的
JUnit
和Spock代码应该如何使用

其次,我需要在
.groovy
文件中编写Spock代码,现有的
JUnit
代码在
.java
文件中。因此,我需要使用
groovyshell
groovyclassloader
JUnit
代码调用Spock code
.groovy
文件。对吗

仅供参考,嵌套方法调用:

Assert.assertTrue(bundleReportService.priceBundle(bundle));
/*->*/bundleReport=ProviderPricinIntegrationService.getPricingClaimDetails(bundle);
/*->*/pricingClaimRestServiceClient.getPricingClaimResponse(pricingRequestXml);
My
JUnit
测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=ConfigurationAwareContextLoader.class,位置={“/integrationTest ApplicationContext.xml”})
公共类BundleReportServiceIT扩展了BatchTestCase{
@自动连线
受保护的BundleReportService BundleReportService;
束;
@以前
public void setUp()引发异常{
获取捆绑包设置
}
@试验
公共void testPriceBundle(){
bundle.getTriggerClaimLines().get(0.setValueCache(getValueCache());
Assert.assertTrue(bundleReportService.priceBundle(bundle));
}
}
bundlereportserviceinpl
class:

公共类BundlerReportServiceImpl扩展基本服务实现BundlerReportService{
公共布尔价格包(Bundle){
BundleReport BundleReport;
试一试{
bundleReport=ProviderPricinIntegrationService.getPricingClaimDetails(bundle);
}捕获(可丢弃的t){
logger.error(String.format(“定价集成服务中的错误:%s”,t));
掷t;
}
}
ProviderPricinIntegrationServiceImpl
class:

公共类ProviderPricinIntegrationServiceImpl实现ProviderPricinIntegrationService{
@凌驾
@事务性(传播=传播。不受支持)
公共Bundle报告getPricingClaimDetails(Bundle Bundle)引发BundlePricingException{
//调用rest客户端以点击服务。
clientResponse=pricingClaimRestServiceClient.getPricingClaimResponse(pricingRequestXml);
}
}
RESTful API客户端调用我想要为其创建模拟服务的其他应用程序

公共类PricingClaimRestServiceClient{
@施工后
void initWebResource(){
ApacheHttpClient=ApacheHttpClient.create();
addFilter(新的HTTPBasicAuthFilter(用户名、密码));
setReadTimeout(requestTimeout);
ppRequestBuilder=client.resource(rootUri.trim()).path(安全的\u API\u APPENDER);
}
/**
*创建资源以使用apache http客户端调用定价声明服务。
*/
私有WebResource getResource(){
返回ppRequestBuilder;
}
公共字符串getPricingClaimResponse(字符串pricingRequestXml){
ClientResponse ClientResponse=getResource().path(定价\u API+定价\u API\u端点)
.type(MediaType.APPLICATION_XML)
.accept(请求\接受\类型\ V14)
.post(ClientResponse.class,pricingRequestXml);
String response=clientResponse.getEntity(String.class);
if(clientResponse.getStatus()!=200){
抛出新的RuntimeException(“调用定价声明服务时出错。服务状态代码为:”+clientResponse.getStatus());
}
返回响应;
}
}
}

构建需要将Groovy代码编译成.class文件。您的JUnit测试将导入并使用这些类。编译的Groovy类由JVM执行,只要它们位于类路径上,单元测试就可以使用它们。Spock使用JUnit runner,因此JUnit或Maven插件(如Surefire和Failsafe)在正确配置后将自动运行Spock测试。如果您认为有意义,还可以在一个项目中同时使用JUnit测试(用Java或Groovy编写)和Spock测试(用Groovy编写)。除了我所说的,你的问题太不清楚了,我无法回答。谢谢保罗和克里加克斯。关于我的第一个问题,请提出建议。我想为RESTAPI客户端开发一个模拟服务(PricingClaimRestServiceClient)。在我的Junit中,在一些嵌套方法调用之后,它最终调用了原始的RESTAPI调用。所以我不想嘲笑所有嵌套的东西。我只想开发一个模拟RESTAPI客户机,当最终调用它时,它将调用模拟REST客户机而不是原始客户机。请建议我的junit/spock代码将是什么样子?您的构建需要编译Groovy代码(到.class文件中)。您的JUnit测试将导入并使用这些类。编译的Groovy类由JVM执行,只要它们位于类路径上,单元测试就可以使用它们。Spock使用JUnit runner,因此JUnit或Maven插件(如Surefire和Failsafe)在正确配置后将自动运行Spock测试。如果您认为有意义,还可以在一个项目中同时使用JUnit测试(用Java或Groovy编写)和Spock测试(用Groovy编写)。除了我所说的,你的问题太不清楚了,我无法回答。谢谢保罗和克里加克斯。关于我的第一个问题,请提出建议。我想为RESTAPI客户端开发一个模拟服务(PricingClaimRestS)