1
1
package test
2
2
3
- import com.nhaarman.expect.expect
4
- import com.nhaarman.expect.expectErrorWithMessage
5
- import com.nhaarman.expect.fail
3
+ import com.nhaarman.expect.*
6
4
import com.nhaarman.mockitokotlin2.*
7
5
import org.junit.Test
8
6
import org.mockito.Mockito
@@ -11,9 +9,8 @@ import org.mockito.exceptions.base.MockitoAssertionError
11
9
import org.mockito.exceptions.verification.WantedButNotInvoked
12
10
import org.mockito.listeners.InvocationListener
13
11
import 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.*
17
14
18
15
19
16
/*
@@ -327,7 +324,7 @@ class MockitoTest : TestBase() {
327
324
try {
328
325
mock.go()
329
326
throw AssertionError (" Call should have thrown." )
330
- } catch (e: IllegalStateException ) {
327
+ } catch (e: IllegalStateException ) {
331
328
}
332
329
}
333
330
@@ -425,7 +422,7 @@ class MockitoTest : TestBase() {
425
422
/* When */
426
423
mock.builderMethod()
427
424
fail(" No exception thrown" )
428
- } catch (e: IllegalArgumentException ) {
425
+ } catch (e: IllegalArgumentException ) {
429
426
}
430
427
}
431
428
@@ -440,7 +437,7 @@ class MockitoTest : TestBase() {
440
437
/* When */
441
438
mock.builderMethod()
442
439
fail(" No exception thrown" )
443
- } catch (e: IllegalArgumentException ) {
440
+ } catch (e: IllegalArgumentException ) {
444
441
}
445
442
}
446
443
@@ -455,14 +452,14 @@ class MockitoTest : TestBase() {
455
452
/* When */
456
453
mock.builderMethod()
457
454
fail(" No exception thrown" )
458
- } catch (e: IllegalArgumentException ) {
455
+ } catch (e: IllegalArgumentException ) {
459
456
}
460
457
461
458
try {
462
459
/* When */
463
460
mock.builderMethod()
464
461
fail(" No exception thrown" )
465
- } catch (e: UnsupportedOperationException ) {
462
+ } catch (e: UnsupportedOperationException ) {
466
463
}
467
464
}
468
465
@@ -477,19 +474,19 @@ class MockitoTest : TestBase() {
477
474
/* When */
478
475
mock.builderMethod()
479
476
fail(" No exception thrown" )
480
- } catch (e: IllegalArgumentException ) {
477
+ } catch (e: IllegalArgumentException ) {
481
478
}
482
479
483
480
try {
484
481
/* When */
485
482
mock.builderMethod()
486
483
fail(" No exception thrown" )
487
- } catch (e: UnsupportedOperationException ) {
484
+ } catch (e: UnsupportedOperationException ) {
488
485
}
489
486
}
490
487
491
488
@Test
492
- fun testMockStubbing_doAnswer () {
489
+ fun testMockStubbing_doAnswer_lambda () {
493
490
/* Given */
494
491
val mock = mock<Methods > {
495
492
on { stringResult() } doAnswer { " result" }
@@ -502,6 +499,21 @@ class MockitoTest : TestBase() {
502
499
expect(result).toBe(" result" )
503
500
}
504
501
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
+
505
517
@Test
506
518
fun testMockStubbing_doAnswer_withArgument () {
507
519
/* Given */
@@ -659,7 +671,7 @@ class MockitoTest : TestBase() {
659
671
/* When */
660
672
verify(mock).stringResult()
661
673
fail(" Expected an exception" )
662
- } catch (e: WantedButNotInvoked ) {
674
+ } catch (e: WantedButNotInvoked ) {
663
675
/* Then */
664
676
verify(out ).println (" methods.stringResult();" )
665
677
}
@@ -768,7 +780,7 @@ class MockitoTest : TestBase() {
768
780
/* When */
769
781
verify(mock).stringResult()
770
782
fail(" Expected an exception" )
771
- } catch (e: WantedButNotInvoked ) {
783
+ } catch (e: WantedButNotInvoked ) {
772
784
/* Then */
773
785
verify(out ).println (" methods.stringResult();" )
774
786
}
@@ -869,7 +881,7 @@ class MockitoTest : TestBase() {
869
881
/* When */
870
882
verify(mock).stringResult()
871
883
fail(" Expected an exception" )
872
- } catch (e: WantedButNotInvoked ) {
884
+ } catch (e: WantedButNotInvoked ) {
873
885
/* Then */
874
886
verify(out ).println (" methods.stringResult();" )
875
887
}
0 commit comments