java junit 对象,java-Junit:在多个测试类中使用同一测试对象

本文探讨了在JUnit中如何避免在每个测试类重复使用@Before注解的问题。作者介绍了TestRule的概念,它提供了一种更强大且可复用的方式来管理测试前后的行为。通过使用TestRule,可以更好地组织和共享测试资源,提升测试效率。
摘要由CSDN通过智能技术生成

我有多个单独的测试类,它们都使用相同的测试对象.目前,我在每个此类中都使用@Before,这显然不是一个很好的解决方案.

当然,一种选择是从创建该对象的抽象类继承.如here所述,这也不是一个好主意.

另一个选择是“外部资源”,但这就是-顾名思义-用于资源而不是测试对象.

我觉得我缺少一些东西,因为这必须是JUnit中的基本任务.

解决方法:

您可能正在寻找TestRule.要引用Javadoc:

A TestRule is an alteration in how a test method, or set of test methods, is run and reported. A TestRule may add additional checks that cause a test that would otherwise fail to pass, or it may perform necessary setup or cleanup for tests, or it may observe test execution to report it elsewhere. TestRules can do everything that could be done previously with methods annotated with Before, After, BeforeClass, or AfterClass, but they are more powerful, and more easily shared between projects and classes.

然后继续列出几种类型,其中ExternalResource是其中一种.但也有其他人.

鉴于您谈论的是比使用@Before方法做的更好,并且您希望在类之间共享,这听起来很像您在描述的内容.

标签:junit,java

来源: https://codeday.me/bug/20191026/1933442.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值