SoapUI groovy脚本错误:没有这样的属性:类的日志

时间:2015-02-25 04:41:07

标签: groovy soapui modularity

我正在尝试在soapui里面的groovy中编写一个模块化代码。 我创建了一个类示例,其中包含一个带有log.info语句的方法hello()。

我正在创建这个类的实例来调用函数hello。 在运行脚本时,我收到以下错误。

  

groovy.lang.MissingPropertyException:没有这样的属性:类的日志:示例

需要帮助。

class Example{

  void hello(){
    log.info '    >>>>    Hello World';
  }

}

Example example = new Example();
example.hello();

6 个答案:

答案 0 :(得分:2)

以下是完整脚本的示例。这里的log对象是成员变量,并使用带有map的Constructor进行初始化。请注意,此类也存在于脚本本身中。如果它是可重用的,则必须在每个脚本中复制它

class TestLogging {
  def log

  def sayHello(def name){
   log.info "Hello $name"
  }
}

def testLogging = new TestLogging(log:log)
testLogging.sayHello('Mr. ABC')

通常当我编写一些groovy库类时,我使用以下方法定义类,创建库并访问SOAPUI提供的变量,以及如何从脚本调用。这将显示类定义的可重用性,重用每个地方:

TestRunnerHelper.groovy - 编译&创建一个jar文件并将其放在SOAPUI_HOME / bin / ext目录下

class TestRunnerHelper {
  def context
  def testRunner
  def log

  def printTestDetails() {
    log.info 'Name of the test case is :'+testRunner.testCase.name
    log.info 'Name of the test suite is : '+testRunner.testCase.testSuite.name
  }
}

现在在你的任何项目中编写一个脚本 - > testsuite->测试用例 - >使用上面的类

进行Groovy Script测试步骤
def testHelper = new TestRunnerHelper(context:context, log:log, testRunner:testRunner)
testHelper.printTestDetails()

注意:如果你在groovy类中有一些包名,那么它也应该在脚本中导入。

我想通过第二个例子传达的是你可以在你喜欢的IDE中编写库类(我写了groovy,但也可以使用java)并利用soapui提供的变量context,log,testRunner变量通过他们到你的班级。

答案 1 :(得分:1)

这是直接而简单的方法。

  1. 在类中将对象声明为静态变量。

    def静态日志;

  2. 在课外(示例)用日志初始化它。

    Example.log = log;

  3. 以下是完整的代码。

    Example.log = log;
    Example.hello();
    
    class Example{
    
    def static log;
    
        def static hello(){
            log.info ("Hello World...");
        }
    }
    

    在非静态方法的情况下。创建一个类引用并通过它调用该函数。 E.g。

    Example ex = new Example;
    ex.hello();
    

答案 2 :(得分:0)

或者你可以移出log.info,仍然这样做。事情。

class Example{

  String hello(){
    return  '    >>>>    Hello World';
  }

}

Example example = new Example();
log.info example.hello();

答案 3 :(得分:0)

找到解决方案。 这里是。 1.需要把课程放在上下文中。 2.需要将log作为对象传递给函数。

希望这可以帮助其他人寻找类似的解决方案。

class Example{

  void hello(Object log){
    log.info '    >>>>    Hello World';
  }

}

context = new Example();
context.hello(log);

答案 4 :(得分:0)

以下是在类函数中添加日志并从其他(外部)脚本调用此函数的解决方案。

在SoapUI中创建一个Groovy步骤(TestLogging)并添加以下代码:

class TestLogging
{
   def log;
   def context;
   def testRunner;

    def sayHello()
      {
          log.info ("Hello ");
    }

}

context.setProperty("testClass", new TestLogging(log:log,context:context,testRunner:testRunner));

添加另一个脚本并添加以下代码以添加TestLogging类的功能:

tSuite = testRunner.testCase.testSuite.project.testSuites["TestSuiteName"]

module = tSuite.testCases["TestCaseName"].testSteps["TestLogging"]

// initialise the tSuite; which places an instance of TestLogging in the context
module.run(testRunner, context)

// get the instance of TestLogging Class from the context.
def testClass= context.testClass

//call function
testClass.sayHello();

答案 5 :(得分:0)

您需要使用类名定义log,并且您应该在类中声明logstatic,如下所示:

Example.log = log    
class Example {    
  def static log    
  public void hello()            
  {        
    log.info '    >>>>    Hello World';        
  }  
}        

Example example = new Example();      
example.hello();