Created
September 23, 2016 10:46
-
-
Save dmcg/3322003b0c8cd55549491c71d94309f9 to your computer and use it in GitHub Desktop.
Typesafe JMock actions
This file contains hidden or 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
| class MockingTesting { | |
| @Rule @JvmField val mockery = JUnitRuleMockery() | |
| val charSequence = mockery.createMock<CharSequence>() | |
| @Test fun `which will taking untyped Action`() { | |
| mockery.expecting { | |
| oneOf(charSequence).length.which will org.jmock.Expectations.returnValue(42) | |
| oneOf(charSequence).length.which will org.jmock.Expectations.throwException(RuntimeException()) | |
| oneOf(charSequence).length.which will org.jmock.Expectations.returnValue("banana") | |
| } | |
| assertEquals(42, charSequence.length) | |
| assertFails { | |
| charSequence.length // RuntimeException | |
| } | |
| assertFails { | |
| charSequence.length // class cast from "banana" to Int | |
| } | |
| } | |
| @Test fun `which will taking TypedAction`() { | |
| mockery.expecting { | |
| oneOf(charSequence).length.which will returnValue(42) | |
| oneOf(charSequence).length.which will throwException(RuntimeException()) | |
| // oneOf(charSequence).length.which will returnValue("banana") // doesn't compile | |
| } | |
| assertEquals(42, charSequence.length) | |
| assertFails { | |
| charSequence.length | |
| } | |
| } | |
| @Test fun `which will taking typed lambda`() { | |
| mockery.expecting { | |
| oneOf(charSequence).length.which will { 42 } | |
| oneOf(charSequence).length.which will { throw RuntimeException() } | |
| // oneOf(charSequence).length.which will { "banana" } // doesn't compile | |
| } | |
| assertEquals(42, charSequence.length) | |
| assertFails { | |
| charSequence.length | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment