zoukankan      html  css  js  c++  java
  • Java的Junit与debug模式入门

    1.Junit的使用入门

    导入eclipse内置的junit—junit—junit相应的版本,方式1如下:

    方式二使用maven加载如下(见后续maven):

     <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.8</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    

    倘若需要找依赖请到官网上查找,例如如下

    https://junit.org/junit5/docs/current/user-guide/ 

     

    2.Junit4的常用的方法

    @Test  测试方法

    @Ignorde  忽略的测试方法

    @Before 在每一个方法测试之前运行

    @After 在每一个方法之后运行

    @BeforeClass 在所有测试方法之前运行,需要的静态的表达方式

    @AfterClass  在所有测试方法之后运行,需要是静态的表达方式

    public class JUnitTest {
    
            @After
            public void init() {
                     System.out.println("init");
            }
    
            @Before   
            public void destroy() {
                     System.out.println("after");
            }
    
    
            @Test
            public  void test() {
                     System.out.println("test");
            }      
    
            @BeforeClass
            public  static void  testBeforeClass() {
                     System.out.println("beforeClass");
            }
    
            @AfterClass
            public static void  testAfaterClass() {
                     System.out.println("afterClass");
            }
    
    }//beforeClass   before   test   after   afterClass
    

    所以顺序是beforeClass   before   test   after   afterClass

    对于junit5的常用方法(较junit4表达略有不同)

    @BeforeAll  只执行一次,在所有的测试方法之前运行(类似于 BeforeClass),需要是静态的方法。

    @BeforeAll   只执行一次,在所有的测试方法之前运行(类似于 AfterClass),需要的静态的方法

    @BeforeEach 在每个测试方法之前运行,类似于(@Before)

    @AfterEach  在每个测试方法之后运行,类似于(@After)

    @Test

    @DisplayName 更改在测试界面显示的名称。

    @Disabled  忽略(类似于@Ignore)

    public class JUnitTest {
    
        @BeforeAll
            public static  void  initBefore() {
                     System.out.println("beforeall");
            }
       
    
        @AfterAll
        public static void  destroyAfter() {
                System.out.println("destroyAfter");
        }   
    
        @Test
        @DisplayName("testHello")
        public void test(){
                System.out.println("test");
        }
    
        @BeforeEach
        public void  testBeforeEach() {
                System.out.println("beforeEach");
        }   
    
        @AfterEach
        public void  testAfterEach() {
                System.out.println("afterEach");
        } 
    
        @Test
        @Disabled
        @DisplayName("testHello")
        public void test1(){
                System.out.println("test");
        }  
    }
    

    输出结果依次为

    Beforeall   beforeEach   test   afterEach    destroyAfter

     

    Failures test运行错误

    Errors 程序自身的runtime错误

    3.Junit的断言

    public class JUnitAssert {
    
             @Test
               public   void standardAssertions() {
                    assertEquals(2, 2);
                    assertEquals(4, 4, "The optional assertion message is now the last parameter.");           assertTrue('a' < 'b', () -> "Assertion messages can be lazily evaluated -- "
                           + "to avoid constructing complex messages unnecessarily.");
                        }         
    
             @Test
              public  void groupedAssertions() {
                    assertAll("person",
                        () -> assertEquals("John", "John"),
                        () -> assertEquals("Doe", "Doe")
                    );
               }
    
            @SuppressWarnings("static-access")
            @Test
              public   void timeoutNotExceeded() throws InterruptedException {
                 assertTimeout(Duration.ofSeconds(2), ()->{ new Thread().sleep(1000);});
                }       
    
            @Test
            public  void assertWithHamcrestMatcher() {
            assertThat(2 + 1, is(equalTo(3)));
            assertThat(2 + 1, not(3));
            /*相应还有containsString endsWith startWith equlTo equalToIgnoringCase  allOf(比某大且比某小)
             anyOf(满足一个条件即可)  greatThan(大于) lessThan(小于)closeTO  正负  hasKey(map集合) hasValue(map集合)等 
         这些方法导包有些麻烦,需要复制import static org.hamcrest.CoreMatchers.not后将not更改为相应的方法  */
        }
    }
    
     
    

    还有很多其他的方法 ,可以相应的去https://junit.org/junit5/docs/current/user-guide/查看

    4.Debug模式

    断点 debug测试相关按钮,或者鼠标右键debug as  表示运行到了这一步是方法进入方法里面,指向下一行 是方法不进入方法里边,指向下一行,运行到下一个断点从方法里面跳出来的下一行 不运行方法里边的后续代码行,返回到当前方法行的起始位置。

    运行相关的变量参数表

    运行相关的信息

    5.unit4的问题点

    public class TestUnitQuestion {
    	private int i = 10;
    	//没有输出,也不会报错
    	@Test
    	public  void test() {
    		TestUnitQuestion volatileTest = new TestUnitQuestion();
    		VolatileTestD volatileTestD1 = volatileTest.new VolatileTestD();
    		VolatileTestD volatileTestD2 = volatileTest.new VolatileTestD();
    		ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(2);
    		newFixedThreadPool.submit(volatileTestD1);
    		newFixedThreadPool.submit(volatileTestD2);
    		newFixedThreadPool.shutdown();
    	}
    
    	// 每个线程自己有自己的内存,--验证共享内存机制
    	class VolatileTestC implements Runnable {
    		private boolean flag2 = true;
    
    		@Override
    		public void run() {
    			flag2 = false;
    		}
    	}
    
    	class VolatileTestD implements Runnable {
    		@Override
    		public void run() {
    			try {
    				Thread.sleep(100);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			while (i-- >0) {
    				System.out.println(Thread.currentThread().getName() + ":" + i);
    			}
    		}
    	}
    }
    

     备注:Junit测试类线程执行睡眠sleep()后次线程后面的程序不能进行;因为junit执行的程序必须是激活状态的。而sleep是睡眠状态,一旦执行就会自动退出程序。

  • 相关阅读:
    点击按钮icon input同步获取焦点
    修改vue项目中 elementUI input样式
    vue $route 和$router的区别
    vue 路由 vue-router 模式 hash history
    vue 路由跳转
    git 合并某个提交 git cherry-pick
    请求传参 有特殊符号
    前端 组件库 ,js等
    ajax请求成功,返回了数据,但是跳到了error情况
    微信-公众号-网页授权开发
  • 原文地址:https://www.cnblogs.com/gg128/p/9231008.html
Copyright © 2011-2022 走看看