BAEL-2891 Add a new section in the Mockito Annotations article

This commit is contained in:
mikr 2019-05-27 00:32:25 +02:00
parent ee9763c0d8
commit 5b11cef14b
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package org.baeldung.mockito;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
@RunWith(MockitoJUnitRunner.class)
public class MockitoInjectIntoSpyUnitTest {
@Before
public void init() {
MockitoAnnotations.initMocks(this);
spyDic = Mockito.spy(new MyDictionary(wordMap));
}
@Mock
private Map<String, String> wordMap;
@InjectMocks
private MyDictionary dic = new MyDictionary();
private MyDictionary spyDic;
@Test
public void whenUseInjectMocksAnnotation_thenCorrect2() {
Mockito.when(wordMap.get("aWord")).thenReturn("aMeaning");
assertEquals("aMeaning", spyDic.getMeaning("aWord"));
}
}

View File

@ -11,6 +11,10 @@ class MyDictionary {
wordMap = new HashMap<>();
}
MyDictionary(Map<String, String> wordMap) {
this.wordMap = wordMap;
}
public void add(final String word, final String meaning) {
wordMap.put(word, meaning);
}