软件工程头歌实训作业:Junit实训入门篇

第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**************************/



   //execute before class
   @BeforeClass
   public static void afterClass() {
      System.out.println("in after class");
   }
 
   //execute after test
   
   @Before
   public void after() {
      System.out.println("in after");
   }
 
   //execute before test
   @After
   public void before() {
      System.out.println("in before");
   }
 
   //execute after class
   @AfterClass
   public static void beforeClass() {
      System.out.println("in before class");
   }
 
   //test case
   @Test
   public void test() {
      System.out.println("in test");
   }
 
   //execute before class
    /************************End***************************/
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值