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(
175
175
KStubbing (this ).stubbing(this )
176
176
}!!
177
177
178
+ inline fun <T : Any > T.stub (stubbing : KStubbing <T >.(T ) -> Unit ) = this .apply { KStubbing (this ).stubbing(this ) }
179
+
178
180
@Deprecated(" Use mock() with optional arguments instead." , ReplaceWith (" mock<T>(defaultAnswer = a)" ), level = WARNING )
179
181
inline fun <reified T : Any > mock (a : Answer <Any >): T = mock(defaultAnswer = a)
180
182
Original file line number Diff line number Diff line change @@ -516,6 +516,41 @@ class MockitoTest : TestBase() {
516
516
expect(result).toBe(" argument-result" )
517
517
}
518
518
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
+
519
554
@Test
520
555
fun mock_withCustomName () {
521
556
/* Given */
You can’t perform that action at this time.
0 commit comments