我做了什么
我正在使用soapUI(3.6.1 免费版)模拟服务向我正在测试的2个客户端应用程序提供特定数据。通过一些简单的 Groovy 脚本,我设置了一些模拟操作,以根据客户端应用程序发出的请求从特定文件中获取响应。
模拟响应的静态内容是:
${responsefile}
操作调度脚本 Pane 中的常规内容是:
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 的内容
我想要实现的目标
这一切都适用于常规中的绝对路径。现在我想将整个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} 属性,但所有此类尝试都失败,并出现“No such property: mockService for class: Script[n]”错误。坦白说,当我尝试这样做时,我真的很摸索。
我尝试使用这篇文章和其他文章中的信息: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 为中心的解决方案,或者至少阐明如何以与 TestSuites 不同的方式对 MockServices 进行处理。
非常感谢任何帮助。谢谢。马克。
解决方案 - 由 GargantuChet 提供
以下内容包括 GargantuChet 建议的更改通过在 groovy 脚本范围内定义一个新的 projectDir 对象来解决尝试访问 ${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$
关于groovy - 如何使用soapUI MockService 的外部响应文件的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12266525/