11package test
22
3- import com.nhaarman.expect.expect
4- import com.nhaarman.expect.expectErrorWithMessage
5- import com.nhaarman.expect.fail
3+ import com.nhaarman.expect.*
64import com.nhaarman.mockitokotlin2.*
75import org.junit.Test
86import org.mockito.Mockito
@@ -11,9 +9,8 @@ import org.mockito.exceptions.base.MockitoAssertionError
119import org.mockito.exceptions.verification.WantedButNotInvoked
1210import org.mockito.listeners.InvocationListener
1311import org.mockito.mock.SerializableMode.BASIC
14- import java.io.IOException
15- import java.io.PrintStream
16- import java.io.Serializable
12+ import org.mockito.stubbing.Answer
13+ import java.io.*
1714
1815
1916/*
@@ -327,7 +324,7 @@ class MockitoTest : TestBase() {
327324 try {
328325 mock.go()
329326 throw AssertionError (" Call should have thrown." )
330- } catch (e: IllegalStateException ) {
327+ } catch (e: IllegalStateException ) {
331328 }
332329 }
333330
@@ -425,7 +422,7 @@ class MockitoTest : TestBase() {
425422 /* When */
426423 mock.builderMethod()
427424 fail(" No exception thrown" )
428- } catch (e: IllegalArgumentException ) {
425+ } catch (e: IllegalArgumentException ) {
429426 }
430427 }
431428
@@ -440,7 +437,7 @@ class MockitoTest : TestBase() {
440437 /* When */
441438 mock.builderMethod()
442439 fail(" No exception thrown" )
443- } catch (e: IllegalArgumentException ) {
440+ } catch (e: IllegalArgumentException ) {
444441 }
445442 }
446443
@@ -455,14 +452,14 @@ class MockitoTest : TestBase() {
455452 /* When */
456453 mock.builderMethod()
457454 fail(" No exception thrown" )
458- } catch (e: IllegalArgumentException ) {
455+ } catch (e: IllegalArgumentException ) {
459456 }
460457
461458 try {
462459 /* When */
463460 mock.builderMethod()
464461 fail(" No exception thrown" )
465- } catch (e: UnsupportedOperationException ) {
462+ } catch (e: UnsupportedOperationException ) {
466463 }
467464 }
468465
@@ -477,19 +474,19 @@ class MockitoTest : TestBase() {
477474 /* When */
478475 mock.builderMethod()
479476 fail(" No exception thrown" )
480- } catch (e: IllegalArgumentException ) {
477+ } catch (e: IllegalArgumentException ) {
481478 }
482479
483480 try {
484481 /* When */
485482 mock.builderMethod()
486483 fail(" No exception thrown" )
487- } catch (e: UnsupportedOperationException ) {
484+ } catch (e: UnsupportedOperationException ) {
488485 }
489486 }
490487
491488 @Test
492- fun testMockStubbing_doAnswer () {
489+ fun testMockStubbing_doAnswer_lambda () {
493490 /* Given */
494491 val mock = mock<Methods > {
495492 on { stringResult() } doAnswer { " result" }
@@ -502,6 +499,21 @@ class MockitoTest : TestBase() {
502499 expect(result).toBe(" result" )
503500 }
504501
502+ @Test
503+ fun testMockStubbing_doAnswer_instance () {
504+ /* Given */
505+ val mock = mock<Methods > {
506+ on { stringResult() } doAnswer Answer <String > { " result" }
507+ }
508+
509+ /* When */
510+ val result = mock.stringResult()
511+
512+ /* Then */
513+ expect(result).toBe(" result" )
514+ }
515+
516+
505517 @Test
506518 fun testMockStubbing_doAnswer_withArgument () {
507519 /* Given */
@@ -659,7 +671,7 @@ class MockitoTest : TestBase() {
659671 /* When */
660672 verify(mock).stringResult()
661673 fail(" Expected an exception" )
662- } catch (e: WantedButNotInvoked ) {
674+ } catch (e: WantedButNotInvoked ) {
663675 /* Then */
664676 verify(out ).println (" methods.stringResult();" )
665677 }
@@ -768,7 +780,7 @@ class MockitoTest : TestBase() {
768780 /* When */
769781 verify(mock).stringResult()
770782 fail(" Expected an exception" )
771- } catch (e: WantedButNotInvoked ) {
783+ } catch (e: WantedButNotInvoked ) {
772784 /* Then */
773785 verify(out ).println (" methods.stringResult();" )
774786 }
@@ -869,7 +881,7 @@ class MockitoTest : TestBase() {
869881 /* When */
870882 verify(mock).stringResult()
871883 fail(" Expected an exception" )
872- } catch (e: WantedButNotInvoked ) {
884+ } catch (e: WantedButNotInvoked ) {
873885 /* Then */
874886 verify(out ).println (" methods.stringResult();" )
875887 }
0 commit comments