Easymock Introduction
first, Mock introduction
1.1 Introduction
Mock Test
It is during the testing process that a virtual object is used to create test methods that are not easily constructed or easy to obtain.
Mock objects
This virtual object is a mock object. Mock objects are substitutes for real objects during debugging.
Mock Object Usage Category
Real objects have unpredictable behavior, produce unpredictable effects, (e.g. stock quotes, weather forecasts) real objects that are difficult to create some behavior of real objects can hardly be triggered by real objects that do not actually exist (with other development teams or with new hardware), and so on.
When do I need a mock object
-----An uncertain behavior (produce unpredictable results, such as stock quotes) for real objects.
-----Real objects are difficult to create (e.g. specific web containers)
-----Certain behaviors of real objects are difficult to trigger (such as network errors)
-----The real situation makes the program slow to run
-----Real object has a user interface
-----test needs to ask the real object how it was invoked (for example, the test might need to verify that a callback function was invoked)
-----Real objects don't really exist (this is a common problem when you need to deal with other development groups, or new hardware systems)
1.2 Example Description
An alarm clock based on the time to remind the service, if after 5 o'clock in the afternoon to play audio files to remind everyone to work, if we want to use the real object to test the words can only wait until five o'clock in the afternoon, and then put the ear next to the speaker ... We don't want to be so stupid, we should use mock objects to test, so we can simulate control time without waiting for the clock to turn to 5 o'clock in the afternoon. An alarm clock based on the time to remind the service, if after 5 o'clock in the afternoon to play audio files to remind everyone to work, if we want to use the real object to test the words can only wait until five o'clock in the afternoon, and then put the ear next to the speaker ... We don't want to be so stupid, we should use mock objects to test, so we can simulate control time without waiting for the clock to turn to 5 o'clock in the afternoon.
[Java] view Plain Copy public abstract class Environmental {Boolean playedwav = false; Public abstract long getTime (); public abstract void Playwavfile (String fileName); Public abstract Boolean wavwasplayed (); public abstract void Resetwav (); }
The real implementation code:
[Java] View Plain Copy public class systemenvironment extends environmental { public long gettime () { return system.currenttimemillis (); } public void playwavfile (string filename) { playedWav = true; } public boolean wavwasplayed () { return playedWav; } public void resetwav () { playedWav = false; } }
The following are mock objects:
[Java] view plain copy public class Mocksystemenvironment extends environmental {private long currenttime ; Public long GetTime () {return currenttime; } public