使用Java JUnit框架裡的@SuiteClasses註解管理測試用例
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-2703835/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用Java JUnit框架裡的@Rule註解的用法舉例Java框架
- JUnit 註解@SuiteClasses的工作原理UI
- Java JUnit框架裡@Category註解的工作原理Java框架Go
- IDEA中用junit寫基本測試用例Idea
- 登入註冊的測試用例
- junit測試工具運用
- 自動的自動化:EvoSuite 自動生成JUnit的測試用例UI
- 大型專案裡Flutter測試應用例項整合測試深度使用Flutter
- 1.13-java單元測試junitJava
- TestLink測試用例管理工具使用說明
- 『測試基礎』| 如何理解測試用例管理和缺陷管理?
- 測者的測試技術手冊:自動的自動化EvoSuite 自動生成JUnit的測試用例UI
- Java新一代單元測試框架JUnit5速覽Java框架
- JUnit 註解@RunWith的工作原理
- JUnit 註解@Category的工作原理Go
- JUnit 註解@Rule的工作原理
- 使用JUnit進行單元測試
- 測試——水杯的測試用例
- 實用指南:使用Pytest Allure測試框架新增用例失敗截圖框架
- springboot junit測試Spring Boot
- 有用 git 管理功能測試用例的公司嗎?Git
- Java Junit單元測試(入門必看篇)Java
- Java單元測試之JUnit 5快速上手Java
- Spring框架裡註解@Autowired的工作原理Spring框架
- 測試用例
- 測試面試-測試用例面試
- 測試用例的方法
- 使用 JUnit 5.7 進行引數化測試:深入瞭解 @EnumSource
- Java註解的使用Java
- Junit 4 測試方法
- 【JUnit測試】總結
- 手工測試用例與自動化測試用例的區別
- 【黑盒測試】測試用例的常用方法
- 【Java】跳躍表的實現以及用例測試Java
- Java-進階篇【Junit單元測試、反射、註解、動態代理、XML、XML解析、XPath、設計模式】---10Java反射XML設計模式
- 測試用例和測試方法
- 【5】測試用例
- 測試用例—教室