`
yijingyong
  • 浏览: 158722 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

juit的使用

阅读更多
     很久前就接触了JUNIT,但是没有真正的使用它,今天又重温了一下,做个笔记,留个痕迹,不至于忘得太彻底,无从查证。
    在Eclipse中使用Junit的步骤:
1.在ECLIPSE菜单上单击”窗口“--“首选项”---JAVA ---构建路径----类路径变量---新建中加入junit.jar(在ELIPSE的PLUGINS中:plugins/org.JUnit_3.8.1).

2.如果需要可以把JUnit的愿代码添加。路径(/plugins/org.eclipse.jdt.sortce_3.12/org.junit_3.81/junitsrc.zip)

3.创建项目:名字叫做JUnitTest,加入调试需要的.jar文件。

4.建立一个文件夹:junit.samples.money,把要被调试的程序放入里面(一般是把测试用例和被测试类放在同一个包中的,但是为了区分,加强体系,我们建立两个包,一个放被测试类,一个放测试类)
被测试类:
package junit.samples.money;

/**
* The common interface for simple Monies and MoneyBags
*
*/
public interface IMoney {
/**
* Adds a money to this money.
*/
public abstract IMoney add(IMoney m);
/**
* Adds a simple Money to this money. This is a helper method for
* implementing double dispatch
*/
public abstract IMoney addMoney(Money m);
/**
* Adds a MoneyBag to this money. This is a helper method for
* implementing double dispatch
*/
public abstract IMoney addMoneyBag(MoneyBag s);
/**
* Tests whether this money is zero
*/
public abstract boolean isZero();
/**
* Multiplies a money by the given factor.
*/
public abstract IMoney multiply(int factor);
/**
* Negates this money.
*/
public abstract IMoney negate();
/**
* Subtracts a money from this money.
*/
public abstract IMoney subtract(IMoney m);
/**
* Append this to a MoneyBag m.
*/
public abstract void appendTo(MoneyBag m);
}

package junit.samples.money;

/**
* A simple Money.
*
*/
public class Money implements IMoney {

private int fAmount;
private String fCurrency;

/**
* Constructs a money from the given amount and currency.
*/
public Money(int amount, String currency) {
fAmount= amount;
fCurrency= currency;
}
/**
* Adds a money to this money. Forwards the request to the addMoney helper.
*/
public IMoney add(IMoney m) {
return m.addMoney(this);
}
public IMoney addMoney(Money m) {
if (m.currency().equals(currency()) )
return new Money(amount()+m.amount(), currency());
return MoneyBag.create(this, m);
}
public IMoney addMoneyBag(MoneyBag s) {
return s.addMoney(this);
}
public int amount() {
return fAmount;
}
public String currency() {
return fCurrency;
}
public boolean equals(Object anObject) {
if (isZero())
if (anObject instanceof IMoney)
return ((IMoney)anObject).isZero();
if (anObject instanceof Money) {
Money aMoney= (Money)anObject;
return aMoney.currency().equals(currency())
&& amount() == aMoney.amount();
}
return false;
}
public int hashCode() {
return fCurrency.hashCode()+fAmount;
}
public boolean isZero() {
return amount() == 0;
}
public IMoney multiply(int factor) {
return new Money(amount()*factor, currency());
}
public IMoney negate() {
return new Money(-amount(), currency());
}
public IMoney subtract(IMoney m) {
return add(m.negate());
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("["+amount()+" "+currency()+"]");
return buffer.toString();
}
public /*this makes no sense*/ void appendTo(MoneyBag m) {
m.appendMoney(this);
}
}


package junit.samples.money;

import java.util.Enumeration;
import java.util.Vector;

/**
* A MoneyBag defers exchange rate conversions. For example adding
* 12 Swiss Francs to 14 US Dollars is represented as a bag
* containing the two Monies 12 CHF and 14 USD. Adding another
* 10 Swiss francs gives a bag with 22 CHF and 14 USD. Due to
* the deferred exchange rate conversion we can later value a
* MoneyBag with different exchange rates.
*
* A MoneyBag is represented as a list of Monies and provides
* different constructors to create a MoneyBag.
*/
public class MoneyBag implements IMoney {
private Vector fMonies= new Vector(5);

public static IMoney create(IMoney m1, IMoney m2) {
MoneyBag result= new MoneyBag();
m1.appendTo(result);
m2.appendTo(result);
return result.simplify();
}
public IMoney add(IMoney m) {
return m.addMoneyBag(this);
}
public IMoney addMoney(Money m) {
return MoneyBag.create(m, this);
}
public IMoney addMoneyBag(MoneyBag s) {
return MoneyBag.create(s, this);
}
void appendBag(MoneyBag aBag) {
for (Enumeration e= aBag.fMonies.elements(); e.hasMoreElements(); )
appendMoney((Money)e.nextElement());
}
void appendMoney(Money aMoney) {
if (aMoney.isZero()) return;
IMoney old= findMoney(aMoney.currency());
if (old == null) {
fMonies.addElement(aMoney);
return;
}
fMonies.removeElement(old);
IMoney sum= old.add(aMoney);
if (sum.isZero())
return;
fMonies.addElement(sum);
}
public boolean equals(Object anObject) {
if (isZero())
if (anObject instanceof IMoney)
return ((IMoney)anObject).isZero();

if (anObject instanceof MoneyBag) {
MoneyBag aMoneyBag= (MoneyBag)anObject;
if (aMoneyBag.fMonies.size() != fMonies.size())
return false;

    for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
        Money m= (Money) e.nextElement();
if (!aMoneyBag.contains(m))
return false;
}
return true;
}
return false;
}
private Money findMoney(String currency) {
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Money m= (Money) e.nextElement();
if (m.currency().equals(currency))
return m;
}
return null;
}
private boolean contains(Money m) {
Money found= findMoney(m.currency());
if (found == null) return false;
return found.amount() == m.amount();
}
public int hashCode() {
int hash= 0;
    for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
        Object m= e.nextElement();
hash^= m.hashCode();
}
    return hash;
}
public boolean isZero() {
return fMonies.size() == 0;
}
public IMoney multiply(int factor) {
MoneyBag result= new MoneyBag();
if (factor != 0) {
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Money m= (Money) e.nextElement();
result.appendMoney((Money)m.multiply(factor));
}
}
return result;
}
public IMoney negate() {
MoneyBag result= new MoneyBag();
    for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
        Money m= (Money) e.nextElement();
        result.appendMoney((Money)m.negate());
}
return result;
}
private IMoney simplify() {
if (fMonies.size() == 1)
return (IMoney)fMonies.elements().nextElement();
return this;
}
public IMoney subtract(IMoney m) {
return add(m.negate());
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("{");
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); )
    buffer.append(e.nextElement());
buffer.append("}");
return buffer.toString();
}
public void appendTo(MoneyBag m) {
m.appendBag(this);
}
}
5建立测试用例,名字叫做testpackage,右击”testpackage“----”新建“---"其他"----Java---Junit----Junit测试用例-----下一步---新建JUnit测试用例,选择setup():测试类的初始化,tearDown():被测试类的销毁,再依次处理。按完成后,一个框架就架设好了,你只要忘里面放入测试代码就可以拉。
测试用例:
package testpackage;

import junit.framework.TestCase;
import junit.samples.money.*;
public class MoneyTest2 extends TestCase {
private Money m1;
private Money m2;
private Money m3;
protected void setUp() throws Exception {
super.setUp();
m1=new  Money(12,"rmb");
m2=new Money(24,"rmb");
m2=new Money(10,"usd");
}

protected void tearDown() throws Exception {
super.tearDown();
}

public void testAddMoney() {
Money temp=new Money(12,"rmb");
assertEquals(m2,m1.addMoney(temp));
}

public void testEqualsObject() {
assertFalse(m3.equals(m1));

}

}


package testpackage;

import junit.framework.TestCase;
import junit.samples.money.*;

public class MoneyTest extends TestCase {
private Money f12CHF;
private Money f14CHF;
private Money f7USD;
private Money f21USD;

private IMoney fMB1;
private IMoney fMB2;

public static void main(String args[]) {
junit.textui.TestRunner.run(MoneyTest.class);
}
protected void setUp() {
f12CHF= new Money(12, "CHF");
f14CHF= new Money(14, "CHF");
f7USD= new Money( 7, "USD");
f21USD= new Money(21, "USD");

fMB1= MoneyBag.create(f12CHF, f7USD);
fMB2= MoneyBag.create(f14CHF, f21USD);
}
public void testBagMultiply() {
// {[12 CHF][7 USD]} *2 == {[24 CHF][14 USD]}
IMoney expected= MoneyBag.create(new Money(24, "CHF"), new Money(14, "USD"));
assertEquals(expected, fMB1.multiply(2));
assertEquals(fMB1, fMB1.multiply(1));
assertTrue(fMB1.multiply(0).isZero());
}
public void testBagNegate() {
// {[12 CHF][7 USD]} negate == {[-12 CHF][-7 USD]}
IMoney expected= MoneyBag.create(new Money(-12, "CHF"), new Money(-7, "USD"));
assertEquals(expected, fMB1.negate());
}
public void testBagSimpleAdd() {
// {[12 CHF][7 USD]} + [14 CHF] == {[26 CHF][7 USD]}
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));
assertEquals(expected, fMB1.add(f14CHF));
}
public void testBagSubtract() {
// {[12 CHF][7 USD]} - {[14 CHF][21 USD] == {[-2 CHF][-14 USD]}
IMoney expected= MoneyBag.create(new Money(-2, "CHF"), new Money(-14, "USD"));
assertEquals(expected, fMB1.subtract(fMB2));
}
public void testBagSumAdd() {
// {[12 CHF][7 USD]} + {[14 CHF][21 USD]} == {[26 CHF][28 USD]}
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(28, "USD"));
assertEquals(expected, fMB1.add(fMB2));
}
public void testIsZero() {
assertTrue(fMB1.subtract(fMB1).isZero());
assertTrue(MoneyBag.create(new Money (0, "CHF"), new Money (0, "USD")).isZero());
}
public void testMixedSimpleAdd() {
// [12 CHF] + [7 USD] == {[12 CHF][7 USD]}
IMoney expected= MoneyBag.create(f12CHF, f7USD);
assertEquals(expected, f12CHF.add(f7USD));
}
public void testBagNotEquals() {
IMoney bag= MoneyBag.create(f12CHF, f7USD);
assertFalse(bag.equals(new Money(12, "DEM").add(f7USD)));
}
public void testMoneyBagEquals() {
assertTrue(!fMB1.equals(null));

assertEquals(fMB1, fMB1);
IMoney equal= MoneyBag.create(new Money(12, "CHF"), new Money(7, "USD"));
assertTrue(fMB1.equals(equal));
assertTrue(!fMB1.equals(f12CHF));
assertTrue(!f12CHF.equals(fMB1));
assertTrue(!fMB1.equals(fMB2));
}
public void testMoneyBagHash() {
IMoney equal= MoneyBag.create(new Money(12, "CHF"), new Money(7, "USD"));
assertEquals(fMB1.hashCode(), equal.hashCode());
}
public void testMoneyEquals() {
assertTrue(!f12CHF.equals(null));
Money equalMoney= new Money(12, "CHF");
assertEquals(f12CHF, f12CHF);
assertEquals(f12CHF, equalMoney);
assertEquals(f12CHF.hashCode(), equalMoney.hashCode());
assertTrue(!f12CHF.equals(f14CHF));
}
public void testMoneyHash() {
assertTrue(!f12CHF.equals(null));
Money equal= new Money(12, "CHF");
assertEquals(f12CHF.hashCode(), equal.hashCode());
}
public void testSimplify() {
IMoney money= MoneyBag.create(new Money(26, "CHF"), new Money(28, "CHF"));
assertEquals(new Money(54, "CHF"), money);
}
public void testNormalize2() {
// {[12 CHF][7 USD]} - [12 CHF] == [7 USD]
Money expected= new Money(7, "USD");
assertEquals(expected, fMB1.subtract(f12CHF));
}
public void testNormalize3() {
// {[12 CHF][7 USD]} - {[12 CHF][3 USD]} == [4 USD]
IMoney ms1= MoneyBag.create(new Money(12, "CHF"), new Money(3, "USD"));
Money expected= new Money(4, "USD");
assertEquals(expected, fMB1.subtract(ms1));
}
public void testNormalize4() {
// [12 CHF] - {[12 CHF][3 USD]} == [-3 USD]
IMoney ms1= MoneyBag.create(new Money(12, "CHF"), new Money(3, "USD"));
Money expected= new Money(-3, "USD");
assertEquals(expected, f12CHF.subtract(ms1));
}
public void testPrint() {
assertEquals("[12 CHF]", f12CHF.toString());
}
public void testSimpleAdd() {
// [12 CHF] + [14 CHF] == [26 CHF]
Money expected= new Money(26, "CHF");
assertEquals(expected, f12CHF.add(f14CHF));
}
public void testSimpleBagAdd() {
// [14 CHF] + {[12 CHF][7 USD]} == {[26 CHF][7 USD]}
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));
assertEquals(expected, f14CHF.add(fMB1));
}
public void testSimpleMultiply() {
// [14 CHF] *2 == [28 CHF]
Money expected= new Money(28, "CHF");
assertEquals(expected, f14CHF.multiply(2));
}
public void testSimpleNegate() {
// [14 CHF] negate == [-14 CHF]
Money expected= new Money(-14, "CHF");
assertEquals(expected, f14CHF.negate());
}
public void testSimpleSubtract() {
// [14 CHF] - [12 CHF] == [2 CHF]
Money expected= new Money(2, "CHF");
assertEquals(expected, f14CHF.subtract(f12CHF));
}
}

6.点击运行--运行Junit Test,若无错,则左边的条子为绿,有错则为红,且有错误的提示,有错的测试类才是好的测试类。
来一起运行。
7.创建测试套件:测试套件可运行多个测试案例或测试套件,测试套件可将多个测试案例组合起


测试套件:
package testpackage;

import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTests {

public static Test suite() {
TestSuite suite = new TestSuite("Test for testpackage");
//$JUnit-BEGIN$
suite.addTestSuite(MoneyTest.class);
suite.addTestSuite(MoneyTest2.class);
//$JUnit-END$
return suite;
}

}
7.创建测试套件:测试套件可运行多个测试案例或测试套件,测试套件可将多个测试案例组合起
分享到:
评论

相关推荐

    Juit In Action 中文版 part1

    Juit In Action 中文版 part1

    Juit In Action 中文版 part2

    Juit In Action 中文版 part2

    Juit In Action 中文版 part3

    Juit In Action 中文版 part3 包太大了,分3部分

    Juit4 jar.rar

    在实际使用中,开发者通常会结合构建工具(如 Maven 或 Gradle)和 IDE(如 Eclipse 或 IntelliJ IDEA)来管理和运行 JUnit 测试。这些工具提供了图形界面,使得编写、运行和查看测试结果变得简单高效。 了解和熟练...

    ACM-Blogs:ACM JUIT成员撰写的技术博客集

    ACM博客ACM JUIT成员撰写的技术博客集。

    junit单元测试相关jar包.zip

    在Junit中,我们通常使用`assertEquals`或`assertNull`等简单的断言,但Hamcrest则允许更复杂的逻辑表达,如`assertThat(expression, matcher)`,这里的`expression`是被测试的对象,`matcher`则是用来验证对象是否...

    junit框架文件包

    JUnit框架是Java编程语言中最广泛使用的单元测试框架,它为开发者提供了一种便捷的方式来编写可重复执行的测试用例,确保代码的质量和稳定性。在JavaScript的开发环境中,虽然JUnit主要用于Java,但通过一些工具和库...

    junit4 jar完整包

    JUnit4是Java编程语言中最广泛使用的单元测试框架之一,它为开发者提供了一种方便、高效的方式来验证代码的正确性。这个“junit4 jar完整包”包含了所有你需要进行单元测试的类和接口,使得测试过程变得简单且易于...

    Junit测试jar包

    1. **创建测试类**:首先,你需要创建一个测试类,这个类通常以`Test`结尾,并且继承自`junit.framework.TestCase`(对于JUnit4)或使用`@org.junit.Test`注解(对于JUnit5)标记测试方法。 2. **测试方法**:测试...

    junit4 jar包 Java单元测试框架绿色免费版.zip

    JUnit4是Java编程语言中最广泛使用的单元测试框架之一,它为开发者提供了一种方便、高效的方式来验证代码的正确性。这个“junit4 jar包 Java单元测试框架绿色免费版.zip”文件包含的是JUnit4框架的可执行jar包,用于...

    java查看class代码工具jd-gui

    java查看class代码工具jd-gui

    牛客AI模拟面试1测开岗

    需要的知识 软件测试基础理论知识 黑盒,白盒 编程语言基础 C/C++ java python等 ...2)偏白盒的robotium,这家伙号称是黑盒,但是本人不太认同~ 因为使用robotium需要知道package和acitivity这样的细节,所以需

    OpenCV-Bootcamp:该存储库包含在2021年计算机视觉训练营期间编写和讨论的所有代码

    该存储库包含在AIT学生分会(JUIT)进行的2021年计算机视觉新手训练营期间编写和讨论的所有代码 依存关系 :hammer_and_wrench: Python 3.6或更高版本- OpenCV 3.5或更高版本pip install opencv-python 目录 :...

Global site tag (gtag.js) - Google Analytics