Tutorials : Using Mock Objects in Java :

The Mock Class for FileHandler

To use a mock object instead of the FileHandler object you must do two things:

  1. Create a mock class that implements the FileInterface
  2. Specify this new mock class when you instantiate ListFile:
    new ListFile(new MockHandler());

It's as easy as that. The question is now, what would you like to put in the mock object? It's important to remember that the purpose of the test is focused on how ListFile uses the "real" class that implements FileInterface. Things to test are:

  • correct sequence of the methods called
  • correct parameters to the methods
  • correct (number of ) methods called
  • correct handling of the data returned from the methods
  • correct Exception handling

Since the mock object doesn't implement the correct file handling logic we should not make a unit test of the implementation of the FileInterface.

Let me first show you a mock implementation of the FileInterface, and then discuss which of the points we've considered:

package hansen.playground.mock;

import junit.framework.Assert;

public class MockHandler implements FileInterface {

  private boolean initialized = false; /* 1 */
  private boolean error = false; 
  private String filename;
  
  public void open(String filename) {
    if (filename == null || filename.equals("")) { /* 2 */
      Assert.fail("Name of file is either null or blank");
    }  
    this.filename = filename;
    initialized = true; /* 1 */
  }

  public void close() {
    if (!initialized) { /* 1 */
      Assert.fail("Trying to close before open");
    }  
  }

  public String read() {
    if (!initialized) { /* 1 */
      Assert.fail("Trying to read before open");
      return null;
    } 
    if (filename.equals("readerror")) { /* 3 */
      error = true;
      return "";
    } 
    return null; 
  }

  public boolean fault() {
    return error;
  }

}

/* 1 */ is an example of "correct sequence of the methods called". open must obviously be called before read and close. Note that we're using the static methods in JUnit's Assert class to report the errors. This way we'll get an indication of the error the moment it occurs. This is in contrast with the way we normally use JUnit, where we only test after a method call has been made.

/* 2 */ is an example of "correct parameters to the methods".This test actually ought to be part of the FileHandler class as well.

/* 3 */ is an example of "correct handling of the data returned from the methods". If the mock object receives a file name of "readerror", it will simulate a read error. This is a situation that can be very difficult to unit test with the real FileHandler class, since an I/O error is something that you can't control.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.