锘??xml version="1.0" encoding="utf-8" standalone="yes"?>欧美日韩1区2区,成人av二区,精品视频在线播放http://www.aygfsteel.com/aldreamlau/category/27257.htmlmezh-cnTue, 13 Nov 2007 09:11:29 GMTTue, 13 Nov 2007 09:11:29 GMT60eclipse Junit嫻嬭瘯鍚戝http://www.aygfsteel.com/aldreamlau/articles/159974.htmlaldreamaldreamMon, 12 Nov 2007 07:10:00 GMThttp://www.aygfsteel.com/aldreamlau/articles/159974.htmlhttp://www.aygfsteel.com/aldreamlau/comments/159974.htmlhttp://www.aygfsteel.com/aldreamlau/articles/159974.html#Feedback0http://www.aygfsteel.com/aldreamlau/comments/commentRss/159974.htmlhttp://www.aygfsteel.com/aldreamlau/services/trackbacks/159974.html 鍘熸枃鍑哄錛?a >http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf

 

JUnit is a simple Java testing framework to write tests for you Java application. This tutorial gives you an overview of the features of JUnit and shows a little example how you can write tests for your Java application.

General

Author:

Sascha Wolski

Sebastian Hennebrueder

http://www.laliluna.de/tutorials.html ? Tutorials for Struts, EJB, xdoclet and eclipse.


Date:

April, 12 2005


Software:

Eclipse 3.x

Junit 2.x


Source code:

http://www.laliluna.de/assets/tutorials/junit-testing-source.zip

PDF Version

http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf

What is JUnit

JUnit is a simple open source Java testing framework used to write and run repeatable automated tests. It is an instance of the xUnit architecture for unit testing framework. Eclipse supports creating test cases and running test suites, so it is easy to use for your Java applications.

JUnit features include:

  • Assertions for testing expected results

  • Test fixtures for sharing common test data

  • Test suites for easily organizing and running tests

  • Graphical and textual test runners

What is a test case

A test case is a class which holds a number of test methods. For example if you want to test some methods of a class Book you create a class BookTest which extends the JUnit TestCase class and place your test methods in there.

How you write and run a simple test

  1. Create a subclass of TestCase:


public class BookTest extends TestCase{ 
//..
}


  1. Write a test method to assert expected results on the object under test:

Note: The naming convention for a test method is testXXX()

public void testCollection() {
Collection collection = new ArrayList();
assertTrue(collection.isEmpty());
}


  1. Write a suite() method that uses reflection to dynamically create a test suite containing all the testXXX() methods:


public static Test suite(){
return new TestSuite(BookTest.class);
}



  1. Activate the JUnit view in Eclipse (Window > Show View > Other.. > Java > JUnit).




You find the JUnit tab near the Package Explorer tab. You can change the position of the tab by drag and drop it.




  1. Right click on the subclass of TestCase and choose Run > JUnit Test to run the test.

Using a test fixture

A test fixture is useful if you have two or more tests for a common set of objects. Using a test fixture avoids duplicating the test code necessary to initialize and cleanup those common objects for each test.

To create a test fixture, define a setUp() method that initializes common object and a tearDown() method to cleanup those objects. The JUnit framework automatically invokes the setUp() method before a each test is run and the tearDown() method after each test is run.

The following test uses a test fixture:

public class BookTest2 extends TestCase {

private Collection collection;

protected void setUp() {
collection = new ArrayList();
}

protected void tearDown() {
collection.clear();
}

public void testEmptyCollection(){
assertTrue(collection.isEmpty());
}
}

Dynamic and static way of running single tests

JUnit supports two ways (static and dynamic) of running single tests.

In static way you override the runTest() method inherited form TestCase class and call the desired test case. A convenient way to do this is with an anonymous inner class.

Note: Each test must be given a name, so you can identify it if it fails.

TestCase test = new BookTest("equals test") {
public void runTest() {
testEquals();
}
};



The dynamic way to create a test case to be run uses reflection to implement runTest. It assumes the name of the test is the name of the test case method to invoke. It dynamically finds and invokes the test method. The dynamic way is more compact to write but it is less static type safe. An error in the name of the test case goes unnoticed until you run it and get a NoSuchMethodException. We leave the choice of which to use up to you.

TestCast test = new BookTest("testEquals");

What is a TestSuite

If you have two tests and you'll run them together you could run the tests one at a time yourself, but you would quickly grow tired of that. Instead, JUnit provides an object TestSuite which runs any number of test cases together. The suite method is like a main method that is specialized to run tests.

Create a suite and add each test case you want to execute:

public static void suite(){
TestSuite suite = new TestSuite();
suite.addTest(new BookTest("testEquals"));
suite.addTest(new BookTest("testBookAdd"));
return suite;
}



Since JUnit 2.0 there is an even simpler way to create a test suite, which holds all testXXX() methods. You only pass the class with the tests to a TestSuite and it extracts the test methods automatically.

Note: If you use this way to create a TestSuite all test methods will be added. If you do not want all test methods in the TestSuite use the normal way to create it.

Example:

public static void suite(){
return new TestSuite(BookTest.class);
}

A little example

Create a new Java project named JUnitExample.

Add a package de.laliluna.tutorial.junitexample where you place the example classes and a package test.laliluna.tutorial.junitexample where you place your test classes.

The class Book

Create a new class Book in the package de.laliluna.tutorial.junitexample.

Add two properties title of type String and price of type double.

Add a constructor to set the two properties.

Provide a getter- and setter-method for each of them.

Add a method trunk for a method equals(Object object) which checks if the object is an instance of the class Book and the values of the object are equal. The method return a boolean value.

Note: Do not write the logic of the equals(..) method, we do it after finish creating the test method.

The following source code shows the class Book.

public class Book {

private String title;
private double price;

/**
* Constructor
*
* @param title
* @param price
*/
public Book(String title,
double price) {
this.title = title;
this.price = price;
}

/**
* Check if an object is an instance of book
* and the values of title and price are equal
* then return true, otherwise return false
*/
public boolean equals(Object object) {

return false;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}

The test case BookTest

Create a new test case BookTest in the package test.laliluna.tutorial.junitexample Right click on the package and choose New > JUnit Test Case.

In the wizard choose the methods stubs setUp(), tearDown() and constructor().






The following source code shows the class BookTest

public class BookTest extends TestCase {

/**
* setUp() method that initializes common objects
*/
protected void setUp() throws Exception {
super.setUp();
}

/**
* tearDown() method that cleanup the common objects
*/
protected void tearDown() throws Exception {
super.tearDown();
}

/**
* Constructor for BookTest.
* @param name
*/
public BookTest(String name) {
super(name);
}

}



Now we want to write a test for the equals(..) method of the class Book. We provide three private properties, book1, book2 and book3 of type Book.

private Book book1;
private Book book2;
private Book book3;



Within the setUp() method we initializes the three properties with some values. Property book1 and book3 are the same.

protected void setUp() throws Exception {
super.setUp();
book1 = new Book("ES", 12.99);
book2 = new Book("The Gate", 11.99);
book3 = new Book("ES", 12.99);
}



Within the tearDown() method we cleanup the properties:

protected void tearDown() throws Exception {
super.tearDown();
book1 = null;
book2 = null;
book3 = null;
}



Now, add a test method testEquals() to the test case. Within the method we use the assertFalse() method of the JUnit framework to test if the return-value of the equals(..) method is false, because book1 and book2 are not the same. If the return-value is false the logic of the equals() method is correct, otherwise there is a logical problem while comparing the objects. We want to test if the method compares the objects correctly by using the assertTrue() method. Book1 and Book3 are the same, because both are an instance of the class Book and have the same values.

The following source code shows the testEquals() method:

public void testEquals(){
assertFalse(book2.equals(book1));
assertTrue(book1.equals(book1));
}



Writing the logic of the equals() method

We have finished the test and now we can add the logic to the equals() method stub. Open the class Book and add the logic to the equals() method. First we check if the object given by the method is an instance of Book. Then compare the properties title and price, if they are equal return true.

public boolean equals(Object object) {
if (object instanceof Book) {
Book book = (Book) object;
return getTitle().equals(book.getTitle())
&& getPrice() == book.getPrice();
}
return false;
}


Create the suite() method

In order to run the test method testEquals() add a method suite() to the class BookTest.

Note: You can also create a separate class where you add the suite() method.

Within the method create a new instance of TestSuite and use the method addTest(..) to add a test. Here we use the dynamically way to add a test to a TestSuite.

The method looks like the follows:

public static Test suite(){
TestSuite suite = new TestSuite();
suite.addTest(new BookTest("testEquals"));
return suite;
}



Run the test

After finishing all test methods we want to run the JUnit test case. Right mouse button on the class BookTest and choose Run As > JUnit Test.

On the JUnit view (Menu Windows -> show view) of Eclipse you can see how many runs, errors and failures occurred.




aldream 2007-11-12 15:10 鍙戣〃璇勮
]]>
涓涓狫unit渚嬪瓙(杞嚜caterpillar.onlyfun.net)http://www.aygfsteel.com/aldreamlau/articles/159653.htmlaldreamaldreamSat, 10 Nov 2007 16:55:00 GMThttp://www.aygfsteel.com/aldreamlau/articles/159653.htmlhttp://www.aygfsteel.com/aldreamlau/comments/159653.htmlhttp://www.aygfsteel.com/aldreamlau/articles/159653.html#Feedback0http://www.aygfsteel.com/aldreamlau/comments/commentRss/159653.htmlhttp://www.aygfsteel.com/aldreamlau/services/trackbacks/159653.html
鍦ㄦ脯瑭︿竴鍊嬪柈鍏冩柟娉曟檪錛屾湁鏅傛偍鏈冮渶瑕佺鄲瀹冧竴浜涚墿浠朵綔鐐洪亱琛屾檪鐨勮硣鏂欙紝渚嬪鎮ㄦ挵瀵笅闈㈤欏嬫脯瑭︽渚嬶細

