diff options
Diffstat (limited to 'tests/com/google/testing/littlemock/LittleMockTest.java')
-rw-r--r-- | tests/com/google/testing/littlemock/LittleMockTest.java | 157 |
1 files changed, 143 insertions, 14 deletions
diff --git a/tests/com/google/testing/littlemock/LittleMockTest.java b/tests/com/google/testing/littlemock/LittleMockTest.java index fde8c7c..e7f9bca 100644 --- a/tests/com/google/testing/littlemock/LittleMockTest.java +++ b/tests/com/google/testing/littlemock/LittleMockTest.java @@ -37,8 +37,10 @@ import static com.google.testing.littlemock.LittleMock.doNothing; import static com.google.testing.littlemock.LittleMock.doReturn; import static com.google.testing.littlemock.LittleMock.doThrow; import static com.google.testing.littlemock.LittleMock.eq; +import static com.google.testing.littlemock.LittleMock.inOrder; import static com.google.testing.littlemock.LittleMock.initMocks; import static com.google.testing.littlemock.LittleMock.isA; +import static com.google.testing.littlemock.LittleMock.matches; import static com.google.testing.littlemock.LittleMock.mock; import static com.google.testing.littlemock.LittleMock.never; import static com.google.testing.littlemock.LittleMock.reset; @@ -48,6 +50,9 @@ import static com.google.testing.littlemock.LittleMock.verify; import static com.google.testing.littlemock.LittleMock.verifyNoMoreInteractions; import static com.google.testing.littlemock.LittleMock.verifyZeroInteractions; +import com.google.testing.littlemock.LittleMock.ArgumentMatcher; +import com.google.testing.littlemock.LittleMock.InOrder; + import junit.framework.TestCase; import java.io.IOException; @@ -1413,16 +1418,151 @@ public class LittleMockTest extends TestCase { } catch (IllegalStateException expected) {} } + public void testCustomMatcher() { + ArgumentMatcher argumentMatcher = new ArgumentMatcher() { + @Override + public boolean matches(Object value) { + return ((String) value).contains("[]"); + } + }; + mFoo.add("as[]df"); + mFoo.add("qwer[]asdf"); + mFoo.add("1234"); + verify(mFoo, times(3)).add(anyString()); + verify(mFoo, times(2)).add((String) matches(argumentMatcher)); + } + + public void testInorderExample_Success() { + @SuppressWarnings("unchecked") + List<String> firstMock = mock(List.class); + @SuppressWarnings("unchecked") + List<String> secondMock = mock(List.class); + firstMock.add("was called first"); + secondMock.add("was called second"); + InOrder inOrder = inOrder(firstMock, secondMock); + inOrder.verify(firstMock).add("was called first"); + inOrder.verify(secondMock).add("was called second"); + } + + public void testInorderExample_Failure() { + @SuppressWarnings("unchecked") + List<String> firstMock = mock(List.class); + @SuppressWarnings("unchecked") + List<String> secondMock = mock(List.class); + firstMock.add("was called first"); + secondMock.add("was called second"); + InOrder inOrder = inOrder(firstMock, secondMock); + inOrder.verify(secondMock).add("was called second"); + try { + inOrder.verify(firstMock).add("was called first"); + throw new IllegalStateException(); + } catch (AssertionError expected) {} + } + + public void testInorderInterleave() { + @SuppressWarnings("unchecked") + List<String> firstMock = mock(List.class); + firstMock.add("a"); + firstMock.add("b"); + firstMock.add("a"); + + // Should be fine to verify a then b, since they happened in that order. + InOrder inOrder = inOrder(firstMock); + inOrder.verify(firstMock).add("a"); + inOrder.verify(firstMock).add("b"); + + // Should also be fine to inorder verify the other way around, they happened in that order too. + inOrder = inOrder(firstMock); + inOrder.verify(firstMock).add("b"); + inOrder.verify(firstMock).add("a"); + + // Should be fine to verify "a, b, a" since that too happened. + inOrder = inOrder(firstMock); + inOrder.verify(firstMock).add("a"); + inOrder.verify(firstMock).add("b"); + inOrder.verify(firstMock).add("a"); + + // "a, a, b" did not happen. + inOrder = inOrder(firstMock); + inOrder.verify(firstMock).add("a"); + inOrder.verify(firstMock).add("a"); + try { + inOrder.verify(firstMock).add("b"); + throw new IllegalStateException(); + } catch (AssertionError expected) {} + + // "b, a, b" did not happen. + inOrder = inOrder(firstMock); + inOrder.verify(firstMock).add("b"); + inOrder.verify(firstMock).add("a"); + try { + inOrder.verify(firstMock).add("b"); + throw new IllegalStateException(); + } catch (AssertionError expected) {} + + // "b" did not happen twice. + inOrder = inOrder(firstMock); + inOrder.verify(firstMock).add("b"); + try { + inOrder.verify(firstMock).add("b"); + throw new IllegalStateException(); + } catch (AssertionError expected) {} + } + + public void testInorderComplicatedExample() { + // TODO: I'm currently totally ignoring the parameters passed to the inorder method. + // I don't understand what the point of them is, anyway. + @SuppressWarnings("unchecked") + List<String> firstMock = mock(List.class); + @SuppressWarnings("unchecked") + List<String> secondMock = mock(List.class); + + firstMock.add("1"); + secondMock.add("2"); + firstMock.add("3"); + secondMock.add("4"); + + InOrder allInOrder = inOrder(firstMock, secondMock); + allInOrder.verify(firstMock).add("1"); + allInOrder.verify(secondMock).add("2"); + allInOrder.verify(firstMock).add("3"); + allInOrder.verify(secondMock).add("4"); + + InOrder firstInOrder = inOrder(firstMock, secondMock); + firstInOrder.verify(firstMock).add("1"); + firstInOrder.verify(firstMock).add("3"); + try { + firstInOrder.verify(secondMock).add("2"); + throw new IllegalStateException(); + } catch (AssertionError expected) {} + firstInOrder.verify(secondMock).add("4"); + + InOrder secondInOrder = inOrder(firstMock, secondMock); + secondInOrder.verify(secondMock).add("2"); + secondInOrder.verify(secondMock).add("4"); + try { + secondInOrder.verify(firstMock).add("1"); + throw new IllegalStateException(); + } catch (AssertionError expected) {} + try { + secondInOrder.verify(firstMock).add("3"); + throw new IllegalStateException(); + } catch (AssertionError expected) {} + } + public static class Jim { - public void bob() { + public int bob() { fail(); + return 3; } } // Does not work on JVM, android only. - public void suppress_testMockingConcreteClasses() throws Exception { + public void testMockingConcreteClasses() throws Exception { Jim mock = mock(Jim.class); - mock.bob(); + assertEquals(0, mock.bob()); + doReturn(8).when(mock).bob(); + assertEquals(8, mock.bob()); } private Future<Void> invokeBarMethodAfterLatchAwait(final CountDownLatch countDownLatch) { @@ -1439,17 +1579,6 @@ public class LittleMockTest extends TestCase { // TODO(hugohudson): 5. Every method that throws exceptions could be improved by adding // test for the content of the error message. - // TODO(hugohudson): 5. Add InOrder class, so that we can check that the given methods on - // the given mocks happen in the right order. It will be pretty easy to do. The syntax - // looks like this: - // InOrder inOrder = inOrder(firstMock, secondMock); - // inOrder.verify(firstMock).firstMethod(); - // inOrder.verify(secondMock).secondMethod(); - // This allows us to verify that the calls happened in the desired order. - // By far the simplest way to do this is have a static AtomicInteger on the class which - // indicates exactly when every method call happened, and then just compare order based on - // that. - // TODO(hugohudson): 5. Make the doReturn() method take variable arguments. // The syntax is: // doReturn(1, 2, 3).when(mFoo).anInt(); |