开发人员始终需要注意所产生的代码。 在实施新功能或修复某些错误之后,应确保它们能够正常工作。 至少可以借助单元测试来实现。 由于此博客致力于Java编程语言,因此今天我将撰写有关JUnit 4.1和EasyMock 3. 1框架的文章。 这些框架的主要目的是简化单元测试的编写。

介绍

为了演示JUnit和EasyMock功能,我需要准备一些代码,这些代码将在测试中介绍。 因此,一开始我将向您介绍一个简单的
我为本教程开发的应用程序。 Java开发人员应该非常熟悉它,因为它可以模拟咖啡机的工作。 现在,我将用几句话描述应用程序的功能。 咖啡机有两个容器:用于盛水和用于咖啡。 您可以根据份量(小,中,大)制作三种咖啡。

  • 积极的情况:您使一部分咖啡和容器中有足够的水和咖啡豆。
  • 负面的情况:您煮了一部分咖啡,容器中没有足够的水或咖啡豆。

如果没有足够的容器,您可以重新装满。 在简要描述了应用程序的功能之后,您可以轻松地想象它应该如何工作。 现在该提供类的代码示例了。

申请代码

现在尝试变得更加耐心和细心。 您将在下面看到很多类,我将在下一段中对其进行测试。 如上所述,咖啡机可以生产三种类型的咖啡,具体取决于要获取的份量。 应用程序中的部分将表示为枚举。

public enum Portion {SMALL(1), MEDIUM(2), LARGE(3);private int size;private Portion(int size) {this.size = size;}public int size() {return size;}
}

我们的咖啡机有两个容器,这意味着我们需要为它们构建逻辑体系结构。 让我们坚持基于接口的智能编码方法。

public interface IContainer {public boolean getPortion(Portion portion) throws NotEnoughException;public int getCurrentVolume();public int getTotalVolume();public void refillContainer();}

为了避免代码重复,我需要为容器开发一个抽象类。 在这种编程方法的上下文中,我想考虑一下我的一篇有关Abstract类VS接口的文章 。

