Junit测试常用注解

2 篇文章 0 订阅

Junit常用注解

0x01 摘要

本文简要说下junit里面常用注解的含义和使用,还会总结一些常用的Assert判断语句。

0x02 常用Junit注解

2.1 @Test

用在方法上,定义该方法是测试方法。

注意:测试方法必须是public void,但可以抛异常,不过一般不这么做。

例子:

@Test (timeout = 30000)
  public void testRMAppSubmitInvalidResourceRequest() throws Exception {
    asContext.setResource(Resources.createResource(
        YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB + 1));

    // submit an app
    try {
      appMonitor.submitApplication(asContext, "test");
      Assert.fail("Application submission should fail because resource" +
          " request is invalid.");
    } catch (YarnException e) {
      // Exception is expected
      // TODO Change this to assert the expected exception type - post YARN-142
      // sub-task related to specialized exceptions.
      Assert.assertTrue("The thrown exception is not" +
          " InvalidResourceRequestException",
          e.getMessage().contains("Invalid resource request"));
    }
  }

2.2 @BeforeClass

2.2.1 含义

用来修饰所有@Test方法之前只会运行一次的方法。

注意:需用public static void 修饰。

2.2.2 用途

当你的测试中多个方法都会用到的一些公共方法可以抽取到用@BeforeClass修饰的方法汇总,如创建数据库驱动等。

2.2.3 示例

@BeforeClass
public static void loadDriver(){
    try {
        Class.forName("org.apache.phoenix.queryserver.client.Driver");
    } catch (ClassNotFoundException e) {
        logger.error("an exception happened while load driver:", e);
    }
}

2.3 @AfterClass

2.3.1 含义

@AfterClass和前面提到的@BeforeClass类似,但是在所有@Test方法之后运行一次。

注意:需用public static void 修饰。

2.3.2 用途

一般他被来处理一些资源清理工作,如关闭数据库连接等。

2.3.3 示例

@AfterClass
public static void closeConnections(){
    for(Connection conn : connectionList){
        try {
            conn.close();
        } catch (SQLException e) {
            logger.error("an exception happened while close connection:", e);
        }
    }
}

2.4 @Before

2.4.1 含义

@Before@BeforeClass类似,他们最大区别在于@Before会在每个@Test方法运行之前都会运行一次。

注意:必须用public void修饰,不能加static

2.4.2 用途

用于每个测试用例都需要的单独的方法,比如获取插入一条记录到数据。

2.4.3 示例

@Before
public void setUp() {
  long now = System.currentTimeMillis();

  rmContext = mockRMContext(1, now - 10);
  ResourceScheduler scheduler = mockResourceScheduler();
  Configuration conf = new Configuration();
  ApplicationMasterService masterService =
      new ApplicationMasterService(rmContext, scheduler);
  appMonitor = new TestRMAppManager(rmContext,
      new ClientToAMTokenSecretManagerInRM(), scheduler, masterService,
      new ApplicationACLsManager(conf), conf);

  appId = MockApps.newAppID(1);
  RecordFactory recordFactory = RecordFactoryProvider.getRecordFactory(null);
  asContext =
      recordFactory.newRecordInstance(ApplicationSubmissionContext.class);
  asContext.setApplicationId(appId);
  asContext.setAMContainerSpec(mockContainerLaunchContext(recordFactory));
  asContext.setResource(mockResource());
  setupDispatcher(rmContext, conf);
}

2.5 @After

2.5.1 含义

@After@AfterClass类似,他们最大区别在于@After会在每个@Test方法运行之后都会运行一次。

注意:必须用public void修饰,不能加static

2.5.2 用途

可以用来清理每个@Test@Before创建的单独资源、恢复测试初始状态等。

2.5.3 示例

@After
public void tearDown() {
  setAppEventType(RMAppEventType.KILL);
  ((Service)rmContext.getDispatcher()).stop();
}

