我如何使用soapUI MockService的外部响应文件的相对路径 [英] How can I use relative paths to external response files for soapUI MockService

查看:157
本文介绍了我如何使用soapUI MockService的外部响应文件的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了什么



我正在使用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 /文本
}
else
{
context.responsefile = new File(Test_Files / ID_List_CategoryB.xml)。text
}

...请记住,soapUI项目xml文件驻留在C:/ soapProject /



我到目前为止所尝试过的



所以,这是行不通的。我尝试了相对路径的变体:
$ b $ ul
./ Test_Files / ID_List_CategoryA.xml

  • / Test_Files / ID_List_CategoryA.xml

  • Test_Files / ID_List_CategoryA.xml



  • 考虑项目文件父目录作为相对路径的根目录,因此也尝试了以下变化:


    • ./ 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类:脚本[n]错误。 Admittefly,当我试图这样做的时候,我真的摸不着头绪。



    我尝试使用这篇文章和其他人的信息:我如何使soapUI附件路径相对?



    ...没有任何运气。用mock代替test(其他更改),在该帖子的解决方案代码中导致更多的属性错误,例如

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

    ..导致。 ..

     没有这样的属性:mockRunner for class:Script3 



    我认为我需要的东西



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



    非常感谢任何帮助。谢谢。 Mark。



    解决方案 - 由 GargantuChet

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

      def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    def projectDir = groovyUtils.projectPath
    $ b $ 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
    }


    解决方案

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



    相对路径被解释为相对于应用程序的当前目录。尝试如下所示来验证:

      def defaultPathBase = new File(。).getCanonicalPath()
    printlnCurrent dir:+ defaultPathBase

    如果这是这种情况,那么您可能想要使用 新的File(String parent,String child) 构造函数,传递资源目录作为第一个参数,相对路径作为第二个参数。
    $ b

    例如:

      //用于演示目的的硬编码
    def pathbase = / Users / chet
    def content = new File(pathbase,Desktop / sample.txt)。text

    println content

    这是执行脚本的结果:

      Chets- MacBook Pro:桌面切换$ groovy sample.groovy 
    这是一个示例文本文件。

    它将通过Groovy脚本显示。
    Chets-MacBook-Pro:Desktop chet $ groovy sample.groovy
    这是一个示例文本文件。

    它将通过Groovy脚本显示。

    Chets-MacBook-Pro:Desktop chet


    What I've Done

    I am using soapUI (3.6.1 Free version) mock services to serve up specific data to 2 client applications I am testing. With some simple Groovy script I've set up some mock operations to fetch responses from specific files based on the requests made by the client applications.

    The static contents of the mock response is:

    ${responsefile}
    

    The groovy in the operation dispatch scripting pane is:

    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
    }
    

    In this example, when the client application issues a request to the mock service that contains the string CategoryA, the response returned by soapUI is the contents of file ID_List_CategoryA.xml

    What I'm Trying To Achieve

    This all works fine with the absolute paths in the groovy. Now I want to pull the whole collection of soapUI project file and external files into a package for easy re-deployment. From my reading about soapUI I hoped this would be as easy as setting the project Resource Root value to ${projectDir} and changing my paths to:

    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
    }
    

    ... keeping in mind that the soapUI project xml file resides in C:/soapProject/

    What I've Tried So Far

    So, that doesn't work. I've tried variations of relative paths:

    • ./Test_Files/ID_List_CategoryA.xml
    • /Test_Files/ID_List_CategoryA.xml
    • Test_Files/ID_List_CategoryA.xml

    One post indicated that soapUI might consider the project files parent directory as the root for the purposes of the relative path, so tried the following variations too:

    • ./soapProject/Test_Files/ID_List_CategoryA.xml
    • /soapProject/Test_Files/ID_List_CategoryA.xml
    • soapProject/Test_Files/ID_List_CategoryA.xml

    When none of that worked I tried making use of the ${projectDir} property in the groovy script, but all such attempts failed with a "No such property: mockService for class: Script[n]" error. Admittefly, I was really fumbling around when trying to do that.

    I tried using information from this post and others: How do I make soapUI attachment paths relative?

    ... without any luck. Replacing "test" with "mock," (among other changes), in the solution code from that post resulted in more property errors, e.g.

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

    .. led to...

    No such property: mockRunner for class: Script3
    

    What I Think I Need

    The posts I've found related to this issue all focus on soapUI TestSuites. I really need a solution that is MockService centric or at least sheds some light on how it can be handled differently for MockServices as opposed to TestSuites.

    Any help is greatly appreciated. Thanks. Mark.

    The Solution - Provided by GargantuChet

    The following includes the changes suggested by GargantuChet to solve the problem of trying to access the ${projectDir} property and enable the use of relative paths by defining a new projectDir object within the scope of the groovy script:

    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
    }
    

    解决方案

    I'm not familiar with Groovy, but I assume the File is a normal java.io.File instance.

    Relative paths are interpreted as being relative to the application's current directory. Try something like the following to verify:

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

    If this is the case here, then you may want to use the new File(String parent, String child) constructor, passing your resource directory as the first argument and the relative path as the second.

    For example:

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

    Here's the result of executing the 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 sample.groovy 
    This is a sample text file.
    
    It will be displayed by a Groovy script.
    
    Chets-MacBook-Pro:Desktop chet$ 
    

    这篇关于我如何使用soapUI MockService的外部响应文件的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