Using JUnit in SAP NetWeaver Developer Studio



Similar documents
Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

JUnit. Introduction to Unit Testing in Java

Slides prepared by : Farzana Rahman TESTING WITH JUNIT IN ECLIPSE

+ Introduction to JUnit. IT323 Software Engineering II By: Mashael Al-Duwais

Step by Step Guide How to Copy Flat File from Other Application Server to BI and Load through Info Package

Unit Testing and JUnit

Unit Testing. and. JUnit

Creating Transaction and Screen Variants

Step by Step Guide for Language Translation Tool

Step by Step Procedure to Block and Debug a CIF Queue Flowing from R/3 to APO System

SAP FI - Automatic Payment Program (Configuration and Run)

SAP CRM 7.0 E2C Setup: CRM via Toolset

Query OLAP Cache Optimization in SAP BW

Table of Contents. LESSON: The JUnit Test Tool...1. Subjects...2. Testing What JUnit Provides...4. JUnit Concepts...5

Approach of Unit testing with the help of JUnit

Tutorial - Creating Pop Up Window Using New Features in WebDynpro Java CE 7.1 Ehp1

Web Application Designer for Beginners

Creating New Unit of Measure in SAP BW

Deploying Crystal Reports on Top of a SAP BI Query

Effective unit testing with JUnit

ABAP Proxy Interfacing

How to Integrate CRM 2007 WebClient UI with SAP NetWeaver Portal

Extractor in R/3 and Delta Queue

Configuration of Enterprise Services using SICF and SOA Manager

Step by Step guide of Report-to- Report Interface in BW Reporting

Reverse Transport Mechanism in SAP BI

Working with SAP BI 7.0 Data Transfer Process (DTP)

Order Split Usage in Production Orders

Configuring HTTPs Connection in SAP PI 7.10

LSMW: Upload Master Data using Batch Input Recording

Restricting Search Operators in any Search View

Agile.NET Development Test-driven Development using NUnit

Java Unit testing with JUnit 4.x in Eclipse

Testing, Debugging, and Verification

How to Create an ecatt?

BW Performance Monitoring

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

SAP NetWeaver Developer Studio 7.30 Installation Guide

Guidelines for Effective Data Migration

Going Interactive: Combining Ad-Hoc and Regression Testing

Exposing RFC as Web Service and Consuming Web Service in Interactive Forms in ABAP

Display Options in Transaction SE16

Workflow Troubleshooting and Monitoring in SAP ECC 6.0

SAP CRM 7.0 for Newbies: (Part 1) Simple BOL Object Creation for CRM Webclient UI

Forgot or Lock "Administrator or J2EE_ADMIN" Password

Organizational Management- Organizational Structure Creation

Creating Content Using SO10 Objects and Text Symbols

How To Test In Bluej

How to Generate Stack Xml for Ehp4 and Above Upgrade

How to Modify, Create and Delete Table Entries from SE16

Deleting the User Personalization done on Enterprise Portal

Currency Conversion using Variables in SAP BI -Reporting

Unit Testing JUnit and Clover

Test Driven Development

SAP NetWeaver Portal Development Kits for.net and Java

Step by Step Guide to Archiving and Deleting of XML Messages in SAP NetWeaver PI

Data Flow from LBWQ/SMQ1 to RSA7 in ECC and Delta Extraction in BI

Web Dynpro ABAP: ALV and Table in Popup Window

SAP CRM System 6.0/7.0. For more information, visit the Customer Relationship Management homepage

SAP CRM Campaign Automation

Deleting the Requests from the PSA and Change Log Tables in Business Intelligence

ABAP Debugging Tips and Tricks

Quick Viewer: SAP Report Generating Tool

Installation Guide of the Change Management API Reference Implementation

Fail early, fail often, succeed sooner!

TESTING WITH JUNIT. Lab 3 : Testing

Unit-testing with JML

Understanding BEx Query Designer: Part-2 Structures, Selections and Formulas

