Forum Discussion

tomaszarmata's avatar
tomaszarmata
Contributor
9 years ago
Solved

Extending classes in Script Library

Hello,

 

There is a way to extend class from other file in script library?

 

For example I have 2 files: class1.groovy and class2.groovy. And inside class2.groovy I want to write class2 which will extend class1 from class1.groovy file.

 

It is possible?

  • You can do every thing in groovy that you do in Java.

     

    I know you may use these classes in ReadyAPI-<version>/bin/script directory

    For eg:

    Person.groovy

     

    class Person {
      def firstname
      def lastname
      def age
    }

    Student.groovy

     

    import Person
    
    class Student extends Person {
       def class
       def department
    }
        

    You may have both in scripts directory or you can use them in groovy step as well.

     

    Say, below is your groovy script test step which shows how to use above created classes.

     

    import Student
    import Person
    
    def person = new Person(firstname: 'John', lastname: 'Miller')
    def student = new Student(firstname: 'Philips', lastname: 'Mark', class: 'B.S', department: 'Mathematics')

4 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    You can do every thing in groovy that you do in Java.

     

    I know you may use these classes in ReadyAPI-<version>/bin/script directory

    For eg:

    Person.groovy

     

    class Person {
      def firstname
      def lastname
      def age
    }

    Student.groovy

     

    import Person
    
    class Student extends Person {
       def class
       def department
    }
        

    You may have both in scripts directory or you can use them in groovy step as well.

     

    Say, below is your groovy script test step which shows how to use above created classes.

     

    import Student
    import Person
    
    def person = new Person(firstname: 'John', lastname: 'Miller')
    def student = new Student(firstname: 'Philips', lastname: 'Mark', class: 'B.S', department: 'Mathematics')
    • tomaszarmata's avatar
      tomaszarmata
      Contributor

      Nice!

       

      So all I have to do is only import first class into second class. Its all I want :)

       

      Thx, guys!

      • nmrao's avatar
        nmrao
        Champion Level 3
        Note that, you may have consider if you have packages along with it.