2.6 @Runwith

2.6.1 含义

首先要分清几个概念:测试方法、测试类、测试集、测试运行器:

  • 其中测试方法就是用@Test注解的一些函数
  • 测试类是包含一个或多个测试方法的一个Test.java文件
  • 测试集是一个suite,可能包含多个测试类
  • 测试运行器则决定了用什么方式偏好去运行这些测试集/类/方法

而@Runwith就是放在测试类名之前,用来确定这个类怎么运行的。也可以不标注,会使用默认运行器。

常见的运行器有:

  1. @RunWith(Parameterized.class) 参数化运行器,配合@Parameters使用junit的参数化功能
  2. @RunWith(Suite.class)
    @SuiteClasses({ATest.class,BTest.class,CTest.class})

测试集运行器配合使用测试集功能

  1. @RunWith(JUnit4.class)

junit4的默认运行器

  1. @RunWith(JUnit38ClassRunner.class)

用于兼容junit3.8的运行器

  1. 一些其它运行器具备更多功能。例如@RunWith(SpringJUnit4ClassRunner.class)集成了spring的一些功能

2.7 @Parameters

用于使用参数化功能。

2.8 @Rule和@TestRule

2.8.1 含义

  • @Rule
    方法级别,每个测试方法执行时都会执行被@Rule注解的成员变量的实现自TestRuleapply方法,和@Before类似。

  • @ClassRule
    类级别,测试类执行时仅会执行一次被@ClassRule注解的静态变量的方法,和@BeforeClass)类似。

2.8.2 用途

Before注解只能作用域当前测试类和子类,而实现了TestRule的类可以被用于多个测试类,可增加代码复用度。

2.8.3 示例

  • Timeout
    为所有测试方法指定统一超时时间
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.rules.Timeout;
    
    import java.util.concurrent.TimeUnit;
    
    public class TimeoutTest {
        @Rule
        public Timeout timeout = new Timeout(1000,TimeUnit.MILLISECONDS);
    
        // 测试失败,超时
        // org.junit.runners.model.TestTimedOutException: test timed out after 1000 milliseconds
        @Test
        public void test1() throws Exception {
            Thread.sleep(2000);
        }
    
        // 测试成功
        @Test
        public void test2() throws Exception {
            Thread.sleep(500);
        }
    
        // 测试成功
        @Test
        public void test3() throws Exception {
            Thread.sleep(1000);
        }
    }
    
  • TestName
    可获取测试方法名字
    import static org.junit.Assert.*;
    import org.junit.*;
    import org.junit.rules.TestName;
    public class TestNameTest {
    	@Rule
    	public TestName name = new TestName();
    
    	@Test
    	public void testA() {
    		System.out.println(name.getMethodName());
    		assertEquals("testA", name.getMethodName());
    
    	}
    
    	@Test
    	public void testB() {
    		System.out.println(name.getMethodName());
    		// fail
    		assertEquals("testC", name.getMethodName());
    	}
    }
    
  • TemporaryFolder
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.rules.TemporaryFolder;
    
    import java.io.File;
    import java.io.IOException;
    
    /**
     * @Author: chengc
     * @Date: 2020-03-12 12:57
     */
    public class TemporaryFolderTest {
        // 使用系统临时目录,也可在构造方法上加入路径来指定临时目录
        @Rule
        public TemporaryFolder tempFolder = new TemporaryFolder(new File("/Users/chengc/cc/work/projects/javaDemos/src/main/resources"));
    
        @Test
        public void testTempFolderRule() throws IOException {
            // 在系统的临时目录下创建目录,当测试方法执行完毕自动删除
            File directory = tempFolder.newFolder("test");
            System.out.println(directory.getAbsoluteFile());
    
    
            // 在系统的临时目录下创建文件,当测试方法执行完毕自动删除
            File file1 = tempFolder.newFile("tmptest.txt");
            File file2 = tempFolder.newFile("tmptest2.txt");
    
    
            System.out.println(file1.getName());
    
            System.out.println(file2.getParentFile());
    
            System.out.println(file2.getAbsoluteFile());
    
            System.out.println(file2.getCanonicalPath());
        }
    }
    
  • 自定义Rule
    实现TestRule即可:
    import org.junit.rules.TestRule;
    import org.junit.runner.Description;
    import org.junit.runners.model.Statement;
    
    /**
     * @Author: chengc
     * @Date: 2020-03-12 13:19
     */
    public class AgeRule implements TestRule {
        private int age;
        public AgeRule(int age){
            this.age = age;
        }
    
        public int getAge() {
            return age;
        }
    
        @Override
        public Statement apply(Statement base, Description description) {
            return new Statement() {
                @Override
                public void evaluate() throws Throwable {
                    //执行一次测试方法
                    base.evaluate();
                }
            };
        }
    }
    
    
    import org.junit.Assert;
    import org.junit.Rule;
    import org.junit.Test;
    
    import java.util.Random;
    
    /**
     * @Author: chengc
     * @Date: 2020-03-12 13:23
     */
    public class TestAgeRule {
        @Rule
        public AgeRule ageRule = new AgeRule(5);
    
        @Test
        public void test1(){
            int age = new Random().nextInt(100);
            if (age > ageRule.getAge()){
                Assert.fail("Beyond the age rule!");
            }
        }
    }
    

