-
-
Save luoyuping/8a2f6a4efeffea581f18136952ebe567 to your computer and use it in GitHub Desktop.
Blog/UT/difference_for_mock_and_spy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sankuai.zcm.trade.core.reverse; | |
import javafx.beans.binding.When; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.Mock; | |
import org.mockito.Mockito; | |
import org.mockito.MockitoAnnotations; | |
import org.mockito.Spy; | |
import org.mockito.runners.MockitoJUnitRunner; | |
import java.util.ArrayList; | |
import java.util.List; | |
import static org.junit.Assert.assertEquals; | |
/** | |
* Created by luo on 14/05/2018. | |
*/ | |
@RunWith(MockitoJUnitRunner.class) | |
public class SpyAndMockDiff { | |
@Mock | |
List<String> mockList; | |
// @Spy | |
// List<String> spyList; | |
@Spy | |
List<String> spyList = new ArrayList<String>(); | |
@Before | |
public void setUp() throws Exception { | |
MockitoAnnotations.initMocks(this); | |
} | |
@Test | |
public void testMock() { | |
mockList.add("one"); | |
assertEquals(0, mockList.size()); | |
Mockito.when(mockList.size()).thenReturn(100); | |
assertEquals(100, mockList.size()); | |
} | |
@Test | |
public void testSpy() { | |
spyList.add("one"); | |
assertEquals(1, spyList.size()); | |
Mockito.verify(spyList).add("one"); | |
Mockito.when(spyList.size()).thenReturn(100); | |
assertEquals(100, spyList.size()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment