zoukankan      html  css  js  c++  java
  • Junit4学习(三)Junit运行流程

    一,验证Junit测试方法的流程

    1,在test/com.duo.util右键,新建测试类

    2,生成后的代码:

     1 package com.duo.util;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import org.junit.After;
     6 import org.junit.AfterClass;
     7 import org.junit.Before;
     8 import org.junit.BeforeClass;
     9 import org.junit.Test;
    10 
    11 public class JunitFlowTest {
    12 
    13     @BeforeClass
    14     public static void setUpBeforeClass() throws Exception {
    15         System.out.println("This is @BeforeClass...");
    16     }
    17 
    18     @AfterClass
    19     public static void tearDownAfterClass() throws Exception {
    20         System.out.println("This is AfterClass...");
    21     }
    22 
    23     @Before
    24     public void setUp() throws Exception {
    25         System.out.println("This is Before...");
    26     }
    27 
    28     @After
    29     public void tearDown() throws Exception {
    30         System.out.println("This is After...");
    31     }
    32     
    33     @Test
    34     public void test1(){
    35         System.out.println("This is test1...");
    36     }
    37     
    38     @Test
    39     public void test2(){
    40         System.out.println("This is test2...");
    41     }
    42 
    43 }

    运行结果:

    This is @BeforeClass...
    This is Before...
    This is test1...
    This is After...
    This is Before...
    This is test2...
    This is After...
    This is AfterClass...

    二,总结:

    1,@BeforeClass修饰的方法会在所有方法被调用前被执行;而且该方法是静态的,所以当测试类被加载后接着就会运行它,而且在内存中它只会存在一份实例,它比较适合加载配置文件;比如数据的连接文件等;

    2,@AfterClass所修饰的方法通常用来对资源的清理,如数据库的关闭;

    3,@Before和@After会在每个测试方法前后执行;通常被称为固定代码(fixure),就是一定会执行的代码.

  • 相关阅读:
    Spring Boot应用程序属性
    Spring Boot Bean和依赖注入
    Spring Boot构建系统
    Spring Boot Tomcat部署
    Spring Boot引导过程
    Spring Boot快速入门
    Spring Boot简介
    eclipse中将项目打包成jar的两种方法,及其问题与解决方法
    配置Zuul代理下游的认证
    WireMock和Spring MVC模拟器
  • 原文地址:https://www.cnblogs.com/august-shi/p/6720047.html
Copyright © 2011-2022 走看看