设为首页 加入收藏

TOP

在Eclipse外使用JUnit测试
2014-11-24 08:08:00 来源: 作者: 【 】 浏览:0
Tags:Eclipse 使用 JUnit 测试

Eclipse IDE 集成了JUnit开源单元测试框架。如果不借助Eclipse的图形界面工具来生成并运行我们的JUnit测试,该怎么实现呢?


1. 首先需要在类路径下添加JUnit-4.X jar 包。


2. 编写需要测试的方法。


public class Calculator {


private Calculator(){}

public static int add(int x, int y) {
return x + y; // 正确
}

public static int subtract(int x,int y) {
return y - x; // 错误
}

public static int multiply(int x, int y) {
throw new RuntimeException(); // 抛出异常
}

public static int divide(int x, int y) {
return x / y; // divided by 0
}

public static int module(int x , int y) {
for(;;); //死循环
}


}


编写测试类(Test Class)


import static org.junit.Assert.*;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.zjut.junit.Calculator.*;


public class CalculatorTest {


@Before
public void setUp() throws Exception {
System.out.println("Before testing");
}


@After
public void tearDown() throws Exception {
System.out.println("After testing");
}


@Test
public void testAdd() {
assertEquals("add", 3, add(1, 2));
}


@Test
public void testSubtract() {
assertEquals("subtract", 3, subtract(5, 2));
}


@Test
public void testMultiply() {
assertEquals("multiply", 9, multiply(3, 3));
}


@Test(expected = ArithmeticException.class)
public void testDivide() {
divide(3, 0);
}


@Test(timeout=200)
public void testModule() {
module(1, 2);
}


}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇使用C和Shell实现远程Tomcat的重.. 下一篇C++实现线程池的经典模型

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·About - Redis (2025-12-26 08:20:56)
·Redis: A Comprehens (2025-12-26 08:20:53)
·Redis - The Real-ti (2025-12-26 08:20:50)
·Bash 脚本教程——Li (2025-12-26 07:53:35)
·实战篇!Linux shell (2025-12-26 07:53:32)