加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

groovy – 如何为soapUI MockService使用外部响应文件的相对路径

发布时间:2020-12-14 16:31:22 所属栏目:大数据 来源:网络整理
导读:我做了什么 我正在使用soapUI(3.6.1免费版)模拟服务来向我正在测试的2个客户端应用程序提供特定数据.使用一些简单的Groovy脚本,我设置了一些模拟操作,以根据客户端应用程序发出的请求从特定文件中获取响应. 模拟响应的静态内容是: ${responsefile} 操作调度
我做了什么

我正在使用soapUI(3.6.1免费版)模拟服务来向我正在测试的2个客户端应用程序提供特定数据.使用一些简单的Groovy脚本,我设置了一些模拟操作,以根据客户端应用程序发出的请求从特定文件中获取响应.

模拟响应的静态内容是:

${responsefile}

操作调度脚本窗格中的groovy是:

def req = new XmlSlurper().parseText(mockRequest.requestContent)
if (req =~ "CategoryA")
{
    context.responsefile = new File("C:/soapProject/Test_Files/ID_List_CategoryA.xml").text
}
else
{
    context.responsefile = new File("C:/soapProject/Test_Files/ID_List_CategoryB.xml").text
}

在此示例中,当客户端应用程序向包含字符串CategoryA的模拟服务发出请求时,soapUI返回的响应是文件ID_List_CategoryA.xml的内容

我正在努力实现的目标

这一切都适用于groovy中的绝对路径.现在我想将soapUI项目文件和外部文件的整个集合放入一个包中,以便于重新部署.从我对soapUI的阅读中我希望这就像将项目资源根值设置为${projectDir}并将路径更改为:

def req = new XmlSlurper().parseText(mockRequest.requestContent)
if (req =~ "CategoryA")
{
    context.responsefile = new File("Test_Files/ID_List_CategoryA.xml").text
}
else
{
    context.responsefile = new File("Test_Files/ID_List_CategoryB.xml").text
}

…请记住,soapUI项目xml文件位于C:/ soapProject /

我到目前为止所做的一切

所以,这不起作用.我尝试过相对路径的变体:

> ./Test_Files/ID_List_CategoryA.xml
> /Test_Files/ID_List_CategoryA.xml
> Test_Files / ID_List_CategoryA.xml

一篇文章指出,soapUI可能会将项目文件父目录视为根目录的相对路径,因此也尝试了以下变体:

> ./soapProject/Test_Files/ID_List_CategoryA.xml
> /soapProject/Test_Files/ID_List_CategoryA.xml
> soapProject / Test_Files / ID_List_CategoryA.xml

如果没有这个工作,我尝试在groovy脚本中使用${projectDir}属性,但是所有这些尝试都失败了“没有这样的属性:mockService for class:Script [n]”错误. Admittefly,当我试图这样做时,我真的在摸索.

我尝试使用这篇文章和其他人的信息:How do I make soapUI attachment paths relative?

……没有运气.在该帖子的解决方案代码中用“mock”(以及其他更改)替换“test”会导致更多属性错误,例如:

testFile = new File(mockRunner.project.getPath())

.. 导致…

No such property: mockRunner for class: Script3

我认为我需要什么

我发现与此问题相关的帖子都集中在soapUI TestSuites上.我真的需要一个以MockService为中心的解决方案,或者至少可以说明MockServices如何以不同的方式处理它而不是TestSuite.

任何帮助是极大的赞赏.谢谢.标记.

解决方案 – 由GargantuChet提供

以下内容包括GargantuChet建议的更改,以解决尝试访问${projectDir}属性的问题,并通过在groovy脚本范围内定义新的projectDir对象来启用相对路径的使用:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectDir = groovyUtils.projectPath

def req = new XmlSlurper().parseText(mockRequest.requestContent)
if (req =~ "CategoryA")
{
    context.responsefile = new File(projectDir,"Test_Files/ID_List_CategoryA.xml").text
}
else
{
    context.responsefile = new File(projectDir,"Test_Files/ID_List_CategoryB.xml").text
}

解决方法

我不熟悉Groovy,但我认为File是一个普通的java.io.File实例.

相对路径被解释为相对于应用程序的当前目录.尝试以下内容来验证:

def defaultPathBase = new File( "." ).getCanonicalPath()
println "Current dir:" + defaultPathBase

如果是这种情况,那么您可能希望使用new File(String parent,String child)构造函数,将您的资源目录作为第一个参数传递,将相对路径作为第二个参数传递.

例如:

// hardcoded for demonstration purposes
def pathbase = "/Users/chet"
def content = new File(pathbase,"Desktop/sample.txt").text

println content

这是执行脚本的结果:

Chets-MacBook-Pro:Desktop chet$groovy sample.groovy 
This is a sample text file.

It will be displayed by a Groovy script.
Chets-MacBook-Pro:Desktop chet$groovy sample.groovy 
This is a sample text file.

It will be displayed by a Groovy script.

Chets-MacBook-Pro:Desktop chet$

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读