天天看點

測試體系 JUnit4 感悟

關于 test 測試方面 自己的體會

http://weity.blogspot.com/

我的海外blog,有興趣的可以去看看,别忘記點一下 google的廣告哦。

  在軟體開發流程裡面,test(測試)屬于比較靠後的 步驟,在經過 概要設計,基本設計,詳細設計,制造等流程之後,

才會進入的一個 非常重要的軟體開發步驟

  基本上,測試的人,應該參與了前面幾個步驟,應該說 是非常了解 業務邏輯,制造方法,

以及明确的知道自己想要什麼結果的人。

  如果做不到上面說的幾項,基本上測試的人是個沒有靈魂的。也是基本上屬于 機械的,不知是以然的 危險的測試。

  基本上 測試分為幾個階段

  1:制造結束初始--單體測試

  2:項目某一個子產品制造結束初始--統合測試

  3:項目結束初始--系統測試

(本文以JUnit單體測試為主要闡述對象)

 單體測試

  在制造結束初始階段,每個單獨的程式都需要來驗證 業務,功能的正确性。

  這個時候,基本面 還是以 程式功能是否正确 作為主要對象。

幾種測試工具選擇

  1:基本上的功能都可以通過JUnit來完成功能測試。點選這裡下載下傳

  2:輔助的 如果操作資料庫方面比較多的程式,可以使用DBUnit。點選這裡下載下傳 

  3:文法上的測試工具,可以選擇 JTest (收費)。點選這裡下載下傳

  這裡基本上介紹JUnit 4.X和DBUnit 2.0,JTest由于收費,還有設計驗證模闆都很繁瑣,不在這裡介紹。

  哦,差一點忘記了,還有一個JUnit 擴充包hamcrest,點選這裡下載下傳

  參考文章:探索 JUnit 4.4 新特性 來自 IBM developerWork china

下面進入正題:

  首先,來看一段代碼

  equalTo方法

定義: public class Matchers {   public static <T> org.hamcrest.Matcher<T> equalTo(T operand) {     return org.hamcrest.core.IsEqual.equalTo(operand);   } } 應用: @Test public void testAssertThatWithEqualTo() {   Assert.assertThat("test", Matchers.equalTo("test"));   Assert.assertThat("test", Matchers.equalToIgnoringCase("TEST"));   Assert.assertThat("test", Matchers.equalToIgnoringWhiteSpace("    test    "));   Assert.assertThat(10, Matchers.equalTo(new Integer(10))); }

  使用過JUnit 的大家,都熟悉這個方法,現在JUnit 4.5 +hamcrest 提供了更友善的 靜态方法。

下面來看一下如何對 String來進行比較

@Test

public void testAssertThatConcerningString() {

  Assert.assertThat("test", Matchers.containsString("es"));

  Assert.assertThat("test", Matchers.startsWith("tes"));

  Assert.assertThat("test", Matchers.endsWith("st"));

}

下面是數值的驗證

@Test

public void testAssertThatConcerningNumber() {

  // clothTo()驗證有沒有誤差

  Assert.assertThat(1.01d, Matchers.closeTo(1.001d, 0.01d));

  Assert.assertThat(0.991d, Matchers.closeTo(1.001d, 0.01d));

  // 因為1.01 不在 1.001 +- 0.005 的範圍,是以導緻失敗

  // Assert.assertThat(1.01d, Matchers.closeTo(1.001d, 0.005d));

  Assert.assertThat(11, Matchers.greaterThan(10));

  Assert.assertThat(11, Matchers.greaterThanOrEqualTo(11));

  // 也有lessThan(), lessThanOrEqualTo()

}

下面是比較困難的數組系列

@Test

public void testAssertThatConcerningCollection() {

  List<Integer> list = new ArrayList<Integer>();

  list.add(1);

  list.add(2);

  list.add(3);

  Assert.assertThat(list, Matchers.hasItem(2));

  // hasItems()方法的參數是可變長度

  Assert.assertThat(list, Matchers.hasItems(2));

  Assert.assertThat(list, Matchers.hasItems(1, 2, 3));

  Integer[] integers = {new Integer(1), new Integer(2)};

  Assert.assertThat(integers, Matchers.hasItemInArray(1));

  Map<Integer, String> map = new HashMap<Integer,String>();

  map.put(1, "one");

  map.put(2, "two");

  map.put(3, "three");

  Assert.assertThat(map, Matchers.hasEntry(1, "one"));

  // Map的key和value,都可以用Matcher來比較

  Assert.assertThat(map, Matchers.hasEntry(Matchers.equalTo(2), Matchers.equalTo("two")));

}