MaxMinTest.java
 1 package onlyfun.caterpillar.test;
 2 
 3 import onlyfun.caterpillar.MaxMinTool;
 4 import junit.framework.TestCase;
 5  
 6 public class MaxMinTest extends TestCase {
 7     public void testMax() {
 8         int[] arr = {-5-4-3-2-1012345};
 9         assertEquals(5, MaxMinTool.getMax(arr));
10     }
11 
12     public void testMin() {
13         int[] arr = {-5-4-3-2-1012345};
14         assertEquals(-5, MaxMinTool.getMin(arr));
15     }
16     
17     public static void main(String[] args) {
18         junit.swingui.TestRunner.run(MaxMinTest.class);
19     }
20 }

鎮ㄥ皣璦▓鐨凪axMinTool鍖呮嫭闈滄厠鏂規硶getMax()鑸噂etMin()錛岀暥鎮ㄧ鄲瀹冧竴鍊嬫暣鏁擱櫍鍒楋紝瀹冨戝皣鍊嬪垾鍌沖洖闄e垪涓殑鏈澶у艱垏鏈灝忓鹼紝欏劧 鐨勶紝鎮ㄦ墍婧栧倷鐨勯櫍鍒楅噸瑜囧嚭鐝懼湪鍏╁嬪柈鍏冩脯瑭︿箣涓紝閲嶈鐨勭▼寮忕⒓鍦ㄨō璦堜腑鍙互娓涘皯灝卞剺閲忔笡灝戯紝鍦ㄩ欏叐鍊嬪柈鍏冩脯瑭︿腑錛屾暣鏁擱櫍鍒楃殑婧栧倷鏄柈鍏冩柟娉曟墍闇瑕佺殑璩囨簮錛? 鎴戝戠ū涔嬬偤fixture錛屼篃灝辨槸涓鍊嬫脯瑭︽檪鎵闇瑕佺殑璩囨簮闆嗗悎銆?br />
fixture蹇呴爤鑸囦笂涓嬫枃錛圕ontext錛夌劇闂滐紝涔熷氨鏄垏紼嬪紡鍩瘋鍓嶅緦鐒¢棞錛岄欐ǎ鎵嶇鍚堝柈鍏冩脯瑭︾殑鎰忔兜錛岀偤姝わ紝閫氬父灝囨墍闇鐨刦ixture鎾板鍦ㄥ柈鍏冩柟娉曚箣涓紝濡傛鍦ㄥ柈鍏冩脯瑭﹂枊濮嬫檪鍓靛緩fixture錛屼甫鏂肩祼鏉熷緦閵鋒瘈fixture銆?br />
鐒惰屽皪鏂奸噸瑜囧嚭鐝懼湪鍚勫嬪柈鍏冩脯瑭︿腑鐨刦ixture錛屾偍鍙互闆嗕腑鍔犱互綆$悊錛屾偍鍙互鍦ㄧ辜鎵縏estCase涔嬪緦錛岄噸鏂板畾緹?span style="font-weight: bold;">setUp()鑸?span style="font-weight: bold;">tearDown()鏂規硶錛屽皣鏁稿嬪柈鍏冩脯瑭︽墍闇瑕佺殑fixture鍦╯etUp()涓壍寤猴紝涓﹀湪tearDown()涓姺姣錛屼緥濡傦細

