Unit testing junit use

1.maven dependence

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito2</artifactId>
    <version>2.0.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>2.0.2</version>
    <scope>test</scope>
</dependency>

2. The method of presentation (junit)

(1) @BeforeClass: static method, the current class loader before the test call;

(2) @AfterClass: static method, the current recovery before the test class calls;

(3) @Before: call before @Test modified test method for each execution, commonly used in the initialization;

(4) @After: After each @Test modified test method calls the first performance;

(5) @Test: test method, every method must be asserted, there is no dependence between the test method, it can perform independently.

3. Test Sample

 Code tested:

 1 public class Calculator {
 2     private int init;
 3     public Calculator(int init) {
 4         this.init = init;
 5     }
 6     public int add(int a,int b) {
 7         return init+a+b;
 8     }
 9 
10 
11 }

testing method:

 1 import org.junit.Before;
 2 import org.junit.BeforeClass;
 3 import org.junit.Test;
 4 import static org.junit.Assert.*;
 5 
 6 public class CalculatorTest {
 7     private Calculator calculator;
 8     private static int init;
 9     @BeforeClass
10     public static void setup() {
11         init = 2;
12     }
13     @Before
14     public  void setup_1 () {
 15          Calculator = new new the Calculator (the init);
 16      }
 . 17      @Test
 18 is      public  void the add_ function return value _returnTrue setup_1 initialized () {
 . 19          the assertEquals (. 6, calculator.add (l, 3 )) ;
 20 is      }
 21 is  
22 is      @Test
 23 is      public  void the add_ return setup_1 function initializes values _returnFalse () {
 24          assertFalse (== calculator.add. 7 (l, 3 ));
 25      }
 26 is }

result:

 

 

 Test. @BeforeClass method for modifying initial value, @ Before the modified method initializes calculator objects, @ Test method for modifying test, simple seen, the initial value 2 plus 3 plus 1, the result is 6; Test Sample 1 the assertEquals assertion 6 satisfying result, correct; test results sample 2 assertFalse assertion comparison 6 7 with the calculated results are not equal to false, the condition is a pre-made false, the test passes.

4.powermock, class have static, native, private, final simulation methods.

Before the test classes plus:

@RunWith (PowerMockRunner. Class ) // tell JUnit tests using PowerMockRunner 
@PrepareForTest ({has a static method of class. Class }) // all need to test the classes listed here for analog final class or final, private , like static, native methods 
@PowerMockIgnore ( "in javax.management that. *") // to solve powermock after use, suggesting that classloader error

Before calling the static method:

PowerMockito.mockStatic (. The method of the class have static class ); 
When (class has static methods static method (the any (parameter 1.. Class ), the any (parameter 2. class ))) thenReturn ( "string");

Then call the static method

Class has a static method. Static method (parameter 1, parameter 2), the method just returns the value "string".

 

Guess you like

Origin www.cnblogs.com/carsonwuu/p/11580281.html