Message handling in SAP CRM Web UI

Standard SAP Configuration of SMS through HTTP with Third Party SMS Gateway

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Table of Contents. Passing Data across Components through Component Controller between Two Value Nodes

Creating Web Service from Function Modules/BAPIs & Integrating with SAP Interactive Forms

Test-Driven Development

SAP BW - Excel Pivot Chart and Pivot Table report (Excel)

SAP CRM-BW Adapter an Overview

Multi Provider Creation Based on Sales and Planning Info Cubes

UNIT TESTING. Written by Patrick Kua Oracle Australian Development Centre Oracle Corporation

Unit testing with JUnit and CPPUnit. Krzysztof Pietroszek

Web Dynpro: Multiple ALV Grids and Layouts in ALV

Configuration and Utilization of the OLAP Cache to Improve the Query Response Time

Transfer of GL Master from Source SAP System to a Target SAP System through IDOCS

Create Automatic Mail Notification/ Alert for Process Chain Monitoring

Direct Subcontracting Process (SAP SD & MM)

Business Scenario Using GP/Web Dynpro with Back and Forth 3 Level Process and Dynamic Approvers in a Loop

Embedding Crystal Reports inside ECC ALV Reports

A Step-by-Step guide on SMTP Configuration and File to Mail Scenario Using Process Integration Scenario in PI 7.1

Creation and Configuration of Business Partners in SAP CRM

ALE Settings, for Communication between a BW System and an SAP System

Different Types of Alerts for Process Chains. Table of Content

SPDD & SPAU Adjustments Handbook

Understanding DSO (DataStore Object) Part 1: Standard DSO

Step by Step Procedure to Create Broadcasters, to Schedule and to Enhance of SAP- BI Queries from Query Designer

A Practical Guide to Test Case Types in Java

ABAP How To on SQL Trace Analysis

Data Extraction and Retraction in BPC-BI

Transcription:

Using JUnit in SAP NetWeaver Developer Studio Applies to: This article applies to SAP NetWeaver Developer Studio and JUnit. Summary Test-driven development helps us meet your deadlines by eliminating debugging time, minimizing design speculation and re-work, and reducing the cost and fear of changing working code. JUnit is an open source, regression-testing framework for Java that developers can use to write unit tests as they develop systems. Author: Kousik Mukherjee Company: HCL Technologies Ltd., Kolkata Created on: 11 June 2007 Author Bio Kousik Mukherjee is working as a Lead Consultant for HCL Technologies Ltd., Kolkata. 2007 SAP AG 1

Table of Contents Applies to:...1 Summary...1 Author Bio...1 Introduction...3 Pre Requisites...3 Overview of JUnit Framework...3 JUnit Goals...3 JUnit API...3 Test-First Programming & Test-Driven Development...4 Writing a Test...4 JUnit in NetWeaver Developer Studio...5 CoffeeMakerTest...9 Running a Successful Test... 12 Test Failure... 13 Creating Test Suite... 14 Related Content...16 Disclaimer and Liability Notice... 17 2007 SAP AG 2

