Last active
August 29, 2015 14:01
-
-
Save codingtony/b2d02a994136fe2fa353 to your computer and use it in GitHub Desktop.
Question asked on http://blog.codingtony.com/2011/02/mockito-recipes.html by Kalil Peixoto
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
import static org.junit.Assert.*; | |
import static org.mockito.Matchers.*; | |
import static org.mockito.Mockito.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.junit.Test; | |
@Test | |
public void testMockReturnArrayWithAnyList() { | |
List<Something> returnList = new ArrayList<Something>(); | |
Something a = new Something(); | |
a.setReturnCode(1);; | |
Something b = new Something(); | |
b.setReturnCode(2);; | |
returnList.add(a); | |
returnList.add(b); | |
InterfaceToMock myMock = mock(InterfaceToMock.class); | |
// here we have anyList in the expected call | |
when(myMock.getList(anyList())).thenReturn(returnList); | |
List<Something> test = myMock.getList(new ArrayList<Object>()); | |
assertEquals(returnList,test); | |
assertEquals(test.get(0),a); | |
test = myMock.getList(null); | |
// When the parameter is null it returns an empty list. | |
assertFalse(test.isEmpty()); | |
} | |
@Test | |
public void testMockReturnArrayWithNewArrayList() { | |
List<Something> returnList = new ArrayList<Something>(); | |
Something a = new Something(); | |
a.setReturnCode(1);; | |
Something b = new Something(); | |
b.setReturnCode(2);; | |
returnList.add(a); | |
returnList.add(b); | |
InterfaceToMock myMock = mock(InterfaceToMock.class); | |
// here we have new ArrayList<Object>() in the expected call | |
when(myMock.getList(new ArrayList<Object>())).thenReturn(returnList); | |
List<Something> test = myMock.getList(new ArrayList<Object>()); | |
assertEquals(returnList,test); | |
assertEquals(test.get(0),a); | |
test = myMock.getList(null); | |
// When the parameter is null it returns an empty list. | |
assertTrue(test.isEmpty()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment