Search code examples
javaselenium-webdrivercucumbergherkin

Creating a Generic method to load dataSet in cucumber using Poiji library


I have a class that loaded data from scenario steps my first class is LoadUserStepDfn

public class LoadUserStepDfn extends LoadDataStepDfn<User> {

public LoadUserStepDfn(ReadingUserUsingPoiji readingUserUsingPoiji) {
    super.readingExcelUsingPoiji = readingUserUsingPoiji;
}

@Given("^Data is loaded from \"([^\"]*)\"$")
public void data_is_loaded_from (String filePath) throws Throwable {
    super.data_is_loaded_from(filePath);
}

and it call class named LoadDataStepDfn

public class LoadDataStepDfn<T> {

public List<T> data;

protected ReadingExcelUsingPoiji readingExcelUsingPoiji;

public void data_is_loaded_from (String filePath) throws Throwable {
    data = readingExcelUsingPoiji.TransformExcelToClass(filePath);

}

and here is my class that reads Excel and stores it to java class

public abstract class ReadingExcelUsingPoiji<T> {


    public List<T> TransformExcelToClass(String filePath){
        PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().addListDelimiter(";").build();
        List<T> data = Poiji.fromExcel(new File(filePath), getMyType(), options);
        return data;
    }

    public abstract Class<T> getMyType();

}

the problem that I want to use one class I don't want it to be abstract and use another one which is this class

    public class ReadingUserUsingPoiji extends ReadingExcelUsingPoiji<User> {


    public Class<User> getMyType(){
        return User.class;
    }

Solution

  • I am trying to understand here, so you dont want @override, but rather 1 method that returns you the type of class to transform to??

    Why can't it be that simple... You have a method that determines what class you should use to transform to...

    I dont understand why you are using generics...your logic doesnt seem to really care for it? Especially if you have 1 ReadingExcelUsingPoiji class..it really shouldnt care.

    public class ReadingExcelUsingPoiji<T> {
    
    public List<T> transformExcelToClass(String filePath, Class<T> classToTransformTo) {
        PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().addListDelimiter(";").build();
        List<T> data = Poiji.fromExcel(new File(filePath), classToTransformTo, options);
        return data;
    }
    
    
    public static void main(String [] args) {
        
        ReadingExcelUsingPoiji genericConverter = new ReadingExcelUsingPoiji();
        
        List<User> listOfUsers = genericConverter.transformExcelToClass("yourFilePath", User.class);
        List<Car> listOfCars = genericConverter.transformExcelToClass("yourFilePath", Car.class);
    }
    

    }

    public class LoadUserStepDfn extends LoadDataStepDfn<User> {
    
        
        
        @Given("^Data is loaded from \"([^\"]*)\"$")
        public void data_is_loaded_from (String filePath) throws Throwable {
            super.data_is_loaded_from(filePath , User.class);
        }
    
    }
    
    
    
    
    public class LoadDataStepDfn<T> {
    
        public List<T> data;
    
        protected ReadingExcelUsingPoiji readingExcelUsingPoiji;
    
        protected void data_is_loaded_from(String filePath, Class<T> classToTransformTo) throws Throwable {
            data = readingExcelUsingPoiji.transformExcelToClass(filePath, classToTransformTo);
    
        }
    
    }