0xFF 参考文档

junit常用注解详细说明

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1.junit 常用注解 @Before 初始化方法,每次测试方法调用前都执行一次。 @After 释放资源:每次测试方法调用后都执行一次 @Test 测试方法:在这里可以测试期望异常和超时时间 @ignore 忽略的测试方法 @BeforeClass 针对所有测试,只执行一次,且必须为static void @AfterClass 针对所有测试,只执行一次,且必须为static void @RunWith 指定测试类使用的某个运行器参数SpringJUnit4ClassRunner.class @Parameters 指定参数类的参数数据集合 @Rule 允许灵活添加或重新定义测试类中的每个测试方法的行为 @FixMethodOrder 指定测试方法的执行顺序 @ContextConfiguration 参数locations="classpath:spring-mybatis.xml" 指向src下的该文件 执行顺序: @BeforeClass---@Before---@Test---@After---@Before ---@Test---@After---@AfterClass junit与main方法相比的优势:代码量少、结构清晰、灵活性更好 main:一个类中只能有一个main方0法 层次结构方面不够清晰 运行具体某一个方法时,要将其他的方法注释掉 2.mybatis的基本配置 1.dao层接口 2.mapper.xml:编辑需要执行的sql语句 (1)mapper标签的namespace属性:指定该xml对应的dao层接口的路径 3.spring-mybatis.xml:spring集成mybatils的配置文件 (1)配置sqlSessionFactory指定要操作的数据库,以及mapper.xml的所在目录 (2)配置指定的dao层接口的目录 3.mybatis的注意事项 1.xml中的sql不得有分号 2.sql语句操作的表明和列名 3.xml中的小于号:$lt;大于号¥> 4.取变量时,如果dao层接口使用的是@param("别名")注解,则根据别名取值 5.mapper.xml中$和#取值的区别 4.mybatis的xml中如何设置返回值 resultType返回的数据类型 5.$和#区别 1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。如:order by #{user_id},如果传入的值是111,那么解析成sql时的值为order by "111", 如果传入的值是id,则解析成的sql为order by "id". 2. $将传入的数据直接显示生成在sql中。如:order by ${user_id}, 如果传入 的值是id,则解析成的sql为order by id. 3. #方式能够很大程度防止sql注入。 4. $方式无法防止Sql注入。 5. $方式一般用于传入数据库对象,例如传入表名. 6. 一般能用#的就别用$ MyBatis排序时使用order by 动态参数时需要注意,用$而不是#

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值