[Java] Junit Quick Start

Junit Introduction

  JUnit is a unit testing framework for the Java language. It was founded by Kent Beck and Erich Gamma, has gradually become due sUnit Kent Beck's xUnit family of one of the most successful. JUnit JUnit extension has its own ecosystem. Most Java development environment have been integrated as a tool for JUnit unit testing. 

  JUnit is written by Erich Gamma and Kent Beck a regression testing framework (regression testing framework). Junit test is a programmer tests, the so-called white-box testing, because of how the software being tested (How) completion and what (What) function to complete the programmers know. Junit is a framework, inheritance TestCase class, it can be automatically tested with Junit.

Simple to use Junit

  1, a new common maven project, dependent on the introduction of

1 <dependency>
2     <groupId>junit</groupId>
3     <artifactId>junit</artifactId>
4     <version>4.12</version>
5 </dependency>

 

  2, a new test class JunitTest.java, and written into a non-parametric, non-return value is an ordinary method, the method in annotate @Test, as follows

. 1  Package com.test.junite;
 2  
. 3  Import org.junit.Test;
 . 4  
. 5  public  class JunitTest {
 . 6  
. 7      @Test
 . 8      public  void testJunit () {
 . 9          System.out.println ( "test Junit, using simple ... " );
 10      }
 11 }

 

  3, the test results

    

Junit common comment

  1, associated with the testing process four notes

. 1  Package com.test.junite;
 2  
. 3  Import org.junit.After;
 . 4  Import org.junit.AfterClass;
 . 5  Import org.junit.Before;
 . 6  Import org.junit.BeforeClass;
 . 7  Import org.junit.Test;
 . 8  
. 9  public  class JunitFlowTest {
 10  
. 11      / ** 
12 is       *. 1, @ BeforeClass modified method will be executed before the method is invoked for all
 13       * and the method is static, then all the test runs it when the class is loaded
 14       * and it exists in memory only a example, it is suitable for loading a configuration file
 15       * 2, @ AfterClass the modified method is usually used to clean up resources, such as closing the connection to the database
 16      * 3, @ Before and @After be executed once for each test method before and after
 . 17       * 
 18 is       * / 
. 19      
20 is      @BeforeClass
 21 is      public  static  void setUpBeforeClass () throws Exception {
 22 is          System.out.println ( "IS @BeforeClass the this ... " );
 23 is      }
 24  
25      @AfterClass
 26 is      public  static  void tearDownAfterClass () throws Exception {
 27          System.out.println (" IS @AfterClass the this ... " );
 28      }
 29  
30      @Before
31     public void setUp() throws Exception {
32         System.out.println("this is @Before ...");
33     }
34 
35     @After
36     public void tearDown() throws Exception {
37         System.out.println("this is @After ...");
38     }
39 
40     @Test
41     public void test1() {
42         System.out.println("this is test1 ...");
43     }
44     
45     @Test
46     public void test2() {
47         System.out.println("this is test2 ...");
48     }
49 
50 }

 

    Results are as follows:

      

  2, associated with @Test

. 1  Package com.test.junite;
 2  
. 3  Import  static org.junit.Assert.assertEquals;
 . 4  
. 5  Import org.junit.Ignore;
 . 6  Import org.junit.Test;
 . 7  
. 8  public  class AnnotationTest {
 . 9  
10      / ** 
. 11       * @Test: the modification of a conventional method as a test method
 12 is       * @Test (expected = XX.class)
 13 is       * @Test (timeout = ms)
 14       * @BeforeClass: it will be executed before any operation method, static modification
 15       * @AfterClass: it will be executed after running all methods, static modification
 16       * @Before: will be executed before each test method run once
17       * @After: will be executed once at each test method runs after
 18       * the @Ignore: The modified test method will be ignored test runner
 19       * @RunWiht: You can change the test runner org.junit.runner.Runner
 20       * / 
21 is      @Test (. = an ArithmeticException expected class )
 22 is      public  void testDivide () {
 23 is          the assertEquals (. 3,. 3/0 );
 24      }
 25      
26 is      @Test (timeout = 2000 )
 27      public  void The testTimeout () {
 28          the while ( to true ) {
 29              System.out.println ( "RUN Forever ..." );
30         }
31     }
32     
33     @Ignore
34     @Test
35     public void testIgnore() {
36         System.out.println("this is testIgnore ...");
37     }
38 }

Write Junit test class principles

  1, the test methods must be modified to use @Test

  2, the test method must use public void modified, you can not take any arguments

  3. Create a directory to hold the source code for our test

  4, the test packet class should be consistent test class

  5, test unit must be able to independently test each method, there can be no dependencies between Test Method

  6, the test using the Test class as the class name suffix (non-essential)

  7, the test method using test method as a prefix (optional)

 

Guess you like

Origin www.cnblogs.com/h--d/p/11415893.html