参考官网教程 http://junit.org/junit4/cookbook.html

使用步骤

1.POM中引入junit的依赖
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>

2.在方法上添加注解@org.junit.Test
  检查结果:org.junit.Assert.assertTrue

运行maven的dependency:tree查看依赖关系,可以看到junit又引入了hamcrest-core.jar.所以如果你不是用maven构建的,需要引入unit.jar和hamcrest-core.jar

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building junit 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ junit ---
[INFO] com.xh.learning:junit:jar:0.0.1-SNAPSHOT
[INFO] \- junit:junit:jar:4.12:compile
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.910s
[INFO] Finished at: Thu Dec 07 21:33:45 GMT+08:00 2017
[INFO] Final Memory: 9M/150M
[INFO] ------------------------------------------------------------------------

一个简单的单元测试Demo:

package com.xh.learning;

import static org.junit.Assert.assertTrue;
import org.junit.Test;


/** 
 * <Description> <br> 
 *  
 * @author luoluocaihong<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate Dec 7, 2017 <br>
 * @since V8.1<br>
 * @see com.xh.learning <br>
 */
public class MyJuintTest {

    @Test
    public void testHello() {
        String key = "Hello";
        assertTrue(key != null);
    }
}

results matching ""

    No results matching ""