Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Code dependencies within the method

Former Member
0 Likes
904

Hi

I am trying to write unit test for one method which is reading the data from database based on certain input.

For illustration purpose I am dividing my the code in the method into 3 sections.

METHOD <name>

"Section 1

< Here is the code which is building dynamic WHERE clause based on input, this WHERE clause is used in next

     section to read the data from database. >

"Section 2

    <  As I said before, here I am writing the SELECT statement which is using the above WHERE clause >

"Section 3

< Processing of data fetched from Section 2 >

ENDMETHOD.

In order to write the unit test for the above method I was told by my fellow developer to simulate the code in section 2 (simulate Database access by certain techniques).

But If I bypass the code in section 2 by using any simulation techniques, I am also indirectly bypassing the section 1 because the simulated data is used in section 3.

How do i verify that section 1 is building the WHERE clause properly ?

Can any one please give some inputs ?

By the way this is my second unit test.

3 REPLIES 3
Read only

Former Member
0 Likes
864

For more clarification,  since SELECT statement depends on the dynamic WHERE clause, by simulating database access we are making our unit test independent of the WHERE clause we built before SELECT statement.

I want to cover my unit test all parts of my code including the logic which is building WHERE clause.

Could any one please help me ?

Read only

Armin_Beil
Product and Topic Expert
Product and Topic Expert
0 Likes
864

How about own methods for section 1 and section 3?

METHOD <name>

data(l_where_condition) = get_where_condition(...).

select ... into @data(l_records) where (l_where_condition).

process_data(l_records).

ENDMETHOD.

Then you could test get_where_condition() and process_data() separately.

At least you can test whether your get_where_condition() creates certain strings for certain inputs. You may also use regex or the ABAP operator CP in case don't want to be completely strict but rather just want to ensure that certain values occur in the where clause.

If you wanted to test whether the generated where clause is syntactically and semantically completely correct and providing the right data once used in your SQL statement - I don't know how to do that without actually accessing the database.

Best regards,

Armin

Read only

0 Likes
864

Hello Beil,

Thanks for the reply.  I will try your idea but before that can we really break the class design for the sake of unit testing ? In that case, what if our method is part of interface ( contract) ?

Can you throw some light on this idea ?

What are the techniques that we have to tackle these kind of scenarios ?