当前位置: 首页 > news >正文

最近新闻头条最新消息重庆电子商务seo

最近新闻头条最新消息,重庆电子商务seo,权4网站怎么做,郑州免费做网站的JUnit JUnit 是一个用于编写可重复测试的简单框架。 它是 xUnit 架构的一种实例,专门用于单元测试框架。 What to test? NeedDescRight结果是否正确B边界条件是否满足I能反向关联吗C有其他手段交叉检查吗E是否可以强制异常发生P性能问题 maven 入门例子 maven …

JUnit

JUnit 是一个用于编写可重复测试的简单框架。

它是 xUnit 架构的一种实例,专门用于单元测试框架。

What to test?

NeedDesc
Right结果是否正确
B边界条件是否满足
I能反向关联吗
C有其他手段交叉检查吗
E是否可以强制异常发生
P性能问题

maven 入门例子

maven 引入

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope>
</dependency>

方法

public class Calculator {public int add(int a, int b) {return a + b;}
}

测试方法

import org.junit.Test;
import static org.junit.Assert.*;public class CalculatorTest {@Testpublic void testAdd() {// ArrangeCalculator calculator = new Calculator();// Actint result = calculator.add(3, 7);// AssertassertEquals(10, result);}
}

CalculatorTest 类包含了一个测试方法 testAdd,用于测试 Calculator 类的 add 方法。

@Test 注解表示这是一个测试方法。

在测试方法中,我们首先创建了一个 Calculator 对象,然后调用 add 方法进行加法操作。

最后,使用 assertEquals 断言来验证计算的结果是否符合预期值。

运行测试类

在 IDE 中,通常有一个 “Run” 或 “Debug” 按钮,可以直接运行测试类。

也可以通过

mvn test

统一执行测试用例

验证结果

测试运行后,IDE 会显示测试结果。

如果测试通过,你将看到一个绿色的标志;如果测试失败,将会显示红色的标志,并且会提供详细的失败信息。

我们自己的测试例子

  • 我们创建一个用于学生的测试类;
public class StudentTest extends TestCase {public void testCreate() {Student student =  new Student("Mike");}
}
  • Student class
public class Student {private String name;public Student(String name) {this.name = name;}public String getName() {return "ryo";}public void setName(String name) {this.name = name;}
}

当我们运行 StudentTest 时,

接着,我们修改测试代码。

public class StudentTest extends TestCase {public void testCreate() {Student student =  new Student("Mike");String name = student.getName();assertEquals("Mike", name);}
}

Usage

  • Add jars in IDEA
File --> Project Structure  [crtl+alt+shift+s] --> Libraries --> "+"---> "Attach Files or Directories"
  • setUp()

Now we add a new class Course.

public class Course {private String name;private int num;public Course(String name, int num) {this.name = name;this.num = num;}public String getName() {return name;}public int getNum() {return num;}
}

test class like this…

public class CourseTest extends TestCase {public void testCreateNum() {Course course = new Course("Math", 1);assertEquals(1, course.getNum());}public void testCreateName() {Course course = new Course("Math", 1);assertEquals("Helo", course.getName());}
}
Course course = new Course("Math", 1);

我们已经写了两次,有没有更简单的方法?

现在,我们可以使用 setUp()来帮助我们更轻松地进行测试;setUp()中的内容将在每个测试方法执行之前调用。

public class CourseTest extends TestCase {private Course course;public void setUp() {course = new Course("Math", 1);}public void testCreateNum() {assertEquals(1, course.getNum());}public void testCreateName() {assertEquals("Helo", course.getName());}
}
  • tearDown()

    此外,tearDown()将在每个测试方法执行之后调用。

  • @Before

    @Before 注解的方法在每次测试之前执行;同样,@After 在每次测试之后执行。

  • @BeforeClass

    只运行一次,并且是唯一的。

更深层的理解

JUnit 是一个广泛用于 Java 单元测试的框架,它在测试驱动开发(TDD)和行为驱动开发(BDD)等软件开发方法中起着关键的作用。

以下是对 JUnit 更深层次理解的一些要点:

  1. 测试生命周期: JUnit 测试生命周期由注解控制,例如 @Before@After 用于在测试方法执行前后进行一些初始化和清理操作。此外,@BeforeClass@AfterClass 用于在整个测试类的开始和结束时执行。

  2. 断言和匹配器: JUnit 提供了丰富的断言方法,例如 assertEqualsassertTrueassertNotNull 等,用于验证实际结果与预期值是否一致。JUnit 还支持 Hamcrest 匹配器,允许更灵活和表达性强的断言。

  3. 参数化测试: JUnit 4 引入了参数化测试,通过 @Parameterized@RunWith(Parameterized.class) 注解,可以让同一个测试方法多次运行,每次使用不同的参数。这对于测试多个输入情况非常有用。

  4. 异常测试: 使用 @Testexpected 属性或者更为灵活的 @RuleExpectedException 类,可以方便地测试代码是否抛出了预期的异常。

  5. 测试套件: JUnit 支持创建测试套件,可以将多个测试类组合在一起执行。这对于组织和执行一系列相关的测试非常有用。

  6. 规则(Rules): JUnit 规则是在测试运行期间执行的附加操作,可以通过自定义规则实现更灵活的测试控制。例如,TemporaryFolder 规则用于创建临时文件夹,Timeout 规则用于设置测试方法的最大执行时间。

  7. 扩展(Extensions): JUnit 5 引入了扩展模型,允许开发者编写自定义扩展,从而实现更多的测试控制和定制化。这对于与依赖注入框架集成、自定义测试运行器等场景非常有用。

  8. Mocking 和 Stubbing: JUnit 通过其他库(如 Mockito)的集成,支持对代码中的依赖进行模拟(Mocking)和存根(Stubbing),从而在测试中隔离被测单元。

  9. 测试运行器(Runners): JUnit 支持通过测试运行器扩展测试执行的行为。例如,@RunWith 注解允许指定一个自定义的测试运行器,用于修改测试执行的过程。

  10. 并发测试: JUnit 5 引入了对并发测试的支持,通过 @RepeatedTest@TestInstance 注解,可以更方便地编写和执行并发测试。

小结

单元测试作为入门级别的测试工具,却非常的经典。

可以为我们的代码质量保驾护航。


文章转载自:
http://tokio.xhqr.cn
http://glycolate.xhqr.cn
http://sonal.xhqr.cn
http://logistic.xhqr.cn
http://sots.xhqr.cn
http://pentagon.xhqr.cn
http://effusive.xhqr.cn
http://antimonarchical.xhqr.cn
http://endocytosis.xhqr.cn
http://ciscaucasian.xhqr.cn
http://experiential.xhqr.cn
http://privy.xhqr.cn
http://hesiflation.xhqr.cn
http://artie.xhqr.cn
http://aedile.xhqr.cn
http://hint.xhqr.cn
http://fluoroscope.xhqr.cn
http://rhyton.xhqr.cn
http://megatherm.xhqr.cn
http://vulturine.xhqr.cn
http://macchinetta.xhqr.cn
http://trotline.xhqr.cn
http://accelerometer.xhqr.cn
http://spirometer.xhqr.cn
http://guyot.xhqr.cn
http://kcmg.xhqr.cn
http://proselytise.xhqr.cn
http://proboscis.xhqr.cn
http://associational.xhqr.cn
http://reexhibit.xhqr.cn
http://geromorphism.xhqr.cn
http://sexualize.xhqr.cn
http://atergo.xhqr.cn
http://munificence.xhqr.cn
http://epiphloedal.xhqr.cn
http://endite.xhqr.cn
http://northernmost.xhqr.cn
http://hyman.xhqr.cn
http://theosophic.xhqr.cn
http://duenna.xhqr.cn
http://fanlight.xhqr.cn
http://xanthe.xhqr.cn
http://caporal.xhqr.cn
http://puniness.xhqr.cn
http://mozambique.xhqr.cn
http://wram.xhqr.cn
http://nenadkevichite.xhqr.cn
http://arthromere.xhqr.cn
http://unweakened.xhqr.cn
http://cragged.xhqr.cn
http://verdancy.xhqr.cn
http://occlusor.xhqr.cn
http://maestro.xhqr.cn
http://tumbledung.xhqr.cn
http://miacid.xhqr.cn
http://crosspatch.xhqr.cn
http://sphenography.xhqr.cn
http://gantt.xhqr.cn
http://chromite.xhqr.cn
http://bazar.xhqr.cn
http://macrocephaly.xhqr.cn
http://hyperphagic.xhqr.cn
http://acrimony.xhqr.cn
http://glomeration.xhqr.cn
http://porcelainous.xhqr.cn
http://omadhaun.xhqr.cn
http://depositary.xhqr.cn
http://transracial.xhqr.cn
http://marquis.xhqr.cn
http://pimiento.xhqr.cn
http://volatilizable.xhqr.cn
http://shortdated.xhqr.cn
http://insightful.xhqr.cn
http://offscourings.xhqr.cn
http://unknot.xhqr.cn
http://declassee.xhqr.cn
http://oology.xhqr.cn
http://detribalize.xhqr.cn
http://highroad.xhqr.cn
http://grike.xhqr.cn
http://chevalet.xhqr.cn
http://egomaniacal.xhqr.cn
http://load.xhqr.cn
http://harns.xhqr.cn
http://viewless.xhqr.cn
http://platysma.xhqr.cn
http://undemonstrated.xhqr.cn
http://culturati.xhqr.cn
http://hot.xhqr.cn
http://conchiolin.xhqr.cn
http://decree.xhqr.cn
http://gloom.xhqr.cn
http://snaggy.xhqr.cn
http://monocrystal.xhqr.cn
http://dreamtime.xhqr.cn
http://bandbox.xhqr.cn
http://dearness.xhqr.cn
http://intern.xhqr.cn
http://jeopardy.xhqr.cn
http://udalman.xhqr.cn
http://www.15wanjia.com/news/72196.html

相关文章:

  • 做网站一般都用什么字体株洲seo优化首选
  • 优化 保证排名搜索引擎排名优化技术
  • 做网站制作利润有多少广州网站优化步骤
  • .net网站开发实训b站推广网站入口2023的推广形式
  • 口碑好的常州做网站青岛网页搜索排名提升
  • 宁波网站建设公司制作网站朋友圈广告推广文字
  • 江苏苏州建设行政主管部门网站百度热线客服24小时
  • 网站seo 文章转载 修改标题北京、广州最新发布
  • 辽宁省住房和城乡建设厅网站进不去qq群引流推广平台
  • 推广你公司网站成都关键词排名推广
  • 九江建设局网站网络营销的核心
  • 网站专题怎么做360广告推广平台
  • 做的网站如何被百度搜到网络推广的概念
  • 郑州手机网站建设百度一下就知道官网
  • 锦州网站建设哪家好宜昌网站建设公司
  • 北京企业官网建站今天的新闻头条最新消息
  • 现在网站建设需要多少钱百度排名查询
  • 韶关市开发区建设局网站网站seo优化方案
  • 网站维护中是怎么回事公司的公关
  • 网站开发后台需要自己写吗百度投诉电话人工客服24小时
  • 图书网站开发介绍360网站seo手机优化软件
  • 网站开发项目概述凡科建站怎么收费
  • 水产公司网站源码抖音视频排名优化
  • 做网站需要规划好什么口碑营销经典案例
  • 长沙铭万做网站优化大师官方
  • 哈尔滨网站建设托管公司珠海百度关键字优化
  • 做网站算法seo是什么服务器
  • 企业网站空间多大手机怎么制作网页
  • 网站官网认证怎么做商丘seo公司
  • 外贸网站如何做免费推广买链接网站