<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: ABAP unit test for updating a database table successfully in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-unit-test-for-updating-a-database-table-successfully/m-p/766924#M37720</link>
    <description>&lt;P&gt;What is your ABAP release? (from 7.52 there is the &lt;A href="https://help.sap.com/viewer/c238d694b825421f940829321ffa326a/7.52.0/en-US/3301a92218f8429fbbff8fcb982f7692.html"&gt;Open SQL Test Double Framework&lt;/A&gt;)&lt;/P&gt;</description>
    <pubDate>Wed, 13 Feb 2019 09:39:56 GMT</pubDate>
    <dc:creator>Sandra_Rossi</dc:creator>
    <dc:date>2019-02-13T09:39:56Z</dc:date>
    <item>
      <title>ABAP unit test for updating a database table successfully</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-unit-test-for-updating-a-database-table-successfully/m-p/766922#M37718</link>
      <description>&lt;P&gt;I am working on ABAP unit test. There is a method that imports some parameters and then update a database table. how can I test whether the update of this method is successful? Thank you! &lt;/P&gt;</description>
      <pubDate>Tue, 12 Feb 2019 20:48:56 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/abap-unit-test-for-updating-a-database-table-successfully/m-p/766922#M37718</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2019-02-12T20:48:56Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP unit test for updating a database table successfully</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-unit-test-for-updating-a-database-table-successfully/m-p/766923#M37719</link>
      <description>&lt;P&gt;On 7.5x (where x = 2, I think), there's a way of telling the kernel this is a test, and instead of writing to the actual database, it reads and writes to an area of memory (entirely transparently to you) instead.&lt;/P&gt;&lt;P&gt;Before then:&lt;/P&gt;&lt;P&gt;I create a local interface &lt;STRONG&gt;lif_db_accessor&lt;/STRONG&gt;. I define methods that mirror the sql statements. So for &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT field1 field2 FROM table INTO some_Structure WHERE field3 = some_value and field4 = some_other_value,&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt; I have a method:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;METHOD get_stuff
IMPORTING i_field1 TYPE ... i_field2 TYPE... i_field3 TYPE ... i_field 4 TYPE ...
RETURNING (VALUE) r_something TYPE some_structure.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then I implement &lt;STRONG&gt;lif_db_accessor&lt;/STRONG&gt; in a local class &lt;STRONG&gt;lcl_db_accessor&lt;/STRONG&gt; with the actual SQL.&lt;/P&gt;&lt;P&gt;For my main class, I define&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CLASS-DATA _db_accessor TYPE REF TO lif_db_accessor.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;You'll need somewhere (look it up)&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;INTERFACE lif_db_accessor DEFERRED.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;in the &lt;STRONG&gt;class_constructor&lt;/STRONG&gt; I have a statement.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;_db_accessor = new zcl_db_accessor( ).&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now, you need to make your&lt;STRONG&gt; ltc_test_main &lt;/STRONG&gt;test class a LOCAL FRIEND of your main class.&lt;/P&gt;&lt;P&gt;Create a test double &lt;STRONG&gt;ltd_db_accessor&lt;/STRONG&gt; that implement &lt;STRONG&gt;lif_db_accessor.&lt;/STRONG&gt; Here, you implement all the methods of the interface, but instead of to a db table, to an internal table. So if it was MARA, you'd have something like.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CLASS ltd_db_accessor DEFINITION.
PUBLIC SECTION.
  INTERFACE lif_db_accessor.
  DATA db TYPE HASHED TABLE OF mara WITH UNIQUE KEY matnr.ENDCLASS.

CLASS ltd_db_accessor IMPLEMENTATION.
  METHOD lif_db_accessor~get_mtart.
    " SELECT SINGLE mtart FROM MARA INTO r_mtart WHERE matnr EQ i_matnr.
    READ TABLE db INTO data(db_Record) WITH TABLE KEY matnr = i_matnr.
    r_mtart = db_Record-mtart.
  ENDMETHOD.
ENDCLASS.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In the definition of your test class you have&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;DATA:
  cut TYPE REF TO main class.
  db TYPE REF TO ltd_db_accessor.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In the setup method you have&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;me-&amp;gt;cut = new #( ).
me-&amp;gt;db = new #( ).
me-&amp;gt;cut-&amp;gt;_db_accessor = me-&amp;gt;db.
me-&amp;gt;db = value #( ...db values... ).&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now, whenever your code under test use the db accessor, instead of using&lt;STRONG&gt; lcl_db_accessor &lt;/STRONG&gt;and getting data from the database, it will get the data from your &lt;STRONG&gt;ltd_db_accessor db&lt;/STRONG&gt; attribute.&lt;/P&gt;&lt;P&gt;I'm preparing a blog on this very subject as I've been doing the above rather a lot recently.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Feb 2019 21:13:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/abap-unit-test-for-updating-a-database-table-successfully/m-p/766923#M37719</guid>
      <dc:creator>matt</dc:creator>
      <dc:date>2019-02-12T21:13:40Z</dc:date>
    </item>
    <item>
      <title>Re: ABAP unit test for updating a database table successfully</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/abap-unit-test-for-updating-a-database-table-successfully/m-p/766924#M37720</link>
      <description>&lt;P&gt;What is your ABAP release? (from 7.52 there is the &lt;A href="https://help.sap.com/viewer/c238d694b825421f940829321ffa326a/7.52.0/en-US/3301a92218f8429fbbff8fcb982f7692.html"&gt;Open SQL Test Double Framework&lt;/A&gt;)&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 09:39:56 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/abap-unit-test-for-updating-a-database-table-successfully/m-p/766924#M37720</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2019-02-13T09:39:56Z</dc:date>
    </item>
  </channel>
</rss>

