package test.myUnitTest.fine.tasting;
import myUnitTest.fine.tasting.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import junit.framework.JUnit4TestAdapter;
public class TestAccountService {
@Test public void transferOk(){
MockAccountManager mockAccountManager =
new MockAccountManager();
Account senderAccount = new Account("1", 200 );
Account beneficiaryAccount = new Account("2", 100 );
mockAccountManager.addAccount("1", senderAccount);
mockAccountManager.addAccount("2", beneficiaryAccount);
AccountService accountService = new AccountService();
accountService.setAccountManager(mockAccountManager);
accountService.transfer("1", "2", 50);
assertEquals( (long)150,(senderAccount.getBalance() ));
assertEquals( (long)150, (beneficiaryAccount.getBalance()) );
//原书
// assertEquals( 150,(senderAccount.getBalance() ));
//assertEquals( 150, (beneficiaryAccount.getBalance()) );
}
}
import myUnitTest.fine.tasting.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import junit.framework.JUnit4TestAdapter;
public class TestAccountService {
@Test public void transferOk(){
MockAccountManager mockAccountManager =
new MockAccountManager();
Account senderAccount = new Account("1", 200 );
Account beneficiaryAccount = new Account("2", 100 );
mockAccountManager.addAccount("1", senderAccount);
mockAccountManager.addAccount("2", beneficiaryAccount);
AccountService accountService = new AccountService();
accountService.setAccountManager(mockAccountManager);
accountService.transfer("1", "2", 50);
assertEquals( (long)150,(senderAccount.getBalance() ));
assertEquals( (long)150, (beneficiaryAccount.getBalance()) );
//原书
// assertEquals( 150,(senderAccount.getBalance() ));
//assertEquals( 150, (beneficiaryAccount.getBalance()) );
}
}
本文介绍了一个针对转账功能的单元测试案例实现,通过MockAccountManager模拟账户管理器,并使用JUnit进行断言验证,确保转账操作正确执行。

被折叠的 条评论
为什么被折叠?



