Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Mock Objects in Unit Tests

EasyMock is a well-known mock tool that can create a mock object for a given interface at runtime.

Tutorial Details:

Unit testing your code against a service or process that's either too expensive (commercial databases) or just not done yet is something you can deal with by simulating the other piece with a mock object. EasyMock can suffice in some cases, but it can only create mock objects for interfaces. Mocquer, based on the Dunamis project, can create mocks for classes, too. Lu Jian shows how it works.

The mock object's behavior can be defined prior encountering the test code in the test case. EasyMock is based on java.lang.reflect.Proxy, which can create dynamic proxy classes/objects according to given interfaces. But it has an inherent limitation from its use of Proxy: it can create mock objects only for interfaces.

Life Cycle Control Methods

public void replay();
public void verify();
public void reset();

The mock object has three states in its life cycle: preparing, working, and checking. Figure 1 shows the mock object life cycle


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Mock Objects in Unit Tests

View Tutorial:
Mock Objects in Unit Tests

Related Tutorials:

Accelerate your Java apps! - JavaWorld - September 1998
Accelerate your Java apps! - JavaWorld - September 1998
 
Test infect your Enterprise JavaBeans - JavaWorld May 2000
Test infect your Enterprise JavaBeans - JavaWorld May 2000
 
JUnit best practices - JavaWorld December 2000
JUnit best practices - JavaWorld December 2000
 
Printing in Java, Part 4 - JavaWorld February 2001
Printing in Java, Part 4 - JavaWorld February 2001
 
Warning! Threading in a multiprocessor world - JavaWorld February 2001
Warning! Threading in a multiprocessor world - JavaWorld February 2001
 
Make bad code good - JavaWorld March 2001
Make bad code good - JavaWorld March 2001
 
Pattern your way to automated regression testing
Pattern your way to automated regression testing
 
Reflection vs. code generation
Reflection vs. code generation
 
Test networked code the easy way
Test networked code the easy way
 
Java tools reign supreme - JavaWorld celebrates the leading Java tools
Java tools reign supreme - JavaWorld celebrates the leading Java tools
 
Good, but obsolete
Good, but obsolete
 
Java Testing and Design
Java Testing and Java Test and Design is the companion to any book on Java software development practices, techniques, and testing. Software developers, QA analysts and IT managers working in large corporate IT groups, software development companies, and
 
Introduction to Tag Unit
Getting Started For the purpose of this article, let's say that we would like to test the core taglib from the Jakarta Taglibs implementation of the JSTL, a taglib that many people will be aware of and have experience with. Assuming that you already have
 
Developing Swing Components Using Simulators
Developing Swing Components Using Simulators In this article, you'll learn about a technique for developing encapsulated components. You'll also learn about how to test graphical parts of your application that JUnit can't, such as layouts and other visua
 
Martin Fowler\'s usual terrific writing
The term 'Mock Objects' has become a popular one to describe special case objects that mimic real objects for testing. However the term mock was not originally meant as a more catchy name for stub, but to introduce a different approach to unit testing. In
 
JDemo: Interactive Testing Refactored
This article will introduce the JDemo framework and its techniques for writing code for interactive testing. It will also show the benefits that can be gained from writing demo code.
 
Practically Groovy: Unit test your Java code faster with Groovy
Why unit test with Groovy? What makes Groovy particularly appealing with respect to other scripting platforms is its seamless integration with the Java platform. Because it's based on the Java language (unlike other alternate languages for the JRE, which
 
Automate GUI tests for Swing applications
Summary Automation is necessary for frequent and consistent testing, which is the foundation of agile development. However, acceptance tests of GUI applications are not always easy to automate. This article explains a simple way of automating Java Swing
 
Testing Your Enterprise JavaBeans with Cactus
Enterprise JavaBeans provide many advantages. But each server-side/back-end developer knows that development of EJBs is sometimes painful, time-consuming, and requires a lot of patience while creating assembly descriptors, application-server-specific conf
 
Mock Objects in Unit Tests
EasyMock is a well-known mock tool that can create a mock object for a given interface at runtime.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.