Using JUnit to unit test EJBS in VisualAge for Java

Source: Internet
Author: User
Tags naming convention

Introduction

This article is intended for visualage®for Java™ users who want to unit test their EJB and develop test cases for these ejbs. This article is based on the visualage for Java 3.5.3 and JUnit 3.7. This article describes JUnit, the difficulties in unit testing of EJBS, and the related steps involved in developing test cases.

Unit tests are written in the programmer's perspective. Unit tests ensure that a particular method of a class succeeds in performing a specific set of tasks. Each test determines that a method produces the expected output when given a known input. Effective testing is an essential part of effective programming. By using the JUnit test framework, you can easily and incrementally build a test suite that helps you adjust your work schedule, uncover unwanted side effects, and focus on your development efforts.

Writing EJBs Test Cases

Here is an example of an EJB with a business method called addition, which takes two integer variables as input, adds them, and returns the result:

/**
* This are session Bean class
*/
Public Class Sampleejbbean implements Sessionbean {
Priva Te javax.ejb.SessionContext mysessionctx = null;
Final static long serialversionuid = 3206093459760846163L;

/**
* Insert The method's description here.
* Creation Date: (8/10/02 1:16:33 PM)
* @return int
* @param a int
*/
//the Business method
Public int addition (int a,int b) {
return a+b;
}
public void Ejbactivate () throws Java.rmi.RemoteException {...}
public void Ejbcreate () throws Javax.ejb.CreateException, Java.rmi.RemoteException {...} public void Ejbpassivate () throws Java.rmi.RemoteException {...}
public void Ejbremove () throws Java.rmi.RemoteException {...}
Public Javax.ejb.SessionContext Getsessioncontext () {
.....
}
public void Setsessioncontext (Javax.ejb.SessionContext ctx) throws Java.rmi.RemoteException {
...
}
}

Use the following procedure to create an EJB test case.

Create a test class by inheriting the JUnit.framework.TestCase class. naming convention: If the name of the Bean is Sampleejbbean, the test class is named Sampleejbbeantest. For example:

public class Sampleejbbeantest extends junit.framework.testcase{.

A class variable that creates a remoteinterface type of Bean. For example:

SAMPLEEJB Remoteinterface

To create a static instance of a test class:static {
  instance = new SampleEjbBeanTest("");
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.