Book Image

Mastering Unit Testing Using Mockito and JUnit

By : Sujoy Acharya
Book Image

Mastering Unit Testing Using Mockito and JUnit

By: Sujoy Acharya

Overview of this book

Table of Contents (17 chapters)
Mastering Unit Testing Using Mockito and JUnit
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Fake


Fake objects are working implementations; mostly, the fake class extends the original class, but it usually hacks the performance, which makes it unsuitable for production. The following example demonstrates the fake object:

public class AddressDao extends SimpleJdbcDaoSupport{

  public void batchInsertOrUpdate(List<AddressDTO> addressList, User user){
    List<AddressDTO> insertList = buildListWhereLastChangeTimeMissing(addressList);

    List<AddressDTO> updateList = buildListWhereLastChangeTimeValued(addressList);
    int rowCount =  0;

    if (!insertList.isEmpty()) {
      rowCount = getSimpleJdbcTemplate().batchUpdate(INSERT_SQL,…);
    }

    if (!updateList.isEmpty()){
      rowCount += getSimpleJdbcTemplate().batchUpdate(UPDATE_SQL,…);
    }

    if (addressList.size() != rowCount){
      raiseErrorForDataInconsistency(…); 
    }
}

AddressDAO extends from a Spring framework class and provides an API for mass update. The same method is used to create a new...