第2關:Junit注解
任務描述
給出一個帶有注解的Junit代碼及其代碼打印輸出,要求學員修改注解位置,讓輸出結果變為逆序。
相關知識
Junit注解
Java注解((Annotation)的使用方法是"@ + 注解名" 。借助注解,我們可以在編程中通過簡單的注解來實現一些功能。在junit中常用的注解有 @Test、@Ignore、@BeforeClass、@AfterClass、@Before、@After
下表列出了這些注釋的概括:
具體解釋如下:
1、@Test
,表明此方法為測試方法。
2、@Before
,用此注解修飾的方法在每個test方法運行前執行
3、@BeforeClass
,用此注解修飾的方法將在所有方法運行前被執行,是一個static方法,只執行一次。
4、@After
,用此注解修飾的方法在每個test方法運行后執行
5、@AfterClass
,用此注解修飾的方法將在所有方法運行后被執行,也是一個static方法,只執行一次。
6、@Ignore
,用此注解修飾的方法會被Junit忽略。
代碼示例
這里新建一個JunitAnnotation.java
,把上面所講的注解全部加到某個測試函數之前,這些注解的作用一目了然:
package com.trustie.junittest;
import static org.junit.Assert.*;
import java.util.*;
import org.junit.*;
public class AnnotationsTest {
private ArrayList testList;
@BeforeClass
public static void onceExecutedBeforeAll() {
System.out.println("@BeforeClass: onceExecutedBeforeAll");
}
@Before
public void executedBeforeEach() {
testList = new ArrayList();
System.out.println("@Before: executedBeforeEach");
}
@AfterClass
public static void onceExecutedAfterAll() {
System.out.println("@AfterClass: onceExecutedAfterAll");
}
@After
public void executedAfterEach() {
testList.clear();
System.out.println("@After: executedAfterEach");
}
@Test
public void EmptyCollection() {
assertTrue(testList.isEmpty());
System.out.println("@Test: EmptyArrayList");
}
@Test
public void OneItemCollection() {
testList.add("oneItem");
assertEquals(1, testList.size());
System.out.println("@Test: OneItemArrayList");
}
@Ignore
public void executionIgnored() {
System.out.println("@Ignore: This execution is ignored");
}
}
如果我們運行上面的測試,控制臺輸出將是下面:
@BeforeClass: onceExecutedBeforeAll
@Before: executedBeforeEach
@Test: EmptyArrayList
@After: executedAfterEach
@Before: executedBeforeEach
@Test: OneItemArrayList
@After: executedAfterEach
@AfterClass: onceExecutedAfterAll
編程要求
本關的編程任務是在JunitAnnotation.java
中修改測試函數對應的注解,使得原代碼輸出結果變為逆序。
本關涉及的代碼文件JunitAnnotation.java
的代碼如下:
package step2;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class JunitAnnotation {
/*
*以下Junit測試程序的輸出結果為:
*in before class
*in before
*in test
*in after
*in after class
*請修改下面Begin/End內各個測試函數的注解,使輸出結果逆序
*/
/***********************Begin**************************/
//execute before class
@BeforeClass
public static void beforeClass() {
System.out.println("in before class");
}
//execute after class
@AfterClass
public static void afterClass() {
System.out.println("in after class");
}
//execute before test
@Before
public void before() {
System.out.println("in before");
}
//execute after test
@After
public void after() {
System.out.println("in after");
}
//test case
@Test
public void test() {
System.out.println("in test");
}
/************************End***************************/
}
評測說明
本關卡的測試文件是TestRunner.java
,該文件進行了函數封裝且學員不可見,用于驗證學員的Junit測試代碼是否正確。
具體測試過程如下:
1.平臺自動編譯生成TestRunner.exe
; 2.平臺運行TestRunner.exe
; 3.獲取TestRunner.exe
輸出,并將其輸出與預期輸出對比:如果一致則測試通過,否則測試失敗。
預期輸入: 預期輸出:
in after class
in after
in test
in before
in before class
true
友情提示
1.請不要直接println
最終輸出,否則平臺發現此類情況后,將一律扣掉本關經驗值,并且追加處罰措施。
2.學員答題時請盡量手敲代碼,請勿從實訓講解代碼片段中復制代碼段粘貼到答題區域作答,復制的內容會保留一些格式和字符,導致編譯失敗。
開始你的任務吧,祝你成功!
代碼如下
package step2;import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;public class JunitAnnotation {/**以下Junit測試程序的輸出結果為:*in before class*in before*in test*in after*in after class*請修改下面Begin/End內各個測試函數的注解,使輸出結果逆序*//***********************Begin**************************/@BeforeClasspublic static void afterClass() {System.out.println("in after class");}@Beforepublic void after() {System.out.println("in after");}@Afterpublic void before() {System.out.println("in before");}@AfterClasspublic static void beforeClass() {System.out.println("in before class");}@Testpublic void test() {System.out.println("in test");}/************************End***************************/
}