MaxMinTest.java
 1 package onlyfun.caterpillar.test;
 2 
 3 import onlyfun.caterpillar.MaxMinTool;
 4 import junit.framework.TestCase;
 5 
 6 public class MaxMinTest extends TestCase {
 7     private int[] arr;
 8 
 9     protected void setUp() throws Exception {
10         super.setUp();
11         arr = new int[]{-5-4-3-2-1012345};
12     }
13 
14     protected void tearDown() throws Exception {
15         super.tearDown();
16         arr = null;
17     }
18     
19     public void testMax() {
20         assertEquals(5, MaxMinTool.getMax(arr));
21     }
22 
23     public void testMin() {
24         assertEquals(-5, MaxMinTool.getMin(arr));
25     }
26     
27     public static void main(String[] args) {
28         junit.swingui.TestRunner.run(MaxMinTest.class);
29     }
30 }

setUp()鏂規硶鏈冨湪姣忎竴鍊嬪柈鍏冩脯瑭estXXX()鏂規硶闁嬪鍓嶈鍛煎彨錛屽洜鑰屾暣鏁擱櫍鍒楁渻琚緩绔嬶紝鑰宼earDown()鏈冨湪姣忎竴鍊嬪柈鍏冩脯瑭? testXXX()鏂規硶緄愭潫寰岃鍛煎彨錛屽洜鑰屾暣鏁擱櫍鍒楀弮鑰冨悕紼卞皣鏈冨弮鑰冭嚦null錛屽姝や竴渚嗭紝鎮ㄥ彲浠ュ皣fixture鐨勭鐞嗛泦涓湪 setUp()鑸噒earDown()鏂規硶涔嬪緦銆?br />
鏈寰屾寜鐓ф脯瑭︽渚嬬殑鍏у錛屾偍瀹屾垚MaxMinTool欏炲垾錛?

MaxMinTool.java
 1 package onlyfun.caterpillar;
 2 
 3 public class MaxMinTool {
 4     public static int getMax(int[] arr) {
 5         int max = Integer.MIN_VALUE;
 6         
 7         for(int i = 0; i < arr.length; i++) {
 8             if(arr[i] > max)
 9                 max = arr[i];
10         }
11         
12         return max;
13     }
14     
15     public static int getMin(int[] arr) {
16         int min = Integer.MAX_VALUE;
17         
18         for(int i = 0; i < arr.length; i++) {
19             if(arr[i] < min)
20                 min = arr[i];
21         }
22         
23         return min;
24     }
25 }

Swing浠嬮潰鐨凾estRunner鍦ㄦ脯瑭﹀け鏁楁檪鏈冮’紺虹磪鑹茬殑媯掑瓙錛岃屽湪娓│鎴愬姛寰屾渻欏ず綞犺壊鐨勬瀛愶紝鑰?"Keep the bar green to keep the code clean." 姝f槸JUnit鐨勫悕璦錛屼篃鏄脯瑭︾殑鏈緄傜洰鐨勩?br />

aldream 2007-11-11 00:55 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 江安县| 沧源| 新疆| 嘉鱼县| 香格里拉县| 右玉县| 曲周县| 陆丰市| 东光县| 荆门市| 贺兰县| 山西省| 民权县| 东乡| 阿荣旗| 清徐县| 鄂托克前旗| 南漳县| 烟台市| 珠海市| 湘潭县| 临武县| 灵宝市| 南召县| 通榆县| 阿克陶县| 滕州市| 海门市| 秦安县| 淄博市| 怀柔区| 浠水县| 陵川县| 五华县| 藁城市| 淮安市| 上林县| 双桥区| 深水埗区| 休宁县| 南昌县|