Introduction Programming bugs have enormous costs: time, money, frustrations, and even lives. It is possible to lessen as much of these pains as possible by creating and continuously executing test cases for our software. In this article I will try to explain a practical and common approach to address programming defects before they make it past our local development environment. For years now, unit testing has been used by software developers as a process of verification of their programs. But there have been always some doubts in the minds of the programmers about who should write the tests. Apparently the answer is, test cases should be easy to write and execute and can be written and used by the programmers in spite of their busy schedules. Programmers need such kind of tool and JUnit is just the right one. Originally written by Erich Gamma and Kent Beck, JUnit is a simple, open source framework to write and run repeatable tests. It is an instance of the xunit architecture for unit testing frameworks and the de facto standard unit testing API for Java Development. SAP NetWeaver Developer Studio provides different test frameworks for testing J2EE application. The integration of JUnit testing framework saves time and effort when performing functional unit testing of code. You can write your own unit tests and embed them in JUnit, thus building a comprehensive test suite incrementally. Pre-requisites It is assumed that the reader of this article has knowledge of: SAP NetWeaver Developer Studio Writing Test cases Overview of JUnit Framework JUnit Goals Easy to write Test code should contain no extraneous overhead. Easy to learn to write Minimize the barriers to test writing, because the target audience for Junit is programmers who are not usually professional testers. Quick to execute Tests should run fast so that they can be run hundreds times per day. Easy to execute Tests should run at the touch of a button and present their results in a clear and unambiguous format. Isolated Tests should not affect each other. If the order in which the tests are run changes, the results shouldn t change. Composable Should be able to run any number of tests together. JUnit API JUnit has a simple API: subclass TestCase for your test cases and calls asserttrue(), assetequals(), assertnull(), or assertnotnull() from time to time. Most of the time you will encounter these five classes or interfaces. Assert A collection of static methods for checking actual values against expected values. An assert statement enables programmers to test their assumptions about their program. Each assert contains a boolean expression that you believe to be true when the assertion execute. If it is not true, the system will throw an error. By verifying that the boolean expression is indeed true, the assertion confirms the 2007 SAP AG 3

assumptions about the behavior of the program, increasing your confidence that the program is free of errors. Test The interface of all objects that act like tests. TestCase A single test. TestSuite A collection of tests. TestCase and TestSuite are the two most prominent implementers of Test. Your test case classes will inherit from junit.framework.testcase. Most of the time tests are run test suites containing a bunch of test cases, all of which are run when the test suite is run. Since it is composite in nature, a suite can contain suites which can contain suites and so on, making it easy to combine tests from various sources and run them together. TestResult A summary of the results of running one or more tests. While you are running all these tests you need somewhere to store all these results: how many tests ran, which failed, and how long they took. TestResult collects results. A single TestResult is used to collect results of all the tests running at one go. When test runs or fails, the fact is noted in the TestResult. At the end of the run, the TestResult contains a summary of all tests. A graphical test runner might observe the TestResult and update a progress bar every time a test starts. JUnit distinguishes between failures and errors. A failure is a violate JUnit assertion. Test-First Programming & Test-Driven Development You can use JUnit to write tests once you are done programming. Remember that tests are more valuable the closer they are written in time to time when an error might have been introduced. So instead of writing tests months after the code is complete, you can write tests days or hours or minutes after the possible introduction of a defect. You can write tests before the possible introduction of a defect. Writing a few lines of code, then a test that should run, or even better, to write a test first that won't run, then write the code that will make it run. This leads to Test-Driven Development. Writing a Test A Test class must extend TestCase A Test method must begin with test package com.hclt.test; public class CollectionTest extends TestCase { private ArrayList list; /** * Before test * setup() allocates Java Objects **/ public void setup() { list = new ArrayList(); /** Test **/ public void testcollection() { 2007 SAP AG 4

