Skip to Content

How to unit test a method that reads an excel file

I have implemented a static method which read excel file and exporting internal table with data. The definition of the method is similar to the following:

    CLASS-METHODS:
      read_file IMPORTING iv_file_name  TYPE localfile
                EXPORTING et_materials TYPE ztt_mm10_material
                RAISING   lcx_exception.

What is the best way to unit tests for this method?

In other programming languages, we can approach the topic in a different way but in ABAP, according to my knowledge, this is not possible. Here are examples of how to solve my problem in python or Java:
Unit Testing code with IO file operations (in Python)

You can create a file as part of the test, no need to mock it out.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Feb 09 at 10:49 AM

    Unit testing also involves coding appropriately in first place, which is why I would consider your code problematic. Your method does two things: Read excel and convert its contents into the table of your choice.

    I don't know how you read the Excel file, but I assume you end up with some form of intermediate table or class returned by whatever FM or class you use. So then in a subclass or alternate implementation you can replace the Excel read with data of your choice. I'm going to simplify it a bit by just hardcoding, but you can also store various datasets in ECATT:

    class lcl_conversion. 
      public section. 
        methods get_data_from_excel importing i_filename 
                                    returning result.
    private section. 
      methods read_excel. 
      methods convert_to_ztab.
    endclass.
    
    
    class lcl_conversion implementation.
    method get_data_from_excel. 
    result = convert_to_ztab( read_excel( i_filename ) ). 
    endmethod. ... endclass. class ltcl_conversion for testing. ... methods no_data for testing. methods valid_data for testing. methods invalid_data for testing. ... method no_data. data(converted_data) = convert_to_ztab( value #( ) ). cl_abap_unit_assert=>assert_initial( converted_data ). endmethod. method valid_data. data(excel_data) = value whatever( ( col = 1 row = 1 value = "Blah" ) ( col = 2 row = 1 value = ... ) ... ( col = 2 row = 3 value = 'XYZ' ) ). data(converted_data) = convert_to_ztab( excel_data ). cl_abap_unit_assert=>assert_equals( act = lines( converted_data ) exp = 2 ). "1 header and two data lines ... a few more asserts to check results. endmethod. method invalid_data. ... "Put some text into numeric fields or whatever try. data(converted_data) = convert_to_ztab( excel_data ). cl_abap_unit_assert=>fail( 'Exception was not raised' ). catch lcx_error. "As expected endtry. endmethod. endclass.
    Add comment
    10|10000 characters needed characters exceeded

  • Feb 09 at 11:25 AM
    Add comment
    10|10000 characters needed characters exceeded

    • Hello Horst,
      I thought I knew all the news from Netweaver 7.5 but you always surprise me with something new.
      "Test Seams and Test Injections" would solve my problem with testing private methods. Unfortunately, I work on the 7.4 system and I do not like the idea with a free-style test flag.