EasyMock技术与应用(八)

EasyMock技术与应用

一、实验目的

二、实验环境(本实验的硬件和软件环境及使用仪器等)

三、实验实现过程

四、实验结果分析与总结


一、实验目的

1.掌握自定义用户库的设置

2.掌握EasyMock模型

3.掌握使用EasyMock进行测试

二、实验环境(本实验的硬件和软件环境及使用仪器等)

硬件:PC电脑一台;

配置: window系统,内存大于4G  硬盘250G及以上

软件:eclipse、 jdk15

三、实验实现过程

实验内容:

任务1、下载Easymock相关Jar包,并配置Eclipse的用户库

右键项目选择添加库

选择用户库

点击右侧用户库

点击新建

自定义库名

点击添加外部jar

选择jar包并打开

导入成功

任务2、、EasyMock测试的应用

1、请查看lab9文件夹下面的account 的相关代码,请用Easymock 编写AccountService.java的测试代码。

新建包

创建类

package lab10.account;

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;

}

}

创建接口

package lab10.account;

public interface AccountManager {

Account findAccountForUser(String userId);

void updateAccount(Account account);

}

创建类

package lab10.account;

public class AccountService {

private AccountManager accountManager;

public void setAccountManager(AccountManager manager) {

this.accountManager = manager;

}

public void transfer(String senderId, String beneficiaryId, long amount) {

Account sender = this.accountManager.findAccountForUser(senderId);

Account beneficiary = this.accountManager

.findAccountForUser(beneficiaryId);

sender.debit(amount);

beneficiary.credit(amount);

this.accountManager.updateAccount(sender);

this.accountManager.updateAccount(beneficiary);

}

}

创建测试用例

package lab10.account;

import static org.junit.Assert.*;

import org.easymock.EasyMock;

import static org.easymock.EasyMock.expect;

import static org.easymock.EasyMock.replay;

import static org.easymock.EasyMock.verify;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

public class TestAccountServiceWithEasyMock {

private AccountManager mockAccountManager;

@Before

public void setUp() {

mockAccountManager = EasyMock.createMock("mockAccountManager", AccountManager.class);

}

@Test

public void testTransferOk() {

Account senderAccount = new Account("1", 200);

Account beneficiaryAccount = new Account("2", 100);

mockAccountManager.updateAccount(senderAccount);

mockAccountManager.updateAccount(beneficiaryAccount);

expect(mockAccountManager.findAccountForUser("1")).andReturn(senderAccount);

expect(mockAccountManager.findAccountForUser("2")).andReturn(beneficiaryAccount);

replay(mockAccountManager);

AccountService accountService = new AccountService();

accountService.setAccountManager(mockAccountManager);

accountService.transfer("1", "2", 50);

assertEquals(150, senderAccount.getBalance());

assertEquals(150, beneficiaryAccount.getBalance());

}

@After

public void tearDown() {

verify(mockAccountManager);

}

}

测试测试用例

测试结果

2、请查看lab9文件夹下面web的相关代码,请用Easymock编写WebClient.java的测试代码。

创建包

创建接口

package lab10.web;

import java.io.InputStream;

public interface ConnectionFactory {

InputStream getData() throws Exception;

}

创建类

package lab10.web;

import java.io.IOException;

import java.io.InputStream;

public class WebClient {

    public String getContent(ConnectionFactory connectionFactory)

           throws IOException {

       String result;

       StringBuffer content = new StringBuffer();

       InputStream is = null;

       try {

           is = connectionFactory.getData();

           int count;

           while (-1 != (count = is.read())) {

              content.append(new String(Character.toChars(count)));

           }

           result = content.toString();

       } catch (Exception e) {

           result = null;

       }

       // Close the stream

       if (is != null) {

           try {

              is.close();

           } catch (IOException e) {

              result = null;

           }

       }

       return result;

    }

}

创建测试用例

package lab10.web;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

import org.easymock.EasyMock;

import static org.easymock.EasyMock.*;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

public class TestWebClientEasyMock {

private ConnectionFactory factory;

private InputStream stream;

@Before

public void setUp() throws Exception {

factory = EasyMock.createMock("factory", ConnectionFactory.class);

stream = EasyMock.createMock("stream", ByteArrayInputStream.class);

}

@After

public void tearDown() throws Exception {

EasyMock.verify(factory,stream);

}

@Test

public void testGetContentOk() throws Exception {

expect(factory.getData()).andReturn(stream);

expect(stream.read()).andReturn(Integer.valueOf((byte)'W'));

expect(stream.read()).andReturn(Integer.valueOf((byte)'o'));

expect(stream.read()).andReturn(Integer.valueOf((byte)'r'));

expect(stream.read()).andReturn(Integer.valueOf((byte)'k'));

expect(stream.read()).andReturn(Integer.valueOf((byte)'s'));

expect(stream.read()).andReturn(-1);

stream.close();

replay(factory, stream);

WebClient client = new WebClient();

String result = client.getContent(factory);

assertNull("Works", result);

}

@Test

public void testGetContentCannotCloseInputStream() throws Exception {

expect(factory.getData()).andReturn(stream);

expect(stream.read()).andReturn(Integer.valueOf((byte) 'W'));

expect(stream.read()).andReturn(Integer.valueOf((byte) 'o'));

expect(stream.read()).andReturn(Integer.valueOf((byte) 'r'));

expect(stream.read()).andReturn(Integer.valueOf((byte) 'k'));

expect(stream.read()).andReturn(Integer.valueOf((byte) 's'));

expect(stream.read()).andReturn(-1);

stream.close();

expectLastCall().andThrow(new IOException("cannot close"));

replay(factory, stream);

WebClient client = new WebClient();

String result = client.getContent(factory);

assertNull(result);

}

}

测试测试用例

测试结果

四、实验结果分析与总结

在account测试中一直报错

之后检查了代码对照发现使用这句导包才没有问题

在web测试时出现测试失败的情况,不断检索和调试也依然失败,报错码如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值