Junit @Rule如何运作? [英] How does Junit @Rule work?

查看:105
本文介绍了Junit @Rule如何运作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为大量代码编写测试用例,我想知道JUnit @Rule 注释功能的详细信息,以便我可以用它来编写测试案例。请提供一些好的答案或链接,通过一个简单的例子详细说明其功能。

I want to write test cases for a bulk of code, I would like to know details of JUnit @Rule annotation feature, so that I can use it for writing test cases. Please provide some good answers or links, which give detailed description of its functionality through a simple example.

推荐答案

规则用于添加其他功能适用于测试类中的所有测试,但是采用更通用的方式。

Rules are used to add additional functionality which applies to all tests within a test class, but in a more generic way.

例如, ExternalResource 在测试方法之前和之后执行代码,而不必使用 @Before @After 。使用 ExternalResource 而不是 @Before @After 给出更好的代码重用的机会;可以从两个不同的测试类中使用相同的规则。

For instance, ExternalResource executes code before and after a test method, without having to use @Before and @After. Using an ExternalResource rather than @Before and @After gives opportunities for better code reuse; the same rule can be used from two different test classes.

该设计基于: JUnit中的拦截器

了解更多有关信息,请参阅 JUnit wiki:Rules

For more information see JUnit wiki : Rules.

这篇关于Junit @Rule如何运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