@@ -5,8 +5,15 @@ import com.nhaarman.expect.expectErrorWithMessage
5
5
import com.nhaarman.expect.fail
6
6
import com.nhaarman.mockito_kotlin.*
7
7
import org.junit.Test
8
+ import org.mockito.Mockito
8
9
import org.mockito.exceptions.base.MockitoAssertionError
10
+ import org.mockito.exceptions.verification.WantedButNotInvoked
11
+ import org.mockito.listeners.InvocationListener
12
+ import org.mockito.mock.SerializableMode.BASIC
9
13
import java.io.IOException
14
+ import java.io.PrintStream
15
+ import java.io.Serializable
16
+
10
17
11
18
/*
12
19
* The MIT License
@@ -244,8 +251,8 @@ class MockitoTest : TestBase() {
244
251
val mock = mock<Methods >()
245
252
246
253
doAnswer { " Test" }
247
- .whenever(mock)
248
- .stringResult()
254
+ .whenever(mock)
255
+ .stringResult()
249
256
250
257
expect(mock.stringResult()).toBe(" Test" )
251
258
}
@@ -479,6 +486,160 @@ class MockitoTest : TestBase() {
479
486
}
480
487
}
481
488
489
+ @Test
490
+ fun mock_withCustomName () {
491
+ /* Given */
492
+ val mock = mock<Methods >(" myName" )
493
+
494
+ /* Expect */
495
+ expectErrorWithMessage(" myName.stringResult()" ) on {
496
+ verify(mock).stringResult()
497
+ }
498
+ }
499
+
500
+ @Test
501
+ fun mock_withCustomDefaultAnswer () {
502
+ /* Given */
503
+ val mock = mock<Methods >(Mockito .RETURNS_SELF )
504
+
505
+ /* When */
506
+ val result = mock.builderMethod()
507
+
508
+ /* Then */
509
+ expect(result).toBe(mock)
510
+ }
511
+
512
+ @Test
513
+ fun mock_withCustomDefaultAnswer_parameterName () {
514
+ /* Given */
515
+ val mock = mock<Methods >(defaultAnswer = Mockito .RETURNS_SELF )
516
+
517
+ /* When */
518
+ val result = mock.builderMethod()
519
+
520
+ /* Then */
521
+ expect(result).toBe(mock)
522
+ }
523
+
524
+ @Test
525
+ fun mock_withSettings_extraInterfaces () {
526
+ /* Given */
527
+ val mock = mock<Methods >(
528
+ withSettings().extraInterfaces(ExtraInterface ::class .java)
529
+ )
530
+
531
+ /* Then */
532
+ expect(mock).toBeInstanceOf<ExtraInterface >()
533
+ }
534
+
535
+ @Test
536
+ fun mock_withSettings_name () {
537
+ /* Given */
538
+ val mock = mock<Methods >(
539
+ withSettings().name(" myName" )
540
+ )
541
+
542
+ /* When */
543
+ expectErrorWithMessage(" myName.stringResult()" ) on {
544
+ verify(mock).stringResult()
545
+ }
546
+ }
547
+
548
+ @Test
549
+ fun mock_withSettings_defaultAnswer () {
550
+ /* Given */
551
+ val mock = mock<Methods >(
552
+ withSettings().defaultAnswer(Mockito .RETURNS_MOCKS )
553
+ )
554
+
555
+ /* When */
556
+ val result = mock.nonDefaultReturnType()
557
+
558
+ /* Then */
559
+ expect(result).toNotBeNull()
560
+ }
561
+
562
+ @Test
563
+ fun mock_withSettings_serializable () {
564
+ /* Given */
565
+ val mock = mock<Methods >(
566
+ withSettings().serializable()
567
+ )
568
+
569
+ /* Then */
570
+ expect(mock).toBeInstanceOf<Serializable >()
571
+ }
572
+
573
+ @Test
574
+ fun mock_withSettings_serializableMode () {
575
+ /* Given */
576
+ val mock = mock<Methods >(
577
+ withSettings().serializable(BASIC )
578
+ )
579
+
580
+ /* Then */
581
+ expect(mock).toBeInstanceOf<Serializable >()
582
+ }
583
+
584
+ @Test
585
+ fun mock_withSettings_verboseLogging () {
586
+ /* Given */
587
+ val out = mock<PrintStream >()
588
+ System .setOut(out )
589
+ val mock = mock<Methods >(
590
+ withSettings().verboseLogging()
591
+ )
592
+
593
+ try {
594
+ /* When */
595
+ verify(mock).stringResult()
596
+ fail(" Expected an exception" )
597
+ } catch (e: WantedButNotInvoked ) {
598
+ /* Then */
599
+ verify(out ).println (" methods.stringResult();" )
600
+ }
601
+ }
602
+
603
+ @Test
604
+ fun mock_withSettings_invocationListeners () {
605
+ /* Given */
606
+ var bool = false
607
+ val mock = mock<Methods >(
608
+ withSettings().invocationListeners(InvocationListener { bool = true })
609
+ )
610
+
611
+ /* When */
612
+ mock.stringResult()
613
+
614
+ /* Then */
615
+ expect(bool).toHold()
616
+ }
617
+
618
+ @Test
619
+ fun mock_withSettings_stubOnly () {
620
+ /* Given */
621
+ val mock = mock<Methods >(
622
+ withSettings().stubOnly()
623
+ )
624
+
625
+ /* Expect */
626
+ expectErrorWithMessage(" is a stubOnly() mock" ) on {
627
+
628
+ /* When */
629
+ verify(mock).stringResult()
630
+ }
631
+ }
632
+
633
+ @Test
634
+ fun mock_withSettings_useConstructor () {
635
+ /* Expect */
636
+ expectErrorWithMessage(" Unable to create mock instance of type" ) on {
637
+ mock<ThrowingConstructor >(
638
+ withSettings().useConstructor()
639
+ )
640
+ }
641
+ }
642
+
482
643
@Test
483
644
fun stubbingTwiceWithArgumentMatchers () {
484
645
/* When */
0 commit comments