Skip to content

Instantly share code, notes, and snippets.

@dmcg
Created September 23, 2016 10:46
Show Gist options
  • Select an option

  • Save dmcg/3322003b0c8cd55549491c71d94309f9 to your computer and use it in GitHub Desktop.

Select an option

Save dmcg/3322003b0c8cd55549491c71d94309f9 to your computer and use it in GitHub Desktop.
Typesafe JMock actions
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