File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
main/kotlin/com/nhaarman/mockito_kotlin Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -175,6 +175,8 @@ inline fun <reified T : Any> mock(
175175 KStubbing (this ).stubbing(this )
176176}!!
177177
178+ inline fun <T : Any > T.stub (stubbing : KStubbing <T >.(T ) -> Unit ) = this .apply { KStubbing (this ).stubbing(this ) }
179+
178180@Deprecated(" Use mock() with optional arguments instead." , ReplaceWith (" mock<T>(defaultAnswer = a)" ), level = WARNING )
179181inline fun <reified T : Any > mock (a : Answer <Any >): T = mock(defaultAnswer = a)
180182
Original file line number Diff line number Diff line change @@ -516,6 +516,41 @@ class MockitoTest : TestBase() {
516516 expect(result).toBe(" argument-result" )
517517 }
518518
519+ @Test
520+ fun testMockStubbingAfterCreatingMock () {
521+ val mock = mock<Methods >()
522+
523+ // create stub after creation of mock
524+ mock.stub {
525+ on { stringResult() } doReturn " result"
526+ }
527+
528+ /* When */
529+ val result = mock.stringResult()
530+
531+ /* Then */
532+ expect(result).toBe(" result" )
533+ }
534+
535+ @Test
536+ fun testOverrideDefaultStub () {
537+ /* Given mock with stub */
538+ val mock = mock<Methods > {
539+ on { stringResult() } doReturn " result1"
540+ }
541+
542+ /* override stub */
543+ mock.stub {
544+ on { stringResult() } doReturn " result2"
545+ }
546+
547+ /* When */
548+ val result = mock.stringResult()
549+
550+ /* Then */
551+ expect(result).toBe(" result2" )
552+ }
553+
519554 @Test
520555 fun mock_withCustomName () {
521556 /* Given */
You can’t perform that action at this time.
0 commit comments