public abstract class AbstractContainer implements IContainer {private int containerTotalVolume;private int currentVolume;public AbstractContainer(int volume) {if (volume < 1)throw new IllegalArgumentException('Container's value must be greater then 0.');containerTotalVolume = volume;currentVolume = volume;}@Overridepublic boolean getPortion(Portion portion) throws NotEnoughException {int delta = currentVolume - portion.size();if (delta > -1) {currentVolume -= portion.size();return true;} elsethrow new NotEnoughException('Refill the '+ this.getClass().getName());}@Overridepublic int getCurrentVolume() {return currentVolume;}@Overridepublic int getTotalVolume() {return containerTotalVolume;}@Overridepublic void refillContainer() {currentVolume = containerTotalVolume;}}

抽象容器中的方法是自我解释的,因此无需在其上停止更多细节。 您可能已经注意到NotEnoughException,不用担心,这没什么特别的,只是针对应用程序定制的异常。

public class NotEnoughException extends Exception {public NotEnoughException(String text) {super(text);}}

容器接口和抽象类的开发完成后,我们可以继续进行具体的容器实现。

public class CoffeeContainer extends AbstractContainer {public CoffeeContainer(int volume) {super(volume);}}

同一类用于水容器:

public class WaterContainer extends AbstractContainer {public WaterContainer(int volume) {super(volume);}}

现在,我们拥有开发与咖啡机相关的代码所需的所有内容。 和以前一样,我将从界面开发开始。

public interface ICoffeeMachine {public boolean makeCoffee(Portion portion) throws NotEnoughException;public IContainer getCoffeeContainer();public IContainer getWaterContainer();}

最后是咖啡机的实现:

public class CoffeeMachine implements ICoffeeMachine {private IContainer coffeeContainer;private IContainer waterContainer;public CoffeeMachine(IContainer cContainer, IContainer wContainer) {coffeeContainer = cContainer;waterContainer = wContainer;}@Overridepublic boolean makeCoffee(Portion portion) throws NotEnoughException {boolean isEnoughCoffee = coffeeContainer.getPortion(portion);boolean isEnoughWater = waterContainer.getPortion(portion);if (isEnoughCoffee && isEnoughWater) {return true;} else {return false;}}@Overridepublic IContainer getWaterContainer() {return waterContainer;}@Overridepublic IContainer getCoffeeContainer() {return coffeeContainer;}}

就是这样,与要在单元测试的帮助下测试的程序有关。

JUnit测试

在开始JUnit测试开发之前,我想重复单元测试的规范目标。 单元测试检查功能的最小部分-方法或类。 这种情况对开发施加了一定的逻辑限制。 这意味着您不需要在方法中添加一些额外的逻辑,因为在此之后,测试变得更加困难。 还有一件更重要的事情–单元测试意味着功能与应用程序其他部分的隔离。 在使用方法“ B”时,我们不需要检查方法“ A”的功能。 因此,让我们为咖啡机应用程序编写JUnit测试。 为此,我们需要向pom.xml添加一些依赖项

...<dependency><groupid>org.easymock</groupid><artifactid>easymock</artifactid><version>3.1</version></dependency><dependency><groupid>junit</groupid><artifactid>junit</artifactid><version>4.11</version></dependency>
...

我选择了AbstractContainer类来演示JUnit测试。 因为在应用程序上下文中,我们有该类的两种实现,并且如果要为其编写测试,则自动地,我们将测试WaterContainer类和CoffeeContainer类。

import static org.junit.Assert.assertEquals;import org.junit.After;
import org.junit.Before;
import org.junit.Test;import com.app.data.Portion;
import com.app.exceptions.NotEnoughException;
import com.app.mechanism.WaterContainer;
import com.app.mechanism.interfaces.IContainer;public class AbstractContainerTest {IContainer waterContainer;private final static int VOLUME = 10;@Beforepublic void beforeTest() {waterContainer = new WaterContainer(VOLUME);}@Afterpublic void afterTest() {waterContainer = null;}@Test(expected = IllegalArgumentException.class)public void testAbstractContainer() {waterContainer = new WaterContainer(0);}@Testpublic void testGetPortion() throws NotEnoughException {int expCurVolume = VOLUME;waterContainer.getPortion(Portion.SMALL);expCurVolume -= Portion.SMALL.size();assertEquals('Calculation for the SMALL portion is incorrect',expCurVolume, waterContainer.getCurrentVolume());waterContainer.getPortion(Portion.MEDIUM);expCurVolume -= Portion.MEDIUM.size();assertEquals('Calculation for the MEDIUM portion is incorrect',expCurVolume, waterContainer.getCurrentVolume());waterContainer.getPortion(Portion.LARGE);expCurVolume -= Portion.LARGE.size();assertEquals('Calculation for the LARGE portion is incorrect',expCurVolume, waterContainer.getCurrentVolume());}@Test(expected = NotEnoughException.class)public void testNotEnoughException() throws NotEnoughException {waterContainer.getPortion(Portion.LARGE);waterContainer.getPortion(Portion.LARGE);waterContainer.getPortion(Portion.LARGE);waterContainer.getPortion(Portion.LARGE);}@Testpublic void testGetCurrentVolume() {assertEquals('Current volume has incorrect value.', VOLUME,waterContainer.getCurrentVolume());}@Testpublic void testGetTotalVolume() {assertEquals('Total volume has incorrect value.', VOLUME,waterContainer.getTotalVolume());}@Testpublic void testRefillContainer() throws NotEnoughException {waterContainer.getPortion(Portion.SMALL);waterContainer.refillContainer();assertEquals('Refill functionality works incorectly.', VOLUME,waterContainer.getCurrentVolume());}}

我需要解释所有使用的注释。 但是我对此很懒,我只给您一个指向JUnit API的链接。 在这里,您可以阅读最正确的说明。 注意所有测试的共同点–它们都标记有@Test批注,它表示以下方法是测试,并且每个测试都以某些“ assert ”方法结尾。 断言对于每个测试都是必不可少的部分,因为应该在测试的最后检查所有操作。

具有EasyMock测试的JUnit

好的,在上一段中,我向您展示了几个简单的JUnit测试示例。 在该示例中,测试不与任何其他类协作。 如果我们需要在JUnit测试中包含一些额外的类怎么办? 我在上面提到,单元测试应该与其余应用程序的功能隔离。 为此,您可以使用EasyMock测试框架 。 借助EasyMock,您可以创建模拟游戏。 嘲笑是模拟真实具体对象行为的对象,但是有了一个大的加号,您可以为模拟指定状态,这样,您就可以在特定的单元测试时刻为假对象获得该状态。

import static org.junit.Assert.*;import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;public class CoffeeMachineTest {ICoffeeMachine coffeeMachine;IContainer coffeeContainer;IContainer waterContainer;@Beforepublic void setUp() {coffeeContainer = EasyMock.createMock(CoffeeContainer.class);waterContainer = EasyMock.createMock(WaterContainer.class);coffeeMachine = new CoffeeMachine(coffeeContainer, waterContainer);}@Afterpublic void tearDown() {coffeeContainer = null;waterContainer = null;coffeeMachine = null;        }@Testpublic void testMakeCoffe() throws NotEnoughException {EasyMock.expect(coffeeContainer.getPortion(Portion.LARGE)).andReturn(true);EasyMock.replay(coffeeContainer);EasyMock.expect(waterContainer.getPortion(Portion.LARGE)).andReturn(true);EasyMock.replay(waterContainer);assertTrue(coffeeMachine.makeCoffee(Portion.LARGE));}@Testpublic void testNotEnoughException() throws NotEnoughException {EasyMock.expect(coffeeContainer.getPortion(Portion.LARGE)).andReturn(false);EasyMock.replay(coffeeContainer);EasyMock.expect(waterContainer.getPortion(Portion.LARGE)).andReturn(true);EasyMock.replay(waterContainer);assertFalse(coffeeMachine.makeCoffee(Portion.LARGE));}}

在前面的代码片段中,您可以看到JUnit和EasyMock的合作。 我可以强调EasyMock使用中的一些基本知识。

  1. 如果测试需要与某个外部对象进行交互,则应对其进行模拟。

    coffeeContainer = EasyMock.createMock(CoffeeContainer.class);
  2. 设置模拟或具体方法的行为,这是测试被测对象所必需的。
    EasyMock.expect(coffeeContainer.getPortion(Portion.LARGE)).andReturn(true);
  3. 将模拟切换到回复模式。
    EasyMock.replay(coffeeContainer);

    EasyMock的API有很多不同的方法,因此我建议在官方网站上内容。

JUnit测试套件

当您有一个小型应用程序时,可以单独启动JUnit测试,但是如果您在大型复杂的应用程序上工作该怎么办? 在这种情况下,可以通过某些功能将单元测试汇总到测试服中。 JUnit为此提供了便捷的方法。

@RunWith(Suite.class)
@SuiteClasses({ AbstractContainerTest.class, CoffeeMachineTest.class })
public class AllTests {}

摘要

单元测试是软件开发中非常重要的一部分,它具有许多方法,方法和工具。 在这篇文章中,我对JUnit和EasyMock进行了概述,但是我忽略了许多有趣的时刻和技术,打算在以下教程中进行介绍。 您可以从我的DropBox下载该教程的源代码 。

参考:在Fruzenshtein的注释博客中,我们的JCG合作伙伴 Alex Fruzenshtein的JUnit和EasyMock合作 。

翻译自: https://www.javacodegeeks.com/2013/03/junit-and-easymock-cooperation.html

JUnit与EasyMock合作相关推荐

  1. JUnit和Mockito合作

    这次,我想对测试框架Mockito进行概述. 毫无疑问,这是用于测试Java代码的最受欢迎的工具之一. 我已经对Mockito的竞争对手EasyMock进行了概述. 这篇文章将基于有关EasyMock ...

  2. junit mockito_JUnit和Mockito合作

    junit mockito 这次,我想对测试框架Mockito进行概述. 毫无疑问,这是用于测试Java代码的最受欢迎的工具之一. 我已经对Mockito的竞争对手EasyMock进行了概述. 这篇文 ...

  3. easymock使用方法_EasyMock最终方法– PowerMock,JUnit 4,TestNG

    easymock使用方法 One of the limitations of EasyMock is that it can't mock final methods and final classe ...

  4. easymock使用方法_EasyMock静态方法– PowerMock,JUnit 4,TestNG

    easymock使用方法 One of the limitations of EasyMock is that it can't mock static methods. However, we ca ...

  5. easymock教程_EasyMock教程–入门

    easymock教程 在本文中,我将向您展示EasyMock是什么,以及如何使用它来测试Java应用程序. 为此,我将创建一个简单的Portfolio应用程序,并使用JUnit&EasyMoc ...

  6. EasyMock教程–入门

    在本文中,我将向您展示EasyMock是什么,以及如何使用它来测试Java应用程序. 为此,我将创建一个简单的Portfolio应用程序,并使用JUnit&EasyMock库对其进行测试. 在 ...

  7. powermock私有字段_使用PowerMock的EasyMock私有方法模拟

    powermock私有字段 Sometimes we want to test a method that is using a private method. We can create the m ...

  8. 应用maven的感想

    使用maven2一段时间了,我基本把我自己能够迁移的project都转换为maven2 project,以下是我的一点感想. (原作者温少,转载请注明) 乱世盼英雄 现在的软件开发,比过去的软件开发要 ...

  9. 代码规范(一)——java篇

    为什么80%的码农都做不了架构师?>>>    1. Result result=new Result(); result.setCode(201); result.setStr(r ...

最新文章

  1. 一步一步指引你在Windows7上配置编译使用Caffe(https://github.com/fengbingchun/Caffe_Test)
  2. lighttpd安装及secdownload,fastcgi,proxy配置
  3. 【luogu P3377 左偏树(可并堆)】 模板
  4. 【直播回放】150分钟详解模型压缩理论和实践
  5. Vision Transformer中的自监督学习
  6. android的应用组件,跟我学android-Android应用基本组件介绍(五)
  7. Odoo12中删除无效项目
  8. C++ ofstream/ifstream读写文件demo
  9. hdu 1890 Robotic SortI(splay区间旋转操作)
  10. 耗时1个月整理的这份英语资源!一次性全部分享给你,手慢无!
  11. 转:Redis 应用案例 - 在问题中不断成长
  12. 计算机应用计算专业难吗,计算机应用专业好学吗
  13. linux下可以输入中文曲,Ubuntu 14.04终端模式下中文输入听歌
  14. Android App加壳中的整体dex加壳的原理和脱壳
  15. My interest is the rules/ways to implement, go and insist with interest!!!
  16. CorelDRAW x8警告您所用的软件疑似非法盗版软件,软件功能3天内将被永久停用,解决方法教程
  17. ROS-control-gazebo-moveit-grasp(一、场景搭建)
  18. c++ opengl 三维图形中显示文字_3D图形 和 OpenGL 简介
  19. 第一章 认识Java 2019-09-28
  20. AFE4490学习(一)

热门文章

  1. 分数优先遵循志愿php源码_分数优先 遵循志愿
  2. 雨滴桌面时间插件_Win10美化向——如何搭配你的桌面
  3. 小程序执行运行过程原理_PLC的基础小知识!不用把PLC想的太难
  4. 如何评价分类模型性能?(足球荔枝)
  5. vue - resource 使用过程的坑
  6. java javap_javap的用途不断发展:您的Java类文件中隐藏了什么?
  7. Infinispan版本已映射到最低Java版本
  8. java的默认值规则_Java 8:默认方法解析规则
  9. jcmd_程序化JCMD访问
  10. java写入单个字符_将2个字符写入单个Java char