/** asserttrue(list.isempty()); * After test * If you allocate many megabytes of objects in setup(), you may want to * clear the variables pointing to those objects so that they can be * garbage-collected because the test case objects are not * garbage-collected at any predictable time **/ public void teardown() { list.clear(); JUnit in SAP NetWeaver Developer Studio Launch NetWeaver Developer Studio Create a new java project. To create a new project follow the below mentioned steps From the File menu, select New... Project. The New Project window is displayed. Select Java, and then Create a Java Project. Click Next. You can select J2EE and create an EJB Module Project or Web Module Project also (I have used Java Project for my example). Enter a name for the project viz. junit_demo. Click next. Java Settings window is displayed. In this window select the Libraries tab and add external jars. Add junit.jar. Click Finish. 2007 SAP AG 5

Right click on your project Select New Package. Create a new package named com.hclt.coffeebeans. Use this package to write your all java classes. 2007 SAP AG 6

Create another package named com.hclt.coffeebeans.test. Use this package to write all your tests. Right click on the com.hclt.coffeebeans.test Select New Other. Select Java JUnit TestCase. Click Next. 2007 SAP AG 7

Create a JUnit TestCase. Name it as CoffeeMakerTest. Select setup() and teardown() methods. Click Finish. 2007 SAP AG 8

CoffeeMakerTest The TestCase named CoffeeMakerTest is explained below. This TestCase is generated by JUnit tool in NetWeaver Developer Studio. package com.hclt.coffeebeans.test; import junit.framework.testcase; import com.hclt.coffeebeans.coffeemaker; import com.hclt.coffeebeans.recipe; public class CoffeeMakerTest extends TestCase { private CoffeeMaker cm; private Recipe r1; 2007 SAP AG 9

/** * Constructor for CoffeeMakerTest. * @param arg0 */ public CoffeeMakerTest(String arg0) { super(arg0); /** * @see TestCase#setUp() */ protected void setup() throws Exception { cm = new CoffeeMaker(); i = cm.checkinventory(); r1 = new Recipe(); r1.setname("coffee"); r1.setprice(50); r1.setamtcoffee(6); r1.setamtmilk(1); r1.setamtsugar(1); r1.setamtchocolate(0); /** * test method to add recipe */ public void testaddrecipe() { asserttrue(cm.addrecipe(r1)); assertnotnull(cm); 2007 SAP AG 10

/** * @see TestCase#tearDown() */ protected void teardown() throws Exception { super.teardown(); setup() - instantiates a new CoffeeMaker and a Recipe and adds ingredients to Recipe. testaddrecipe() asserts true that recipe r1 is added to coffeemaker cm. It also asserts that coffeemaker cm is not a null object as r1 is already added to it. We can keep on adding tests to this TestCase viz. we are going to add testeditrecipe() to the TestCase to create a new Recipe. public void testeditreceipe() { r2 = editrecipe(); asserttrue(cm.addrecipe(r2)); asserttrue(cm.editrecipe(r1,r2)); assertnotnull(cm); public Recipe editrecipe(){ r2 = new Recipe(); r2.setname("nescafe"); r2.setprice(85); r2.setamtcoffee(4); r2.setamtmilk(1); r2.setamtsugar(2); r2.setamtchocolate(0); return r2; 2007 SAP AG 11

Running a Successful Test Steps to be followed to Run a Test. JUnit tool displays a green bar on successful Test run. 2007 SAP AG 12

Test Failure The whole idea behind running tests is to write tests that will fail. Fix the defects of your programs and the test will be successful. So a code like this will fail. public void xtestaddrecipe() { asserttrue(cm.addrecipe(r1)); assertnotnull(cm); public void testeditreceipe() { r2 = editrecipe(); asserttrue(cm.editrecipe(r1,r2)); assertnotnull(cm); /* * @see TestCase#tearDown() */ protected void teardown() throws Exception { cm.deleterecipe(r1); 2007 SAP AG 13

The test results are displayed and the root for the failure can be traced. Making logical corrections to test assures that the programs on which the test is being run are correct in nature unless there is some incident occurs like the data has been deleted and so on. Tests are used to track and minimize very common exceptions in programs related to Null Pointer and Data Availability. Note: Add x in front of test method name to make the method inactive i.e. this method will not be taken into consideration when the test runs. Creating Test Suite Select New Project Other Java JUnit TestSuite Enter TestSuite name. Select the classes required to be included in the Suite. Click Finish. 2007 SAP AG 14

Code snippet from our TestSuite 2007 SAP AG 15

Select CoffeeBeanTests. Click Run Run As JUnit Test. Related Content The Philosophy of Unit Tests Developing J2EE Applications Integration Unit Testing on SAP NetWeaver Application Server 2007 SAP AG 16

Disclaimer and Liability Notice This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document. 2007 SAP AG 17