0% found this document useful (0 votes)
49 views

Using JUnit in SAP NetWeaver Developer Studio

Uploaded by

Bojan Stojković
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Using JUnit in SAP NetWeaver Developer Studio

Uploaded by

Bojan Stojković
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Using JUnit in SAP NetWeaver Developer Studio

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.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 1
Using JUnit in SAP NetWeaver Developer Studio

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

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 2
Using JUnit in SAP NetWeaver Developer Studio

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

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 3
Using JUnit in SAP NetWeaver Developer Studio

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() {

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 4
Using JUnit in SAP NetWeaver Developer Studio

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.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 5
Using JUnit in SAP NetWeaver Developer Studio

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.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 6
Using JUnit in SAP NetWeaver Developer Studio

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.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 7
Using JUnit in SAP NetWeaver Developer Studio

Create a JUnit TestCase. Name it as CoffeeMakerTest. Select setUp() and tearDown() methods.
Click Finish.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 8
Using JUnit in SAP NetWeaver Developer Studio

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;

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 9
Using JUnit in SAP NetWeaver Developer Studio

/**
* 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);
}

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 10
Using JUnit in SAP NetWeaver Developer Studio

/**
* @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;
}

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 11
Using JUnit in SAP NetWeaver Developer Studio

Running a Successful Test

Steps to be followed to Run a Test.

JUnit tool displays a green bar on successful Test run.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 12
Using JUnit in SAP NetWeaver Developer Studio

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);
}

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 13
Using JUnit in SAP NetWeaver Developer Studio

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.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 14
Using JUnit in SAP NetWeaver Developer Studio

Code snippet from our TestSuite

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 15
Using JUnit in SAP NetWeaver Developer Studio

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

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 16
Using JUnit in SAP NetWeaver Developer Studio

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.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com

© 2007 SAP AG 17

You might also like