使用Java JUnit框架裡的@Rule註解的用法舉例
Suppose you need to repeatedly execute some test method in your unit test case, for example, you would like to test getPrice based on the first set of test data 5 times in test method test1() while for the second set of test data, only one time should be executed.
The below class RepeatDemoOne is a bad example, where this special LOOP operation is mixed with test method implementation.
Ideally the test method should only contain the pure logic to operate on the method being tested. So we have a better solution RepeatDemoTwo: It could easily be observed that now the test method test1 and test2 are rather clean: no more for LOOP and System.out.println exist any more.
Instead, I put the LOOP logic and print out operation into class RepeatableRule which implements interface MethodRule. The concrete rule implementation is done by overriding method apply as below:
class RepeatableRule implements MethodRule{
int times = 1;
String[] testMethods = null;
RepeatableRule(int times, String[] testMethods){
this.times = times;
this.testMethods = testMethods;
}
@Override
public Statement apply(final Statement base, final FrameworkMethod method, Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
int loopTime = 1;
if(Arrays.asList(testMethods).contains(method.getName())) {
loopTime = times;
}
for(int i = 0; i < loopTime; i++ ) {
base.evaluate();
System.out.println(method.getName() + " executed.");
}
}
};
} }
When I execute this test case, I can get exactly the same result as RepeatDemoOne:
With the help of @Rule, we can achieve the same as @Test(expected=).
For example, we can use an instance of class ExpectedException to manually declare within a test method itself that a test method expects a given type of exception class.
Besides exception, we can also manually specify a sub string which is expected to appear in an error message, and add our custom error message in Junit report if a test method fails. See following code for example:
public class RuleWithException {
@Rule
public ExpectedException exp = ExpectedException.none();
@Test
public void expectMessage()
{
exp.expectMessage("Hello World");
throw new RuntimeException("Hello World will throw exception.");
}
@Test
public void expectCourse()
{
exp.expectCause(new BaseMatcher<IllegalArgumentException>()
{
public boolean matches(Object item)
{
return item instanceof IllegalArgumentException;
}
@Override
public void describeTo(org.hamcrest.Description description) {
description.appendText("Expected exception with type IllegalArgumentException "
+ "raised in test method! ");
}
});
Throwable cause = new IllegalArgumentException("Cause Test.");
throw new RuntimeException(cause);
}}
In this example, if we comment out line 46, the customed message defined in method describeTo will be printed out in JUnit console:
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2703834/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用Java JUnit框架裡的@SuiteClasses註解管理測試用例Java框架UI
- JUnit 註解@Rule的工作原理
- Java JUnit框架裡@Category註解的工作原理Java框架Go
- JUnit 註解@RunWith的工作原理
- JUnit 註解@Category的工作原理Go
- JUnit 註解@SuiteClasses的工作原理UI
- Day69.註解&列舉類的複習 -Java註解&列舉類Java
- Java enum列舉類詳解 列舉的常見用法Java
- Java 列舉 switch的用法Java
- HTTP 請求響應頭部欄位裡 ETAG 的用法舉例HTTP
- Java註解的使用Java
- Spring框架裡註解@Autowired的工作原理Spring框架
- Java 列舉(enum) 詳解7種常見的用法Java
- java中的註解使用Java
- jquery九大選擇器的用法舉例jQuery
- Java Stream六個使用舉例Java
- Java反射和註解基本用法Java反射
- Java列舉類、註解和反射Java反射
- mysql_config_editor用法舉例MySql
- Python qutip用法(舉例介紹)Python
- 【mysql】SUBSTRING_INDEX 用法舉例MySqlIndex
- Java註解與反射的使用Java反射
- torch.argmin()的使用舉例
- JAVA反射舉例Java反射
- Java基礎(十)——列舉與註解Java
- Java裡的Character類的基本用法Java
- Java 如何優雅的使用註解Java
- Java註解解析-搭建自己的註解處理器(CLASS註解使用篇)Java
- 【Java註解用法】@Autowired 與@Resource的區別以及@Qualifier的介紹Java
- Java ”框架 = 註解 + 反射 + 設計模式“ 之 註解詳解Java框架反射設計模式
- JUnit5註解學習指引
- Scrapy框架的使用之Selector的用法框架
- Scrapy框架的使用之Spider的用法框架IDE
- Selenium用法詳解 -- Selenium3 自動化測試 frame多種用法舉例
- 在SAP ABAP裡使用註解@Inject模擬Java SpringJavaSpring
- @Query註解的用法(Spring Data JPA)Spring
- Spring 快取註解@Cacheable的用法Spring快取
- Java列舉類與註解詳解——一篇文章讀懂列舉類與註解詳Java