2011-03-26 93 views
1

我是新来的单元测试和阅读它的时候,我弄不清如何做到这一点。单元测试简单的文字功能

我有这个代码进入洗牌一句话:

public static void shuffle(String word) { 

    // Store string into array 
     ArrayList<Character> wordShuffled = new ArrayList<Character>(); 
     // loop the index of the string 
     for (int i = 0; i < wordE.length(); i++) { 
      wordShuffled.add(word.charAt(i));// add the word 
     } 
     Collections.shuffle(wordShuffled);// shuffle the word 

我如何编写单元测试上面的代码。由于

+0

@CarlosZ提出了一个有效的观点。为什么测试Collections.shuffle(),因为我们知道它工作正常。在那种情况下,为什么测试这个功能呢?for循环也可以用标准API来替换,以将原始字符串转换为字符列表,从而将您的代码减少到一两行。 – Rahul 2011-03-26 04:20:57

回答

0

明显的通行证测试:

安排:创建一个已知的字。

:呼叫洗牌。

断言:检查结果是原词的排列。

然后寻找故障点或边界案件作进一步检查。

0

一对夫妇的,使这段代码难以测试的问题是:

  1. 你的功能洗牌声明为static
  2. 的函数调用Collections.shuffle,使得它真的很难嘲笑

这里有一些建议,你如何可以测试这种方法,尽管他们需要一些改变你的设计:

public class WordUtil { 
    public void shuffle(String word) { 

     // Store string into array 
     // -> It is better to code to the interface, eg, List, Set, Map 
     // than to the implementation, eg. ArrayList, LinkedList, HashMap, etc. 
     List<Character> wordShuffled = new ArrayList<Character>(); 
     // loop the index of the string 
     for (int i = 0; i < wordE.length(); i++) { 
      wordShuffled.add(word.charAt(i));// add the word 
     } 
     Collections.shuffle(wordShuffled);// shuffle the word 
    } 
    // we are making this method visible only for testing purposes but 
    // shouldn't be regarded as public API 
    void shuffle(Collection c) { 
     Collections.shuffle(c); 
    } 
} 

public class WordUtilTest { 
    private boolean shuffleForCollectionWasCalled; 
    private Collection collectionForShuffle; 

    public void testShuffle() throws Exception { 
     WordUtil util = new WordUtil_ForTest(); 
     String word = "some word"; 

     util.shuffle(word); 

     assertTrue(shuffleForCollectionWasCalled); 
     List<Character> expected = new ArrayList<Character>(); 
     for (int i = 0; i < word.length; i++) { 
      expected.add(word.charAt(i); 
     } 
     assertEquals(expected, collectionForShuffle); 
    } 

    private static class WordUtil_ForTest extends WordUtil { 

     @Override 
     void shuffle(Collection c) { 
      shuffleForCollectionWasCalled = true; 
      collectionForShuffle = c; 
     } 
    } 
} 

虽然这似乎有点令人费解,我们需要引入void shuffle(Collection c),因为它使我们能够控制的Collections.shuffle使其确定性的行为,我们在WordUtil_ForTest实现很简单,因为我们没有测试Collections.shuffle如何完成其​​工作(这应该在JDK中得到很好的测试),我们只关心我们发送正确的参数给它。

可以通过添加条件处理空输入和另一个边缘延伸的情况下该测试。

1

一个简单的检查是创建角色的一个HashMap与频率为原来的字。

例如,如果您的单词是“Doppelganger”,地图将为

D->1 
o->1 
p->2 
e->2 
l->1 
g->2 
a->1 
n->1 
r->1 

为混洗词创建类似地图。两个hashmaps应该是平等的。

但是,这只会检查混洗字是否包含与原始单词相同的字母。 @Shengyuan指出,你还应该通过检查字符串相等性并多次运行洗牌来检查该字是否实际上被洗牌。

0

比较结果shuffle()两次,assertFail()如果结果相同。