下面是Bean的驗證

@Test

public void testAssertThatConcerningBeans() {

  Item item = new Item();

  item.setName("test");

  Assert.assertThat(item, Matchers.hasProperty("name"));

  Assert.assertThat(item, Matchers.hasProperty("name", Matchers.equalTo("test")));

}

下面是Object的驗證

@Test

public void testAssertThatConcerningObject() {

  Item item = new Item();

  item.setName("test");

  // 驗證第一個參數對象的toString()

  Assert.assertThat(item, Matchers.hasToString(Matchers.equalTo(item.toString())));

  // 驗證Class#isAssignableFrom()

  Assert.assertThat(item.getClass(), Matchers.typeCompatibleWith(Serializable.class));

  Assert.assertThat(item.getClass(), Matchers.instanceOf(Serializable.class));

  Item nullItem = null;

  Assert.assertThat(nullItem, Matchers.nullValue());

  Assert.assertThat(item, Matchers.notNullValue());

  Assert.assertThat(item, Matchers.sameInstance(item));

}

下面是邏輯的驗證

@Test

public void testAssertThatConcerningLogic() {

  // Matchers.anything()是一直為True

  Assert.assertThat("test", Matchers.anything());

  // 全部都是True才可以

  Assert.assertThat("test", Matchers.allOf(Matchers.equalTo("test"), Matchers.containsString("es")));

  // 有一個為True就可以

  Assert.assertThat("test", Matchers.anyOf(Matchers.equalTo("xxxx"), Matchers.containsString("es")));

}

下面是完整的代碼:

theOthersOfMatchers.java

package com.weity.JUnit4.samples;

import static org.hamcrest.CoreMatchers.allOf;

import static org.hamcrest.CoreMatchers.anyOf;

import static org.hamcrest.CoreMatchers.anything;

import static org.hamcrest.CoreMatchers.equalTo;

import static org.hamcrest.CoreMatchers.is;

import static org.hamcrest.CoreMatchers.not;

import static org.junit.Assert.assertThat;

import static org.junit.matchers.JUnitMatchers.containsString;

import java.io.Serializable;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.hamcrest.Matchers;

import org.junit.Test;

public class theOthersOfMatchers {

@SuppressWarnings("unchecked")

@Test

public void chkString(){

String testedString = "developerWorks";

String testedValue = "abc";

String expectedValue = "abc";

// is 

assertThat( testedString, is( "developerWorks" ) );

// not 

assertThat( testedString, not( "BdeveloperWorks" ) );

// containsString 

assertThat( testedString, containsString( "developerWorks" ) );

// endsWith 

assertThat( testedString, Matchers.endsWith( "developerWorks" ) ); 

// startsWith 

assertThat( testedString, Matchers.startsWith( "developerWorks" ) ); 

// equalTo 

assertThat( testedValue, equalTo( expectedValue ) ); 

// equalToIgnoringCase

assertThat( testedString, Matchers.equalToIgnoringCase( "developerWorks" ) ); 

// equalToIgnoringWhiteSpace

assertThat( testedString, Matchers.equalToIgnoringWhiteSpace( "developerWorks" ) );

assertThat("test", Matchers.equalTo("test"));

assertThat("test", Matchers.equalToIgnoringCase("TEST"));

assertThat("test", Matchers.equalToIgnoringWhiteSpace("    test    "));

}

@SuppressWarnings("unchecked")

@Test

public void chkInt(){

int testedNumber = 1;

// allOf == &&

assertThat( testedNumber, allOf( Matchers.greaterThan(0), Matchers.lessThan(16) ) );

// anyOf == ||

assertThat( testedNumber, anyOf( Matchers.greaterThan(16), Matchers.lessThan(8) ) );

// anything ==true

assertThat( testedNumber, anything() );

// is 

assertThat( testedNumber, is( 1 ) );

// not 

assertThat( testedNumber, not( 2 ) );

// equalTo

assertThat( testedNumber, Matchers.equalTo(new Integer(1)));

}

@SuppressWarnings("unchecked")

@Test

public void chkDouble(){

double testedDouble = 1.0;

// closeTo 

assertThat( testedDouble, Matchers.closeTo( 20.0, 0.5 ) );

// greaterThan 

assertThat( testedDouble, Matchers.greaterThan(16.0) );

// lessThan 

assertThat( testedDouble, Matchers.lessThan (16.0) );

// greaterThanOrEqualTo 

assertThat( testedDouble, Matchers.greaterThanOrEqualTo (16.0) );

// lessThanOrEqualTo 

assertThat( testedDouble, Matchers.lessThanOrEqualTo (16.0) );

// closeTo()

assertThat(1.01d, Matchers.closeTo(1.001d, 0.01d));

assertThat(0.991d, Matchers.closeTo(1.001d, 0.01d));

// 

assertThat(1.01d, Matchers.closeTo(1.001d, 0.005d));

assertThat(11, Matchers.greaterThan(10));

assertThat(11, Matchers.greaterThanOrEqualTo(11));

// lessThan(), lessThanOrEqualTo()

}

@SuppressWarnings("unchecked")

@Test

public void chkArray(){

//collection

List<Integer> list = new ArrayList<Integer>();

list.add(1);

list.add(2);

list.add(3);

assertThat(list, Matchers.hasItem(2));

// hasItems()

assertThat(list, Matchers.hasItems(2));

assertThat(list, Matchers.hasItems(1, 2, 3));

Integer[] integers = {new Integer(1), new Integer(2)};

assertThat(integers, Matchers.hasItemInArray(1));

Map<Integer, String> map = new HashMap<Integer,String>();

map.put(1, "one");

map.put(2, "two");

map.put(3, "three");

assertThat(map, Matchers.hasEntry(1, "one"));

// 

assertThat(map, Matchers.hasEntry(Matchers.equalTo(2), Matchers.equalTo("two")));

List<String> lis = new ArrayList<String>();

lis.add("key");

lis.add("value");

lis.add("elemment");

//collection mapObject = new collection();

//Map mapObject = new HashMap(1);

//Map iterableObject = new HashMap(1);

Map<String, String> mapObject = new HashMap<String,String>();

mapObject.put("key", "value");

mapObject.put("ke2", "two");

mapObject.put("ke3", "three");

//String iterableObject = "";

// hasEntry 

assertThat( mapObject, Matchers.hasEntry( "key", "value" ) );

// hasItem 

//assertThat( iterableObject, Matchers.hasItem ( "element" ) );

// hasKey 

//assertThat( mapObject.get(0), Matchers.hasKey ( "key" ) );

// hasValue 

//assertThat( mapObject, Matchers.hasValue ( "key" ) );

}

@Test

public void testAssertThatConcerningBeans() {

Item item = new Item();

item.setStrName("test");

assertThat(item, Matchers.hasProperty("strName"));

assertThat(item, Matchers.hasProperty("strName", Matchers.equalTo("test")));

}

@Test

public void testAssertThatConcerningObject() {

Item item = new Item();

item.setStrName("test");

// 

assertThat(item, Matchers.hasToString(Matchers.equalTo(item.toString())));

// 

assertThat(item.getClass(), Matchers.typeCompatibleWith(Serializable.class));

assertThat(item.getClass(), Matchers.instanceOf(Serializable.class));

Item nullItem = null;

assertThat(nullItem, Matchers.nullValue());

assertThat(item, Matchers.notNullValue());

assertThat(item, Matchers.sameInstance(item));

}

@Test

public void testAssertThatConcerningLogic() {

// Matchers.anything()

assertThat("test", Matchers.anything());

// 

assertThat("test", Matchers.allOf(Matchers.equalTo("test"), Matchers.containsString("es")));

// 

assertThat("test", Matchers.anyOf(Matchers.equalTo("xxxx"), Matchers.containsString("es")));

}

}

接下來是: Item.java

package com.weity.JUnit4.samples;

import java.io.Serializable;

public class Item implements Serializable {

public String strName;

public Item(){

}

public String getStrName() {

return strName;

}

public void setStrName(String strName) {

this.strName = strName;

}

@Override

protected Object clone() throws CloneNotSupportedException {

// TODO Auto-generated method stub

return super.clone();

}

@Override

public boolean equals(Object arg0) {

// TODO Auto-generated method stub

return super.equals(arg0);

}

@Override

protected void finalize() throws Throwable {

// TODO Auto-generated method stub

super.finalize();

}

@Override

public int hashCode() {

// TODO Auto-generated method stub

return super.hashCode();

}

@Override

public String toString() {

// TODO Auto-generated method stub

return super.toString();

}

}