1. easymock Overview:
Next, let me show you how to use the JUnit and easymock frameworks for unit testing.
Our first example is very simple, but in reality, you usually use other classes in some classes. Before you perform a real test, you may need to do a lot of work.CodeTo start a large and complex system, it may be a database, a success, or a certain type of IDE environment. Your preset Environment Code must be that the system enters a specific State, in order to respond according to the methods required by the test. However, this kind of work is unlikely to be completed soon.
To perform unit tests on some classes, you need to establish and control other classes.The best way is to create a simulated object for the class to be tested.You can write classes manually or use easymock to generate these objects.
The simulated object provides a solution that proves to be successful.When it is difficult or impossible for us to create a desired state for a resource that is difficult to process or to access a resource, you can use a simulated object.
The simulated object replaces the location of the real object and is used to test some functions that interact with the real object or depend on the real object. The basic idea behind a simulated object is to create a lightweight and controllable object to replace the object to be used for writing a test. The simulated object also allows you to specify and test the interaction between your code and the simulated object.
Simply put, a simulated object is a simple interface or class, in which you can define a simple output after a specific method is called.
2. method to be tested:
Public ClassIncomecalb {PrivateIcalcmethod method;PrivatePosition position;Public VoidSetmethod (icalcmethod method ){This. Method = method ;}Public VoidSetposition (position ){This. Position = position ;}Public DoubleCalc (){ReturnMethod. Calc (position );}}
Interface:
Public InterfaceIcalcmethod {Public Abstract DoubleCalc (position );}
Enumeration class:
PublicEnum position {boss, programmer, Manager}
3. Use JUnit and easymock for testing:
Import Static Org. JUnit. Assert .*; Import Org. easymock. easymock; Import Org. JUnit. before; Import Org. JUnit. test; Public Class Incomecalctest { Private Icalcmethod mock; Private Incomec1c incomec1c; @ before Public Void Setup () Throws Exception { // 1. Create a simulated object Mock = easymock. createmock (icalcmethod. Class ); Incomec1c = New Incomecalb ();} /*** The Except CT method tells easymock to get a specific method and uses some specific parameters. * addreturn defines the return value for a method. The Times method defines how many times a simulated object will be called. * The reply method must be called before the simulated object can be used. * After the test is completed, you can call the verify method to check whether the simulated object is called as expected. */ @ Test Public Void Testcalc (){ // 2. Preparation: record the methods, return values, and execution times used in the test. Easymock. round CT (mock. calc (position. boss )). andreturn (7000.00 ). times (2); easymock. round CT (mock. calc (position. manager )). andreturn (5000.00 ); // 3. After preparation, set the mock object to the "playback" mode. Easymock. Replay (Mock ); // 4. perform the test Incomec1c. setmethod (Mock); incomec1c. setposition (position. Boss); assertequals (7000.00, incomec1c. Calc ()); // Because the test is scheduled to be performed twice Assertequals (7000.00, incomec1c. Calc ()); // Otherwise, an error occurs. Incomec1c. setposition (position. Manager); assertequals (5000.00, incomec1c. Calc ());// Incomec1c. setposition (position. programmer ); // 5. Determine whether all methods in the simulated object have been executed. // Not a required step Easymock. Verify (Mock );}}