简体   繁体   中英

I have Class file which has multiple methods, Can i invoke the class from Testcase

I have a class called TestingClass and I have a one more class called TestCase .

In TestingClass I have 4 methods which are dependent on each other, Can i call the this class file from my TestCase file and invoke all the methods at once?

You can try with extend the TestingClass and overwriting those methods as like,

public class TestCase extends TestingClass {
    @Override
    public void method1(){
       super.method1();
    }

    @Override
    public void method2(){
       super.method2();
    }

    @Override
    public void method3(){
       super.method3();
    }

    @Override
    public void method4(){
       super.method4();
    }
 }

Yes you can if they have visibility.

In your TestCase you have to declarate a variable of TestingClass and instantiate it. Then, if your methods are public you can use them.

Here is an example:

public class TestCase {

     private TestingClass testingClass = new TestingClass();

     @Test
     public void test1(){
         testingClass.methodFromTestCase();
     }
}

There is another way to do it, refer below example:

            package overridefunction;
            public class SuperClass 
                {
                public void method1()
                {
                    System.out.println("superclass method1");
                    this.method2();
                    this.method3();
                    this.method4();
                    this.method5();
                }
                public void method2()
                {
                    System.out.println("superclass method2");
                }
                private void method3()
                {
                    System.out.println("superclass method3");
                }
                protected void method4()
                {
                    System.out.println("superclass method4");
                }
                void method5()
                {
                    System.out.println("superclass method5");
                }
            }

        package overridefunction1;
            public class SubClass extends SuperClass
            {
                @Override
                public void method1()
                {
                    System.out.println("subclass method1");
                    super.method1();
                }
            }

            
            package overridefunction1;
            public class Demo 
            {
                public static void main(String[] args) 
                {
                    SubClass mSubClass = new SubClass();
                    mSubClass.method1();
                }
            }

            subclass method1
            superclass method1
            superclass method2
            superclass method3
            superclass method4
            superclass method5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM