Junit testing framework using Java learning

Foreword

  In everyday development, unit testing can not do without, but also in learning Java, especially in testing different API uses to keep writing the main method, it can be tedious, so the introduction method using Junit learning Java here. In addition, I would use log4j output (not so used in the project, here only use). In order to facilitate everyone to understand, I simply summed up what simple test code and spread to GitHub on, I will follow continuously updated, everyone welcome criticism.

1. Junit introduction and use

  About 1.1 Junit

  1.1.1 Junit related identity

  @BeforeClass will be executed before all method calls, the method is static, when the test class is loaded and then run it more suitable to load configuration file.

  @AfterClass labeling method is also static, performed after all methods for cleaning up of resources, such as the release of the release of the database.

  @Before and @After are executed once before and after each test method.

  @Test a method to identify a test method. When in use are the following uses:

  1.      @Test (timeout = N), performing a similar scenario in the infinite loop, the end time can be set.
  2.      @Test (except = ...) can be received abnormal test method, the test does not make an error.

  1.1.2 Junit test suite

  To facilitate tissue run with a plurality of test classes may use the test kit, the following steps:

  1. Other methods of writing a class as the entry type does not contain the test suite;
  2. Change the test runner Suite.class;
  3. To be tested is passed as a parameter class SuiteClasses ({});

  1.1.3 Junit parameterization

  In the same, only the different parameters and the expected values ​​of the case where the code structure, method and the like, may be used in the parameterization Junit.

  1. The test runner instead Parameterized.class;
  2. Declare variables to hold the parameters and expected values;
  3. A constructor must be public;
  4. The method returns the value of public static Collection and use @Parameters modified;

  Furthermore, using the method to be tested in @Test identified class.

 1.2 Junit dependence of maven

  In order to use Junit testing framework, simply add the pom maven project file can be dependent, here also joined the log4j pom configuration file, version of the selection period can be set according to the actual situation, as follows:

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>    

2. Test the sample

  In naming the actual classes, methods, and recommended that the format for the test + class name / method name, it helps to understand, of course, this is not necessary.

import org.apache.log4j.Logger;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;

public class TestMap {
    private static final Logger logger=Logger.getLogger(TestMap.class);

    @Test
    public void testPut(){
        Map<String ,Integer> map=new HashMap<>();
        map.put("apple",123);
        map.put("banana",2345);
        logger.info(map.get("apple"));
        logger.info(map.put("apple",567));
        logger.info(map.get("apple"));
        logger.info(map.put("water",1000));
    }
    @Test
    public void testTraverse(){
        Map<String,Integer> map=new HashMap<>();
        map.put("a",1);
        map.put("b",2);
        map.put("c",3);
        assertEquals(map.get("c"),new Integer(3));

        for(String key:map.keySet()){
            System.out.println(map.get(key));
        }

        for(Map.Entry<String,Integer> entry:map.entrySet()){
            System.out.println(entry.getKey()+"="+entry.getValue());

        }
    }
}

   Comments welcome everyone to share their experiences!

 

Guess you like

Origin www.cnblogs.com/love-yh/p/11569908.html