@@ -5,8 +5,15 @@ import com.nhaarman.expect.expectErrorWithMessage
55import com.nhaarman.expect.fail
66import com.nhaarman.mockito_kotlin.*
77import org.junit.Test
8+ import org.mockito.Mockito
89import 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
913import java.io.IOException
14+ import java.io.PrintStream
15+ import java.io.Serializable
16+
1017
1118/*
1219 * The MIT License
@@ -244,8 +251,8 @@ class MockitoTest : TestBase() {
244251 val mock = mock<Methods >()
245252
246253 doAnswer { " Test" }
247- .whenever(mock)
248- .stringResult()
254+ .whenever(mock)
255+ .stringResult()
249256
250257 expect(mock.stringResult()).toBe(" Test" )
251258 }
@@ -479,6 +486,160 @@ class MockitoTest : TestBase() {
479486 }
480487 }
481488
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+
482643 @Test
483644 fun stubbingTwiceWithArgumentMatchers () {
484645 /* When */
0 commit comments