Skip to content

Instantly share code, notes, and snippets.

@luoyuping

luoyuping/java

Last active May 24, 2018
Embed
What would you like to do?
Blog/UT/difference_for_mock_and_spy
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