Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Word is a string of lower case alphabetic characters.
- * @author jdalbey
- */
- public class Word
- {
- /** Construct a word from a sequence of letters.
- @throws IllegalArgumentException if letters aren't lower case alpha characters
- */
- public Word(String letters) {}
- /** Return the length of the word */
- public int length() { return 0; }
- /** Return the number of vowels in the word */
- public int vowels() { return 0; }
- }
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Sentence is a sequence of words.
- * @author jdalbey
- */
- public class Sentence
- {
- List<Word> sentence = new ArrayList<Word>();
- /** Construct an empty Sentence. */
- public Sentence() {}
- public void add(Word item)
- {
- sentence.add(item);
- }
- /** Return the number of words in the sentence */
- public int numberOfWords()
- {
- return sentence.size();
- }
- /** Return the length of the sentence in letters */
- public int length()
- {
- int total = 0;
- for (Word word: sentence)
- {
- total += word.length();
- }
- return total;
- }
- /** Return the number of vowels in the sentence */
- public int totalVowels()
- {
- int total = 0;
- for (Word word: sentence)
- {
- total += word.vowels();
- }
- return total;
- }
- }
- /**
- * Page manipulates sentences
- * @author jdalbey
- */
- public class Page
- {
- // Determine which sentence is longer
- public static Sentence getlonger(Sentence alpha, Sentence beta)
- {
- Sentence result = alpha;
- if (beta.length() > alpha.length())
- {
- result = beta;
- }
- return result;
- }
- }
- import junit.framework.TestCase;
- import static org.mockito.Mockito.*;
- /**
- * Test of Sentence class.
- * @author jdalbey
- */
- public class SentenceTest extends TestCase
- {
- // First test doesn't need a mock
- public void testNumberOfWords()
- {
- Word word = new Word("hello");
- Sentence alpha = new Sentence();
- alpha.add(word);
- alpha.add(word);
- assertEquals(2, alpha.numberOfWords());
- }
- public void testLength()
- {
- // Establish how we want the mock to behave
- Word mockedWord = mock(Word.class);
- when(mockedWord.length()).thenReturn(5).thenReturn(3);
- // Test sentence length
- Sentence alpha = new Sentence();
- alpha.add(mockedWord);
- alpha.add(mockedWord);
- assertEquals(8, alpha.length());
- }
- public void testVowels()
- {
- // Establish how we want the mock to behave
- Word mockedWord = mock(Word.class);
- when(mockedWord.vowels()).thenReturn(1).thenReturn(2);
- // Test vowel totalling
- Sentence alpha = new Sentence();
- alpha.add(mockedWord);
- alpha.add(mockedWord);
- alpha.add(mockedWord);
- assertEquals(5, alpha.totalVowels());
- }
- }
- import junit.framework.TestCase;
- import static org.mockito.Mockito.*;
- /**
- * Test of Page class
- * @author jdalbey
- */
- public class PageTest extends TestCase
- {
- /**
- * Test of getlonger method, of class Page.
- */
- public void testGetlonger()
- {
- // Note: we don't need to mock Word
- // Create 2 mock sentences
- Sentence alpha = mock(Sentence.class);
- Sentence beta = mock(Sentence.class);
- when(alpha.length()).thenReturn(1);
- when(beta.length()).thenReturn(2);
- assertTrue(beta == Page.getlonger(alpha, beta));
- Sentence charly = Page.getlonger(beta, alpha);
- assertEquals(beta, charly);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement