Using Cactus to test the application of Java EE

Source: Internet
Author: User

JUnit is currently the most popular test framework, it allows developers to easily write test units, so that they can "rest assured" to develop. But now a lot of applications are based on Java, the code is in the server-side container running, this makes the test brought some trouble. Testing with JUnit for ordinary jsp,servlet seems less convenient, and for EJBS, especially the 2.0 version, many interfaces are local Interface and there is no way to do distributed testing. So how do we test this code? Apache provides us with a powerful tool cactus! It's a simple, Easy-to-use server-side test framework that makes it easy for developers to test server-side programs, and they say, "Oh, that's easy." Cactus is an extension of junit, but it is somewhat different from junit. Cactus's tests are divided into three different test categories, jsptestcase,servlettestcase,filtertestcase, rather than JUnit as a testcase. Cactus's test code has server-side and client two parts, they work together. So why don't we use junit to test it? There are mainly several reasons:

The local interface in the EJB2.0, the remote call is not heard. Difficult to test with junit, and Cactus's redirector is on the server side and can be run in a container with EJBS, which makes it possible to access the local Interface directly.

The general EJB or servlet,jsp is running on the server, and if you use JUnit tests, your test is on the client, which makes the running environment and the test environment in a different system environment, which sometimes results in different tests.

In an EJB application, there are generally some front-end applications to access the EJB, such as: Jsp,servlet,javabean. This means that you need a test framework to test these front-end components. Cactus provides a test method for all of these components. Oh, that's great.

Cactus and Ant are well integrated and can easily complete automated tests, reducing a lot of work. Of course, JUnit also provides such support.

The

is preceded by a rough introduction to cactus, and then we use a practical example to apply this powerful test framework. First we need a tested object, where we choose EJB2.0 CMP. We do a simple user management. For some of the main code, to do some analysis.

Userhome.java
Package usersystem
Import javax.ejb.*;
import java.util.*;
Public interface Userhome extends Javax.ejb.EJBLocalHome {
Public User Create (string name, string password) throws Createexception; public Collection FindAll () throws finderexception;
Public User Findbyprimarykey (String name) throws Finderexception;
}
User.java
Package usersystem
Import javax.ejb.*;
import java.util.*;
Public Interface User ext   Ends Javax.ejb.EJBLocalObject {
Public String getName ();
public void SetPassword (String password);
Public String GetPassword ();
public void Setuserinfo (UserInfo UserInfo);
Public UserInfo getuserinfo ();
public void SetName (String name); 
}
Userinfohome.java
Package usersystem
Import javax.ejb.*;
import java.util.*;
Public Interface Userinfohome extends Javax.ejb.EJBLocalHome {
Public UserInfo Create (string name, string email, string ad Dress, String Tel) throws
CreateException;
Public UserInfo Findbyprimarykey (String name) throws Finderexception;
}

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.