• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockitousage.examples.use;
7 
8 import org.junit.Rule;
9 import org.junit.Test;
10 import org.mockito.InOrder;
11 import org.mockito.InjectMocks;
12 import org.mockito.Mock;
13 import org.mockito.junit.MockitoJUnit;
14 import org.mockito.junit.MockitoRule;
15 
16 import java.util.Arrays;
17 
18 import static org.mockito.Mockito.*;
19 
20 public class ExampleTest {
21 
22     @Rule public MockitoRule rule = MockitoJUnit.rule();
23 
24     @Mock private ArticleCalculator mockCalculator;
25     @Mock private ArticleDatabase mockDatabase;
26 
27     @InjectMocks private ArticleManager articleManager;
28 
29     @Test
managerCountsArticlesAndSavesThemInTheDatabase()30     public void managerCountsArticlesAndSavesThemInTheDatabase() {
31         when(mockCalculator.countArticles("Guardian")).thenReturn(12);
32         when(mockCalculator.countArticlesInPolish(anyString())).thenReturn(5);
33 
34         articleManager.updateArticleCounters("Guardian");
35 
36         verify(mockDatabase).updateNumberOfArticles("Guardian", 12);
37         verify(mockDatabase).updateNumberOfPolishArticles("Guardian", 5);
38         verify(mockDatabase).updateNumberOfEnglishArticles("Guardian", 7);
39     }
40 
41     @Test
managerCountsArticlesUsingCalculator()42     public void managerCountsArticlesUsingCalculator() {
43         articleManager.updateArticleCounters("Guardian");
44 
45         verify(mockCalculator).countArticles("Guardian");
46         verify(mockCalculator).countArticlesInPolish("Guardian");
47     }
48 
49     @Test
managerSavesArticlesInTheDatabase()50     public void managerSavesArticlesInTheDatabase() {
51         articleManager.updateArticleCounters("Guardian");
52 
53         verify(mockDatabase).updateNumberOfArticles("Guardian", 0);
54         verify(mockDatabase).updateNumberOfPolishArticles("Guardian", 0);
55         verify(mockDatabase).updateNumberOfEnglishArticles("Guardian", 0);
56     }
57 
58     @Test
managerUpdatesNumberOfRelatedArticles()59     public void managerUpdatesNumberOfRelatedArticles() {
60         Article articleOne = new Article();
61         Article articleTwo = new Article();
62         Article articleThree = new Article();
63 
64         when(mockCalculator.countNumberOfRelatedArticles(articleOne)).thenReturn(1);
65         when(mockCalculator.countNumberOfRelatedArticles(articleTwo)).thenReturn(12);
66         when(mockCalculator.countNumberOfRelatedArticles(articleThree)).thenReturn(0);
67 
68         when(mockDatabase.getArticlesFor("Guardian")).thenReturn(Arrays.asList(articleOne, articleTwo, articleThree));
69 
70         articleManager.updateRelatedArticlesCounters("Guardian");
71 
72         verify(mockDatabase).save(articleOne);
73         verify(mockDatabase).save(articleTwo);
74         verify(mockDatabase).save(articleThree);
75     }
76 
77     @Test
shouldPersistRecalculatedArticle()78     public void shouldPersistRecalculatedArticle() {
79         Article articleOne = new Article();
80         Article articleTwo = new Article();
81 
82         when(mockCalculator.countNumberOfRelatedArticles(articleOne)).thenReturn(1);
83         when(mockCalculator.countNumberOfRelatedArticles(articleTwo)).thenReturn(12);
84 
85         when(mockDatabase.getArticlesFor("Guardian")).thenReturn(Arrays.asList(articleOne, articleTwo));
86 
87         articleManager.updateRelatedArticlesCounters("Guardian");
88 
89         InOrder inOrder = inOrder(mockDatabase, mockCalculator);
90 
91         inOrder.verify(mockCalculator).countNumberOfRelatedArticles((Article) anyObject());
92         inOrder.verify(mockDatabase, atLeastOnce()).save((Article) anyObject());
93     }
94 }
95