Replace the abstract method with different parameters?

advertisements

I'd like to define an abstract method, in order to use this method already in my abstract class. Then, I'd like to have this abstract method defined in different classes that extend my abstract class.

But how could I do this?

(The goal is to provide some basic methods for Integration Tests, that all use the same "structure", but implement some different logic).

How can I define a abstract method WITH parameters in the abstract class, use this abstract method already in abstract class, but implement the method first in my test implementation?

public abstract class BaseIT {
    protected doVerify(String params..) {
        if (isValid()) {
            //basic verification for all extending testcases
        }
    }

    protected abstract boolean isValid(Object o);
}

public class TestA_IT extends BaseIT{
    @Test
    public void myTest {
        //do some specific stuff and verify with resulting params

        doVerify(params..);
    }

    protected boolean isValid(Object o) {
        return o.isValid();
    }
}

Please bear in mind that this is just a (silly) example of my problem. Of course I could just hand the result of isValid to the base class method, but that's not possibly like this in my specific case.

The problem is: Object o is a different object for the different testcase implementations.

I may not use interfaces for this. How can I make this possible?


You could do:

public class TestA_IT extends BaseIT {
    @Test
    public void myTest {
        //do some specific stuff and verify with resulting params

        doVerify(params..);
    }

    protected boolean isValid(Object o) {
        return (o instanceOf YourObject) ? ((YourObject)o).isValid() : false;
    }
}

A cooler way to do this is to implement a Visitor Pattern, but I think you'll be fine like this.