JUnit - isolation test with Mock Object (on)

xiaoxiao2021-03-06  38

Recently, I took time to translate the JUnit in action of Manning, and the translation is more bad. You don't want BT. The following is the first section of Chapter 7, the introduction of the first section of Mock Object: Isolation test has a lot of benefits, such as testing those still have not Written code (as long as he has an interface). In addition, the isolation test is conducive to the work team to test a part of the code without waiting until all parts are developed. However, the best benefit is to write relevant tests to test a single method, avoid other side effects that are called objects. MockObject is well solved the isolation test problem. Mocks replaces those objects (your method needs to be called), thus generating a barrier layer, from this perspective, he and Stubs are similar. However, this is their only similar point. Because Mocks does not implement any logic: they are just empty shells - providing a way to make test control for all business methods for fake classes.

Below is the definition of Mock Object: Mock Object is an object that is used instead of the object being called in your program. Your code can call MockObject objects, the result of his generated is the same as your real environment. Section 2 A simple example: AccountService is commercial logic. AccountManager is equivalent to Facade, providing access to the database. The ability of Account is equivalent to VO. In the case where there is no Mocks, test AccountService --- Transfer will create a database, preset some test data, but also to deploy your code to the application server (Container). Although these processes need to be used one by one in your application, but he did too much unnecessary job for test code logic. The structural diagram of this example is as follows: Account.java

Package junitbook.fine.tasting;

Public Class Account

{

PRIVATE STRING ACCOUNTID;

PRIVATE long balance;

Public Account (String Accountid, Long Initialbalance)

{

this.accountid = AccountId;

THIS.BALANCE = INITIALBALANCE;

}

Public void debit (long amount)

{

THIS.BALANCE - = Amount;

}

Public Void Credit (Long Amount)

{

THIS.BALANCE = Amount;

}

Public long getBalance ()

{

Return this.balance;

}

} Generate a simple Account object, he has two properties: ID, balance.accountManager.javaPackage junitbook.fine.tasting;

Public Interface AccountManager

{

Account Findaccountforuser (String UserID);

Void UpdateAccount; Acount Account;

} An interface. Used to control the Account object. There are two ways. AccountService.java

Package junitbook.fine.tasting;

Public Class AccountService

{

Private AccountManager AccountManager;

Public void setAccountManager (AccountManager Manager) {

this.accountManager = manager;

}

Public void transfert (String BeneficiaryID,

Long Amount

{

Account sender =

This.accountManager.FindaccountForuser (Senderid);

Account beneficious =

This.accountManager.findaccountforuser (BeneficiaryID);

Sender.debit (Amount);

Beneficiary.credit (Amount);

this.accountManager.UpdateAccount (Sender);

This.accountManager.UpdateAccount (Beneficia);

}

转载请注明原文地址:https://www.9cbs.com/read-52495.html

New Post(0)