有点需要注意,当 JUnit 主线程退出,子线程也会跟着退出,需要使用子线程的 join() 方法使主线程等待

Maven 依赖

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
// src/main/java/club/seliote/App.java
package club.seliote; public class App
{
public int add(int first, int second) {
return first + second;
} public int exception() {
throw new NullPointerException();
}
}
// src/test/java/club/seliote/AppTest.java
package club.seliote; import org.junit.*; /**
* Unit test for simple App.
*/
public class AppTest
{
/**
* 用于保存 @Before 生成的数据
*/
private App app = null; /**
* 每个测试方法执行前都需要重新构造对象
*/
public AppTest() {
super();
System.out.println("#Constructor");
} /**
* static 方法,测试类中第一个执行
*/
@BeforeClass
public static void beforeClassTest() {
System.out.println("#BeforeClass");
} /**
* 初始化一些每个 @Test 方法都会用到的数据
*/
@Before
public void beforeTest() {
this.app = new App();
System.out.println("#Before");
} @Test
public void testAdd() {
System.out.println("#TestAdd");
Assert.assertEquals(6, this.app.add(3, 3));
if (8 != this.app.add(1, 7)) {
Assert.fail("Something error!");
}
} /**
* 超时测试
*/
@Test(timeout = 100)
public void testTimeout() {
Math.pow(10, 100);
} /**
* 异常测试
*/
@Test(expected = NullPointerException.class)
public void testIO() {
System.out.println("#TestException");
this.app.exception();
} @After
public void afterTest() {
this.app = null;
System.out.println("#After");
} @AfterClass
public static void afterClassTest() {
System.out.println("#AfterClass");
}
} // output :
// #BeforeClass
// #Constructor
// #Before
// #After
// #Constructor
// #Before
// #TestAdd
// #After
// #Constructor
// #Before
// #TestException
// #After
// #AfterClass

最新文章

  1. [VB] if 判断语句 和 If、IIf函数的比较
  2. tyvj1114 搭建双塔
  3. Android ProgressBar分析及自定义ProgressBar
  4. Erlang环境用eclipse搭建
  5. SVN 文件解锁
  6. BpBinder 转换为 BpCameraService 流程
  7. jQuery 选择器语法
  8. 【海岛帝国系列赛】No.2 海岛帝国:“落汤鸡”市的黑帮危机
  9. php获取客户端浏览器以及操作系统信息的方法
  10. 骇客(Hacker)用语
  11. 《DDNS服务器的搭建和案例解决方法》
  12. POJ1426Find The Multiple
  13. hive 经常使用命令
  14. BZOJ 1593: [Usaco2008 Feb]Hotel 旅馆
  15. 幻灯片(响应式设计)(jquery实现)
  16. cocos2dx 自己主动加入cpp文件到android.mk
  17. effective java读书小记(一)创建和销毁对象
  18. C#中string的小结
  19. C#中使用SHA1和MD5加密字符串
  20. [CodeForces10D]LCIS(最长公共上升子序列) - DP

热门文章

  1. 关于基于Linphone的视频通话Android端开发过程中遇到的问题
  2. ActionMethod_DMI_动态方法调用
  3. 只为更快、更省、更安全的 Azure CDN
  4. c++基础(积少成多)
  5. win10下各种问题的解决办法
  6. vos设置禁止被叫特定号码段特定区域
  7. C++11 新特性之 序列for循环
  8. Gym - 100004A 树的性质
  9. Java继承和访问修饰符
  10. 2017.11.27 用Servlet在JSP中加入验证码