集成方式1 (下载jar包并添加到classpath中):
集成方式2 — 用maven:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
为了性能,可以手动一个一个引入:
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
如果怕麻烦,直接*号引入所有。
import static org.hamcrest.CoreMatchers.×;
import static org.junit.Assert.*;
常规用法不赘述。
@Suite.SuiteClasses聚合测试(用来一次性运行多个测试类):
代码:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
ATest.class,
BTest.class
})
public class SuiteDemo {
}
测试这个类,@Suite.SuiteClasses{}中包含的类的所有测试方法都会被执行。
@Ignore忽略测试(有的时候,某个方法出错,但是又不想改,先忽略它)
@Ignore("Test is ignored as a demonstration")
@Test
public void testSame() {
assertThat(1, is(1));
}
测试的时候就不会运行这个方法了。
junit4官网地址:
https://junit.org/junit4/