Prerequisite for reading this blog: you should be familiar with how to work with ABAP unit test framework. Requirement is to write unit test for this method below:
In this blog, I copy the standard class CL_PRDTXT_TEXTCUCO_CN02 into a local class ZCL_PRDTXT_TEXTCUCO_CN02 and generate test class based on the latter. The same approach could be applied to the standard class for sure. Create local unit test class for it via wizard:
Here below is automatically generated code:
Since usually we will manipulate private attributes of CUT ( class under test ) so insert the following code into header part of local test class, so that local test class can modify private attributes of CUT.
First attempt
When performing unit test for the first time, the statement coverage is extremely low:
Check the source code of tested method, the reason is in our local test class, we didn’t pass a valid value for parameter FOCUS_BO, so the method directly return when its first line is executed.
Solution
I plan to pass a fake bol entity for parameter FOCUS_BO. Then I construct it in method class_setup:
METHOD class_setup.
lo_prod = zcl_prod_unit_test_tool=>get_fake_bol_entity(
iv_bol_name = 'Product'
is_data = get_sample_data( )
iv_key = get_sample_data( )-product_guid ).
ENDMETHOD.
METHOD get_sample_data.
rs_data = VALUE #( product_guid = '0123456789123456' product_id = 'I042416' product_type = '01' ).
ENDMETHOD.
Now the local test class method is changed as below:
METHOD on_new_focus.
DATA focus_bo TYPE REF TO if_bol_bo_property_access.
focus_bo ?= lo_prod.
f_cut->on_new_focus( focus_bo ).
ENDMETHOD.
Second attempt
When I run unit test, it fails this time with following error message:
The reason is in line 42, the method being tested tries to read product with a valid guid, unfortunately in my test code I have passed a fake guid ‘0123456789123456’, thus not_found exception is raised.
Solution
Change get_sample_data in local test class as below, which can ensure the product_guid is always valid since it is read from DB table.
METHOD get_sample_data.
DATA:ls_prod TYPE comm_product.
SELECT SINGLE * INTO ls_prod FROM comm_product WHERE product_type = '01'.
rs_data = VALUE #( product_guid = ls_prod-product_guid product_id = ls_prod-product_id product_type = '01' ).
ENDMETHOD.
Third attempt
Now the unit test could pass successfully, however still some statement is not executed at all.
Those unreached statements are marked as red. The reason is there is no entity contained in collection wrapper.
Solution
Create a new method in test class:
METHOD create_wrapper.
DATA: lr_attr TYPE REF TO crmst_uiu_text_attr.
CREATE DATA lr_attr.
DATA(lr_value) = NEW cl_bsp_wd_value_node( lr_attr ).
CREATE OBJECT f_cut->collection_wrapper.
f_cut->collection_wrapper->add( lr_value ).
ENDMETHOD.
And call it in set_up method:
METHOD setup.
CREATE OBJECT f_cut.
create_wrapper( ).
ENDMETHOD.
Finally, the unit test is finished successfully and all executable statements have been covered:
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关推荐
在SAP CRM 7.0版本中,WebClient UI配置是一项关键任务,它涉及到了用户界面的定制和优化,以提升用户体验和业务效率。WebClient UI是SAP CRM的一个核心组件,提供了一个基于Web的交互界面,使得用户能够访问和操作...
这是cross component navigation在起作用。 Result list是model在ICQR里面,如果点service,会navigate到service detail page,这个service detail page是做在PRD02OV上,所以这个navigation是cross component ...
WebClient类提供了DownloadData、DownloadFile等方法来下载数据或文件。例如,下载文件到指定路径可以使用以下代码: ```csharp using (WebClient client = new WebClient()) { client.DownloadFile(...
通过这个过程,你不仅学会了如何在SAP CRM WebUI中创建下拉列表,还了解了值帮助方法和接口在SAP UI开发中的重要性。这是提高用户界面功能性和用户体验的关键步骤,对于任何WebUI开发者来说都是必备技能。
首先,让我们来看看SAP CRM WebClient UI的发展历程。从最初的GUI界面,到PCUI,再到IC WebClient,最终发展到目前最新的WebClient UI,这一过程中,用户界面不断优化,以适应不同设备和用户的使用需求。在CRM 7.0...
Sent: Thursday, May 9, 2013 6:30 PM Subject: ON_NEW_FOCUS的用途 ROOT ———————————— Product ID PRODshortText ———————————— Product Description Genil Model hierarchy
在SAP CRM基础培训中,WebClient UI(Web客户端用户界面)是一个重要的学习焦点,它是用户与系统交互的主要界面。下面我们将深入探讨WebClient UI的相关知识点。 WebClient UI的发展历程是从GUI(图形用户界面)...
开发者可能在这个类中重写了WebClient的某些方法,比如`DownloadDataAsync`和`DownloadFileAsync`,以便在下载过程中能够实时更新UI状态。 `Form1.Designer.cs`和`Form1.cs`是主应用程序窗体的相关文件,Designer....
SAP CRM的基础知识通常包括对于其核心组件的理解,包括主数据(如账户、组织模型、产品等),交易处理、定价、开票、CRM中间件以及CRM WebClient UI。CRM中间件主要负责不同系统组件之间的信息交换,而CRM WebClient...
在C#编程中,WebClient类提供了一种简单的方式来实现文件下载。WebClient是一个高度封装的网络通信类,主要用于HTTP协议交互,包括上传和下载数据。以下是对标题和描述中涉及知识点的详细解释: 1. **WebClient下载...
C#语言提供了多种方法来实现这一功能,其中`WebClient`类就是一种非常简便的方式。本文将详细介绍如何使用`WebClient`类上传文件,并对代码进行深入解析。 #### 二、`WebClient`类简介 `WebClient`是.NET Framework...
public class WebClientDemo { public void DownloadWithProgress(string url, string localPath) { using (var client = new WebClient()) { // 订阅下载进度改变事件 client.DownloadProgressChanged += ...
UI Guidelines for CRM WebClient User Interface
【标题】:“WEBCLIENT使用方法” 在C#编程中,`WebClient`是一个非常重要的类,它位于`System.Net`命名空间下,主要用于处理HTTP请求,执行下载或上传操作。`WebClient`类提供了简单易用的方法来访问Web资源,如...
### WebClient用法详解 在C#编程语言中,`WebClient` 类是处理网络操作的一种简单方式,尤其适用于HTTP协议的交互。它提供了一个高级、易于使用的接口来发送HTTP请求和接收HTTP响应。 #### 一、`WebClient`类简介 ...
通过WebClient的UploadStringAsync或UploadDataAsync方法,我们可以轻松地实现POST请求。例如,要发送JSON数据,我们可以使用UploadStringAsync方法,将数据作为字符串传递。 以下是一个简单的使用WebClient进行...
例1:使用WebClient中的UploadFile方法上传文件。代码如下。使用此方法需要将上传的文件夹权限设置为 IIS来宾账户允许读写。 例2:使用WebClient的UpLoadData上传文件 例3:使用WebClient的Write方法上传。 例4:...
【WebClientDemo】是一个示例项目,展示了如何在Eclipse集成开发环境中使用`WebClient`进行网络通信。`WebClient`是Java中的一个类,主要用在Spring Framework的WebFlux模块,它允许开发者发送HTTP请求并处理响应,...
本教程将详细解释`WebClient.DownloadFileAsync`方法,这是一个异步下载文件的功能,适用于C#开发者,特别是初学者。我们将探讨异步编程的基本概念,`DownloadFileAsync`的工作原理,以及如何在实际项目中应用。 1....
WebClient安装文件。 在Java世界中,有许多用于发送HTTP请求的库。比较三种流行的HTTP客户端:WebClient,HttpClient和OkHttp。WebClient是Spring 5中引入的非阻塞,反应式的Web客户端。它是在Project Reactor和...