Merge pull request #7033 from Maiklins/BAEL-2891

Bael 2891
This commit is contained in:
rpvilao 2019-05-31 20:13:06 +02:00 committed by GitHub
commit 190fa8f071
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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);
}