在MyEclipse 2014 中使用JUnit-(二)
2016-01-14 10:50
549 查看
当一个项目中存在较多case需要测试时,我们可以利用JUnit提供的Suite来测试。
步骤如下:
1.首先我们编写两个被测试的java文件,如下:
目前有两种方法可以
方法一:
方法二
步骤如下:
1.首先我们编写两个被测试的java文件,如下:
package com.jc.demo1; public class Demo1 { public int add(int a,int b){ return a+b; } public int minus(int a,int b){ return a-b; } }
package com.jc.demo1; public class Demo2 { public int divide(int a,int b){ return a/b; } public int mul(int a,int b){ return a*b; } }2.分别编写两个test case,如下:
package com.jc.demo1; import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestDemo1 { Demo1 demo1; @Before public void setUp() throws Exception { demo1=new Demo1(); } @After public void tearDown() throws Exception { } @Test public void testAdd() { int rel=demo1.add(12, 22); assertEquals("加法有问题",rel,34); } @Test public void testMinus() { int rel=demo1.minus(24, 12); assertEquals("减法有问题",rel,12); } }
package com.jc.demo1; import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestDemo2 { Demo2 demo2; @Before public void setUp() throws Exception { demo2=new Demo2(); } @After public void tearDown() throws Exception { } @Test public void testDivide() { int rel=demo2.divide(24, 12); assertEquals("除法有问题",rel,2); } @Test public void testMul() { int rel=demo2.mul(2, 12); assertEquals("乘法有问题",rel,24); } }
目前有两种方法可以
方法一:
package com.jc.demo1; import junit.framework.JUnit4TestAdapter; import junit.framework.Test; import junit.framework.TestSuite; public class SuiteTest2 { public static Test suite(){ TestSuite suite= new TestSuite(SuiteTest2.class.getName()); suite.addTest(new JUnit4TestAdapter(TestDemo1.class)); suite.addTest(new JUnit4TestAdapter(TestDemo2.class)); return suite; } }
方法二
package com.jc.demo1; import junit.framework.JUnit4TestAdapter; import junit.framework.Test; import junit.framework.TestSuite; public class SuiteTest2 { public static Test suite(){ TestSuite suite= new TestSuite(SuiteTest2.class.getName()); suite.addTest(new JUnit4TestAdapter(TestDemo1.class)); suite.addTest(new JUnit4TestAdapter(TestDemo2.class)); return suite; } }
相关文章推荐
- java流与文件——java生成解压缩文件(夹)
- java中应该尽量使用notifyall而不是notify(容易发生死锁)
- 每日一算法3--JAVA时间格式化处理
- spring 的MAVEN配置
- Eclipse中Java项目的红色感叹号和黄色盾牌感叹号
- 在Springmvc中导出报表下载Excel文件
- java配置数据库连接池的方法步骤
- Eclipse插件的安装方法三则
- java 验证码 基于servlet+jsp
- Linkedin工程师是如何优化他们的Java代码的
- 来学java吧
- Java图形化界面设计——容器(JFrame)
- Java中BigDecimal的8种舍入模式
- java notify wait
- 用java解析在OpenStreetMap上下载的地图数据(SAX版,适合比较大的xml文件)
- eclipse常用快捷键,这个只要新学会的常用的会陆续更新的。
- java160108TicketDemo2
- java160108TicketDemo
- java160108ThisLockDemo
- struts2常用标签总结