The scenario is described as follows:
Suppose we have a bookservice class that simply implements two functions for borrowing and returning books, but Bookservice needs to call the Bookmanager interface to interact with the database.
Now that we have an Bookmanager interface (which is mainly used for database operations), let's say that we don't have a database environment now, so we can emulate the functionality of the Easymock framework.
In this scenario, the basic steps for unit testing using the Easymock framework are as follows:
1. Call the Ceartemock method to create a mock (mock object) to simulate the Bookmanager interface
2. Set the expected behavior and output of the mock object
3. Switch the mock object to the replay state
4. Calling a mock object for unit testing
5. Validating the behavior of mock objects
Precautions:
1. Learn about mock object before you use the Easymock framework to deepen your understanding.
2. Download the Easymock package to the Easymock website (http://easymock.org/) and import the Easymock.jar package into the project.
(Select the project, right-click the build path, choose Add External jar package, to find the corresponding Easymock.jar package location)
The specific code examples are as follows:
Book entity class
public class Book {
Private String name;//Title
Private String isbn;//
Private double price;//Price
Private Boolean Inshell; Is it on the shelf?
Public book () {
}
Public Book (string Name, String ISBN, double price, Boolean inshell) {
THIS.name = name;
THIS.ISBN = ISBN;
This.price = Price;
This.inshell = Inshell;
}
Public String GetName () {
return this.name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String GETISBN () {
return THIS.ISBN;
}
public void Setisbn (String ISBN) {
THIS.ISBN = ISBN;
}
Public double GetPrice () {
return this.price;
}
public void Setprice (double price) {
This.price = Price;
}
public Boolean Isinshell () {
return This.inshell;
}
public void Setinshell (Boolean inshell) {
This.inshell = Inshell;
}
}
Bookmanager interface for interacting with the database
Public interface Bookmanager {
Book Findbook (String ISBN); Find the appropriate books in the database according to the ISBN
void Updatebook (book book); Update the status of a book in the database (whether on a bookshelf)
}
Bookservice class, simple implementation of borrowing books, return two functions
public class Bookservice {
Private Bookmanager Bookmanager;
Public Bookservice (Bookmanager Bookmanager) {
This.bookmanager = Bookmanager;
}
/**
* Borrowing Books
*
* @param ISBN
* @return
*/
public boolean Borrowbook (String ISBN) {
Book book = Bookmanager.findbook (ISBN);
if (book! = null && Book.isinshell ()) {
Books exist and on the shelves
Book.setinshell (FALSE);
Bookmanager.updatebook (book);
return true;
}
return false;
}
/**
* Return Book
*
* @param ISBN
* @return
*/
public boolean Returnbook (String ISBN) {
Book book = Bookmanager.findbook (ISBN);
if (book! = null &&!book.isinshell ()) {
Books exist and are not on the shelves
Book.setinshell (TRUE);
return true;
}
return false;
}
}
Simulating tests using the Easymock framework
Import static org.junit.assert.*;
Import static org.easymock.easymock.*;
Import Org.junit.After;
Import Org.junit.Before;
Import Org.junit.Test;
public class Testbookserviceeasymock {
Declare the object we want to mock
The core Easymock framework can only mock interface objects
Private Bookmanager Mockbookmanager = null;
Private book Book1 = null;
Private book book2 = null;
@Before
public void SetUp () throws Exception {
Call the Createmock method to create a mock to simulate bookmanager
Mockbookmanager = Createmock ("Mockbookmanager", Bookmanager.class);
Create two books
Book1 = new book ("Software Test Automation", "1", 32.3, true);
Book2 = new book ("Software Test process Management", "2", 35.3, false);
}
@After
public void TearDown () throws Exception {
Validating mock object behavior
Verify (Mockbookmanager);
}
Normal borrowing
@Test
public void TestBorrowBook1 () {
Using Easymock we have two ways of declaring the expected
When the return value type of the method is void, we use this method on the mock object
When the method returns any type of object, we need to use the expect and Andreturn methods in the Easymock API
Expect (Mockbookmanager.findbook ("1")). Andreturn (Book1);
Mockbookmanager.updatebook (BOOK1);
You need to call the replay method to toggle the state of the mock object
Replay (Mockbookmanager);
Bookservice bookservice = new Bookservice (Mockbookmanager);
Boolean flag = Bookservice.borrowbook ("1");
Asserttrue (flag);
Assertfalse (Book1.isinshell ());
}
The borrowed books do not exist
@Test
public void TestBorrowBook2 () {
Expect (Mockbookmanager.findbook ("3")). Andreturn (NULL);
You need to call the replay method to toggle the state of the mock object
Replay (Mockbookmanager);
Bookservice bookservice = new Bookservice (Mockbookmanager);
Boolean flag = Bookservice.borrowbook ("3");
Assertfalse (flag);
}
The borrowed books are not on the shelves.
@Test
public void TestBorrowBook3 () {
Expect (Mockbookmanager.findbook ("2")). Andreturn (BOOK2);
You need to call the replay method to toggle the state of the mock object
Replay (Mockbookmanager);
Bookservice bookservice = new Bookservice (Mockbookmanager);
Boolean flag = Bookservice.borrowbook ("2");
Assertfalse (flag);
Assertfalse (Book2.isinshell ());
}
Normal return
@Test
public void TestReturnBook1 () {
Expect (Mockbookmanager.findbook ("2")). Andreturn (BOOK2);
You need to call the replay method to toggle the state of the mock object
Replay (Mockbookmanager);
Bookservice bookservice = new Bookservice (Mockbookmanager);
Boolean flag = Bookservice.returnbook ("2");
Asserttrue (flag);
Asserttrue (Book2.isinshell ());
}
The books are not there.
@Test
public void TestReturnBook2 () {
Expect (Mockbookmanager.findbook ("3")). Andreturn (NULL);
You need to call the replay method to toggle the state of the mock object
Replay (Mockbookmanager);
Bookservice bookservice = new Bookservice (Mockbookmanager);
Boolean flag = Bookservice.returnbook ("3");
Assertfalse (flag);
}
The books are still on the shelves
@Test
public void TestReturnBook3 () {
Expect (Mockbookmanager.findbook ("1")). Andreturn (Book1);
You need to call the replay method to toggle the state of the mock object
Replay (Mockbookmanager);
Bookservice bookservice = new Bookservice (Mockbookmanager);
Boolean flag = Bookservice.returnbook ("1");
Assertfalse (flag);
Asserttrue (Book1.isinshell ());
}
}
A detailed description of the use of Easymock framework