您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > junit
IntelliJ IDEA 2016 Maven項(xiàng)目配置JUnit
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2017/6/14 11:04:15 ] 推薦標(biāo)簽:單元測(cè)試 單元測(cè)試工具

  添加插件:File->Settings->Plugins

  這里寫(xiě)圖片描述
  設(shè)置生成模式:File->Settings->Other Settings

  這里寫(xiě)圖片描述
  修改模板:File->Settings->Other Settings->Junit Generator->Junit4
#if( $entry.packageName.length()>0)package test.$entry.packageName; #end
    * @author:Hunter
    * @since:$today
    * @version 1.0
  在代碼上右鍵Generate
  結(jié)構(gòu)圖

  這里寫(xiě)圖片描述
  src/main/Java:source root
  src/test/java:test root
  ●Main.java
/**
 * Created by Hunter on 2016/07/19.
 */
import org.apache.Hadoop.conf.Configuration;


public class Main {

    public static void main(String[] args){
        for(String arg:args)
            System.out.println(arg);

    }

    public static String getColor(){
        Configuration conf =new Configuration();
        conf.addResource("Configuration-1.xml");
        return conf.get("color");
    }

    public static int getSize(){
        Configuration conf =new Configuration();
        conf.addResource("Configuration-1.xml");
        return conf.getInt("size",0);
    }
}
 ●自動(dòng)生成MainTest.java
import org.junit.Test;
import org.junit.Before;
import org.junit.After;

import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.hamcrest.Matchers.*;

import org.apache.log4j.Logger;

/**
* Main Tester.
*
* @author:Hunter
* @since:7/19/2016
* @version 1.0
*/
public class MainTest {
private static Logger logger=Logger.getLogger(MainTest.class);

@Before
public void before() throws Exception {
    logger.info("Test start……");
}

@After
public void after() throws Exception {
    logger.info("Test end……");
}

/**
*
* Method: main(String[] args)
*
*/
@Test
public void testMain() throws Exception {
    String[] args=new String[2];
    args[0]="Hello";
    args[1]="world";
    Main.main(args);
}

/**
*
* Method: getColor()
*
*/
@Test
public void testGetColor() throws Exception {
    assertThat(Main.getColor(),is("yellow"));
}

@Test
public void testGetSize() throws Exception{
    assertEquals(Main.getSize(),10);
}

}
 ●Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ggz</groupId>
    <artifactId>MRTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
        </dependency>
    </dependencies>
</project>

軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd