Advertisement
jdalbey

Mockito Demo (Word, Sentence, Page)

Apr 21st, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.72 KB | None | 0 0
  1. /**
  2.  * Word is a string of lower case alphabetic characters.
  3.  * @author jdalbey
  4.  */
  5. public class Word
  6. {
  7.     /** Construct a word from a sequence of letters.
  8.      @throws IllegalArgumentException if letters aren't lower case alpha characters
  9.      */
  10.     public Word(String letters) {}
  11.     /** Return the length of the word */
  12.     public int length() { return 0; }
  13.     /** Return the number of vowels in the word */
  14.     public int vowels() { return 0; }
  15. }
  16.  
  17. import java.util.ArrayList;
  18. import java.util.List;
  19.  
  20. /**
  21.  * Sentence is a sequence of words.
  22.  * @author jdalbey
  23.  */
  24. public class Sentence
  25. {
  26.     List<Word> sentence = new ArrayList<Word>();
  27.     /** Construct an empty Sentence. */
  28.     public Sentence()    {}
  29.     public void add(Word item)
  30.     {
  31.         sentence.add(item);
  32.     }
  33.     /** Return the number of words in the sentence */
  34.     public int numberOfWords()
  35.     {
  36.         return sentence.size();
  37.     }
  38.     /** Return the length of the sentence in letters */
  39.     public int length()
  40.     {
  41.         int total = 0;
  42.         for (Word word: sentence)
  43.         {
  44.             total += word.length();
  45.         }
  46.         return total;
  47.     }
  48.     /** Return the number of vowels in the sentence */
  49.     public int totalVowels()
  50.     {
  51.         int total = 0;
  52.         for (Word word: sentence)
  53.         {
  54.             total += word.vowels();
  55.         }
  56.         return total;
  57.     }
  58.            
  59. }
  60. /**
  61.  * Page manipulates sentences
  62.  * @author jdalbey
  63.  */
  64. public class Page
  65. {
  66.     // Determine which sentence is longer
  67.     public static Sentence getlonger(Sentence alpha, Sentence beta)
  68.     {
  69.         Sentence result = alpha;
  70.         if (beta.length() > alpha.length())
  71.         {
  72.             result = beta;
  73.         }
  74.         return result;
  75.     }
  76. }
  77. import junit.framework.TestCase;
  78. import static org.mockito.Mockito.*;
  79. /**
  80.  * Test of Sentence class.
  81.  * @author jdalbey
  82.  */
  83. public class SentenceTest extends TestCase
  84. {
  85.     // First test doesn't need a mock
  86.     public void testNumberOfWords()
  87.     {
  88.         Word word = new Word("hello");  
  89.         Sentence alpha = new Sentence();
  90.         alpha.add(word);
  91.         alpha.add(word);      
  92.         assertEquals(2, alpha.numberOfWords());
  93.     }
  94.     public void testLength()
  95.     {
  96.         // Establish how we want the mock to behave
  97.         Word mockedWord = mock(Word.class);
  98.         when(mockedWord.length()).thenReturn(5).thenReturn(3);
  99.         // Test sentence length
  100.         Sentence alpha = new Sentence();
  101.         alpha.add(mockedWord);
  102.         alpha.add(mockedWord);
  103.         assertEquals(8, alpha.length());
  104.     }
  105.     public void testVowels()
  106.     {
  107.         // Establish how we want the mock to behave
  108.         Word mockedWord = mock(Word.class);
  109.         when(mockedWord.vowels()).thenReturn(1).thenReturn(2);
  110.         // Test vowel totalling
  111.         Sentence alpha = new Sentence();
  112.         alpha.add(mockedWord);
  113.         alpha.add(mockedWord);
  114.         alpha.add(mockedWord);
  115.         assertEquals(5, alpha.totalVowels());
  116.     }
  117. }
  118. import junit.framework.TestCase;
  119. import static org.mockito.Mockito.*;
  120. /**
  121.  * Test of Page class
  122.  * @author jdalbey
  123.  */
  124. public class PageTest extends TestCase
  125. {
  126.     /**
  127.      * Test of getlonger method, of class Page.
  128.      */
  129.     public void testGetlonger()
  130.     {
  131.         // Note: we don't need to mock Word
  132.         // Create 2 mock sentences
  133.         Sentence alpha = mock(Sentence.class);
  134.         Sentence beta = mock(Sentence.class);
  135.         when(alpha.length()).thenReturn(1);
  136.         when(beta.length()).thenReturn(2);
  137.        
  138.         assertTrue(beta == Page.getlonger(alpha, beta));
  139.        
  140.         Sentence charly = Page.getlonger(beta, alpha);
  141.         assertEquals(beta, charly);
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement