From cf0347810cce9906d23bddff5f3bdc1f7b22e25a Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Tue, 27 Dec 2016 20:19:29 +0100 Subject: [PATCH 1/7] Cleanup --- tmp.txt | 181 -------------------------------------------------------- 1 file changed, 181 deletions(-) delete mode 100644 tmp.txt diff --git a/tmp.txt b/tmp.txt deleted file mode 100644 index 171670e6..00000000 --- a/tmp.txt +++ /dev/null @@ -1,181 +0,0 @@ -a1ea33b Update Dokka to 0.9.11 -e3344f4 Update Gradle wrapper to 3.2.1 -3c714eb Update Kotlin to 1.0.5-3 -8118f1a Update Mockito to 2.3.9 -327d431 Merge pull request #137 from nhaarman/release-1.0.1 -8c9e5d7 Merge branch 'master' into dev -e07e419 Update PULL_REQUEST_TEMPLATE.md -263344f Merge pull request #135 from nhaarman/handle-null-in-argumentmatcher -b77d969 Handle `null` in argument matchers. -543238f Merge pull request #131 from nhaarman/release-1.0.0 -ed6d30d Update tagging script -af1940c Update Mockito to 2.2.28 -0ba47f4 Update dokka plugin -04cd24d Merge pull request #130 from nhaarman/release-1.0.0-RC1 -5418cff Update README.md -559ffc5 Remove deprecated methods -dc4ab0a Update publishing -a734cde Test with Kotlin 1.1-M03 -7fb4bd9 Merge pull request #127 from nhaarman/release-0.12.2 -05a65ec Update Mockito to 2.2.22 -62f3170 Merge branch 'ghostbuster91-FeatureRequest_isAclassShouldReturnInstance' into dev -acde82f Change isA method to return instance of T -72279be Merge pull request #123 from nhaarman/release-0.12.1 -c974db0 Update Mockito to 2.2.17 -e6f6d2c Update Kotlin to 1.0.5-2 -b665264 Merge pull request #122 from nhaarman/inorder-lambda -59bf311 Add a lambda to inOrder() for easier verification -b50831b Fix release build config -ebc6147 Merge pull request #121 from nhaarman/release-0.12.0 -66d8249 Also test with Kotlin 1.1 -cdc34a0 Merge pull request #120 from nhaarman/cast-null -18ed580 Use `null as T` to create instances -ae1d863 Merge pull request #119 from nhaarman/clean-tests -ab1eb0d Use same test source for inline tests -2b31ed7 Merge branch 'master' into dev -57d0d8b Merge pull request #118 from nhaarman/improve-argumentcaptor -81a0996 Add value shortcuts to ArgumentCaptor -37f1717 Merge pull request #117 from nhaarman/nullable-eq -1eead3e Return passed value instead of trying to create an instance -a831657 Update Mockito to 2.2.15 -5f12032 Merge pull request #113 from rongi/patch-1 -e7ed7cb Update to Kotlin 1.0.5 -27c6bf6 Merge pull request #111 from nhaarman/release-0.11.0 -771f761 Merge pull request #110 from nhaarman/graciously_fail -c8fecf0 Try to use constructor when mocking fails -fa27978 Merge pull request #109 from nhaarman/nullable-mockstubbing -f353562 Accept nullable return types when stubbing -50a1b91 Merge pull request #108 from nhaarman/release-0.10.1 -c3f7e68 Merge pull request #107 from nhaarman/create-primitive-instance -0615231 Don't try to mock primitive/wrapper instances -494e7ca Merge pull request #105 from nhaarman/fix-generic-returntype-npe -113d9e9 Catch an NPE thrown by the Kotlin type checker in the Mockito library. -3b5c40a Merge pull request #102 from nhaarman/release-0.10.0 -6af0904 Update Mockito to 2.2.9 -287065a Merge pull request #101 from nhaarman/skip-optional-parameters -42cee20 Don't count optional parameters when trying to find an easy constructor -e43ccea Merge pull request #100 from nhaarman/nullable-argumentcaptor -086d482 Add `nullableArgumentCaptor` to be able to work with lists of nullables -7b6c5b7 Merge branch 'desilvai-dev' into dev -395e578 Fixed infinite loop caused by 'copy constructors' invoked by createInstance. The fix is to skip all constructors that take in parameters with same type as the object we are trying to build. -24108bd Merge pull request #98 from nhaarman/release-0.9.0 -6126454 Update version badge -5a31910 Merge pull request #97 from nhaarman/deprecation-levels -63826bf Upgrade deprecation levels to ERROR -3db9e21 Update Mockito to 2.2.6 -5884adc Merge pull request #96 from nhaarman/dokka -fb74552 Use dokka do generate docs -f165976 Merge pull request #95 from nhaarman/publish-to-maven-central -8e083ad Publish to Maven Central instead of Bintray -5878bbe Merge pull request #93 from nhaarman/release-0.8.0 -d556691 Update Mockito to 2.2.1 -6ee75f7 Merge pull request #92 from nhaarman/create-array-instance -0b43d0b Use the java class' name to create an array instance -cd16b04 Merge pull request #91 from nhaarman/improve-captors -80dbefd Improve argument captors -fd98501 Merge pull request #90 from nhaarman/ongoingstubbing-dothrow -94b3c65 Added doThrow methods to OngoingStubbing -85bb5e4 Merge branch 'desilvai-dev' into dev -6965414 Fix compatibility with mock-maker-inline for mocking final classes -ea6e64c Merge pull request #85 from nhaarman/release-0.7.0 -36e3dd5 Merge pull request #84 from nhaarman/fix-createinstance-from-lambda -1d65fc0 Strip methods from file identifiers. -1025568 Merge pull request #83 from nhaarman/stubbing-mock-reference -0c3e137 Provide mock instance to stubbing method in mock() -3f73e27 Merge pull request #82 from nhaarman/mockito-rc2 -e549d8d Update Mockito dependency to 2.1.0 -0886485 Update Mockito dependency to RC2 -2d724bc Merge pull request #74 from viniciussoares/dev -f2ed004 Upgrade Kotlin to 1.0.4. -9c4234e Merge pull request #73 from nhaarman/release-0.6.2 -cf06555 Merge pull request #72 from nhaarman/any-or-null -9351840 Include `anyOrNull()` -3d64888 Merge pull request #71 from jpshelley/mockito-2.1.0-RC.1 -6862ea5 update to RC.1 -4c09ebc Merge branch 'master' into dev -f18d86e Merge pull request #70 from nhaarman/release-0.6.1 -650e293 Update README.md -f291e71 Merge pull request #69 from nhaarman/argforwhich -9a3b7de Introduce argForWhich() as an alias for argThat() -8d53bad Merge pull request #68 from nhaarman/fix-anyarrayofnullables -9507a20 Make anyArray() accept nullable types. -7dcebc9 Stop. Sending me. Emails. -c029a13 Merge pull request #67 from nhaarman/deprecate-deprecated-methods -400b1cc Deprecate methods that are deprecated in Mockito. -c06d37f Merge pull request #64 from nhaarman/fix-simplest-constructor -18be968 Improve `easiestConstructor()` to sort by parameters size. -855c14b Update gradle wrapper to 3.0 -58013d8 Update dependencies -6110a90 Update Mockito version to 2.1.0-beta.125 -6cf8557 Update README.md -d07a2c9 Merge pull request #59 from nhaarman/release-0.6.0 -2a2ed98 Merge pull request #58 from nhaarman/mock-stubbing -ecfaf58 Add infix modifiers to doReturn methods -2d4eef9 Accept a stubbing lambda in mock() -84fb7e0 Merge branch 'master' into dev -49d59a6 Merge pull request #55 from sghill/mockito-2.0.99-beta -2f94f52 Upgrade Mockito to 2.0.99-beta -a6f8604 Merge pull request #53 from nhaarman/release-0.5.2 -51385ca Added an example to the Readme on Argument Captors. -6a521ea Make `doReturn` accept null values. -c3e6182 Specify explicit return types for Mockito calls. -6867394 Merge pull request #50 from Zoltu/patch-1 -25fb19c Upgrade Kotlin version to 1.0.3. -e1de1f6 Makes `mock` return non-null types. -bb4ac76 Merge pull request #43 from nhaarman/release-0.5.1 -8e6ab15 Update gradle wrapper version -c400381 Merge pull request #42 from hamen/master -c44c15a Update Kotlin version -62cd94f Merge pull request #38 from nhaarman/release-0.5.0 -aa49bde Merge pull request #23 from nhaarman/instance-creators -f765a31 Use instance creators only from single test file. -17dadf9 Update README.md -cddcf7f Introduce instance creators. -4cbd876 Merge pull request #36 from nhaarman/release-0.4.1 -abfc083 Update dependencies -772d0d9 Merge pull request #33 from nhaarman/release-0.4.0 -1322b92 Update dependencies -55e9a3c Merge pull request #30 from nhaarman/release-0.3.1 -df0c3f6 Remove Travis-CI cache -d4e73a9 Merge pull request #29 from nhaarman/uncheckedmock-interceptor -c8d9f68 Unset the MockitoInterceptor for unchecked mocks. -129f9e6 Merge pull request #28 from nhaarman/create-class-objects -ae4ab17 Account for Class when creating instances. -a013a48 Merge pull request #19 from nhaarman/avoid-array-parameters -ea0263d Avoid constructors with an array as a parameter. -48fc1aa Merge pull request #16 from nhaarman/release-0.3.0 -fc95068 Merge branch 'dev' of https://github.com/codeborne/mockito-kotlin into codeborne-dev -5105493 Include more static Mockito methods -9238c4c add simpler capture() method with lambda -ce93783 Merge pull request #12 from codeborne/master -66e3be5 add simplified ArgumentCaptor support -1262c19 add deep stubs test -f6f142a add spy stubbing tests -0efd093 Added PR template -624b7c4 Merge pull request #10 from ViFork/master -334d471 add missing imports -76be5d0 add more missing Mockito methods, especially to be used with spies -40bf7c6 changed kotlin version to 1.0.0 -7cafd23 Merge pull request #7 from nhaarman/release-0.2.0 -8b93c66 Update Kotlin to rc-1036 -8488479 Merge pull request #6 from nhaarman/release-0.1.6 -1d9a588 Use smallest constructor -37ded6f Merge pull request #5 from nhaarman/release-0.1.5 -8157be6 Support Boolean -0665c25 Merge pull request #4 from nhaarman/release-0.1.4 -a873fa7 Support private classes -0cc78ba Merge pull request #3 from nhaarman/release-0.1.3 -65d6a5f Support object instances -c4bde0f Merge pull request #2 from nhaarman/release-0.1.2 -9856798 Proper license -9f1df2d Make sure 'eq' does not return null -90f6598 Merge branch 'master' into dev -b40c9ae Update README.md -ad964f7 Update README.md -92bb350 Cache Travis dependencies -886cad6 Proper version names -139d47c Proper version names -78505b7 Merge pull request #1 from nhaarman/release-0.1.1 -b5a4e87 Support enums -b113029 Upload to Bintray -2f9603a Initial commit From ce6bf8a2b5082da8255c4216fd39449904d3600b Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Sat, 21 Jan 2017 14:14:02 +0100 Subject: [PATCH 2/7] Update Dokka --- gradle/wrapper/gradle-wrapper.properties | 2 +- mockito-kotlin/build.gradle | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index cf73567f..bf0524de 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip diff --git a/mockito-kotlin/build.gradle b/mockito-kotlin/build.gradle index 4236a272..98d1ab90 100644 --- a/mockito-kotlin/build.gradle +++ b/mockito-kotlin/build.gradle @@ -13,7 +13,7 @@ buildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.9.11" + classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.9.13" classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3" classpath "com.github.dcendents:android-maven-gradle-plugin:1.5" } From 8e8fe3af463d470b07e67ce7bcaa9a1e27c54db4 Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Sat, 21 Jan 2017 14:17:51 +0100 Subject: [PATCH 3/7] Update Mockito to 2.6.5 --- mockito-kotlin/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mockito-kotlin/build.gradle b/mockito-kotlin/build.gradle index 98d1ab90..79fbdd3c 100644 --- a/mockito-kotlin/build.gradle +++ b/mockito-kotlin/build.gradle @@ -28,7 +28,7 @@ repositories { dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" - compile "org.mockito:mockito-core:2.4.5" + compile "org.mockito:mockito-core:2.6.5" /* Tests */ testCompile "junit:junit:4.12" From 1d39f6f56a7f61f570bf76d1f459e1f0151c7fff Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Sat, 21 Jan 2017 14:18:26 +0100 Subject: [PATCH 4/7] Update Expect.kt to 0.6.2 --- mockito-kotlin/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mockito-kotlin/build.gradle b/mockito-kotlin/build.gradle index 79fbdd3c..31799629 100644 --- a/mockito-kotlin/build.gradle +++ b/mockito-kotlin/build.gradle @@ -32,7 +32,7 @@ dependencies { /* Tests */ testCompile "junit:junit:4.12" - testCompile "com.nhaarman:expect.kt:0.6.0" + testCompile "com.nhaarman:expect.kt:0.6.2" } dokka { From a804d1e996017b609ae5df0a97253b4011c24ce1 Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Sat, 21 Jan 2017 14:26:47 +0100 Subject: [PATCH 5/7] Test with Mockito 1.1.0-beta-18 --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 36ba3954..3a873bef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,11 +7,11 @@ matrix: - jdk: oraclejdk7 env: TERM=dumb MOCK_MAKER=mock-maker-inline KOTLIN_VERSION=1.0.6 - jdk: oraclejdk7 - env: TERM=dumb MOCK_MAKER=mock-maker-inline KOTLIN_VERSION=1.1-M04 + env: TERM=dumb MOCK_MAKER=mock-maker-inline KOTLIN_VERSION=1.1.0-beta-18 - jdk: oraclejdk8 env: TERM=dumb KOTLIN_VERSION=1.0.6 - jdk: oraclejdk8 - env: TERM=dumb KOTLIN_VERSION=1.1-M04 + env: TERM=dumb KOTLIN_VERSION=1.1.0-beta-18 env: From f9f3abc42dd55a991ffa2699f8d0987a68947d90 Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Thu, 19 Jan 2017 22:20:06 +0100 Subject: [PATCH 6/7] Add regression tests in preparation for settings API --- .../src/test/kotlin/test/Classes.kt | 11 ++ .../src/test/kotlin/test/MockitoTest.kt | 165 +++++++++++++++++- 2 files changed, 174 insertions(+), 2 deletions(-) diff --git a/mockito-kotlin/src/test/kotlin/test/Classes.kt b/mockito-kotlin/src/test/kotlin/test/Classes.kt index 58dc9958..3476585c 100644 --- a/mockito-kotlin/src/test/kotlin/test/Classes.kt +++ b/mockito-kotlin/src/test/kotlin/test/Classes.kt @@ -60,6 +60,17 @@ interface Methods { fun stringResult(s: String): String fun nullableStringResult(): String? fun builderMethod(): Methods + + fun nonDefaultReturnType(): ExtraInterface +} + +interface ExtraInterface + +abstract class ThrowingConstructor { + + constructor() { + error("Error in constructor") + } } interface GenericMethods { diff --git a/mockito-kotlin/src/test/kotlin/test/MockitoTest.kt b/mockito-kotlin/src/test/kotlin/test/MockitoTest.kt index 65670f2c..ace45f92 100644 --- a/mockito-kotlin/src/test/kotlin/test/MockitoTest.kt +++ b/mockito-kotlin/src/test/kotlin/test/MockitoTest.kt @@ -5,8 +5,15 @@ import com.nhaarman.expect.expectErrorWithMessage import com.nhaarman.expect.fail import com.nhaarman.mockito_kotlin.* import org.junit.Test +import org.mockito.Mockito import org.mockito.exceptions.base.MockitoAssertionError +import org.mockito.exceptions.verification.WantedButNotInvoked +import org.mockito.listeners.InvocationListener +import org.mockito.mock.SerializableMode.BASIC import java.io.IOException +import java.io.PrintStream +import java.io.Serializable + /* * The MIT License @@ -244,8 +251,8 @@ class MockitoTest : TestBase() { val mock = mock() doAnswer { "Test" } - .whenever(mock) - .stringResult() + .whenever(mock) + .stringResult() expect(mock.stringResult()).toBe("Test") } @@ -479,6 +486,160 @@ class MockitoTest : TestBase() { } } + @Test + fun mock_withCustomName() { + /* Given */ + val mock = mock("myName") + + /* Expect */ + expectErrorWithMessage("myName.stringResult()") on { + verify(mock).stringResult() + } + } + + @Test + fun mock_withCustomDefaultAnswer() { + /* Given */ + val mock = mock(Mockito.RETURNS_SELF) + + /* When */ + val result = mock.builderMethod() + + /* Then */ + expect(result).toBe(mock) + } + + @Test + fun mock_withCustomDefaultAnswer_parameterName() { + /* Given */ + val mock = mock(defaultAnswer = Mockito.RETURNS_SELF) + + /* When */ + val result = mock.builderMethod() + + /* Then */ + expect(result).toBe(mock) + } + + @Test + fun mock_withSettings_extraInterfaces() { + /* Given */ + val mock = mock( + withSettings().extraInterfaces(ExtraInterface::class.java) + ) + + /* Then */ + expect(mock).toBeInstanceOf() + } + + @Test + fun mock_withSettings_name() { + /* Given */ + val mock = mock( + withSettings().name("myName") + ) + + /* When */ + expectErrorWithMessage("myName.stringResult()") on { + verify(mock).stringResult() + } + } + + @Test + fun mock_withSettings_defaultAnswer() { + /* Given */ + val mock = mock( + withSettings().defaultAnswer(Mockito.RETURNS_MOCKS) + ) + + /* When */ + val result = mock.nonDefaultReturnType() + + /* Then */ + expect(result).toNotBeNull() + } + + @Test + fun mock_withSettings_serializable() { + /* Given */ + val mock = mock( + withSettings().serializable() + ) + + /* Then */ + expect(mock).toBeInstanceOf() + } + + @Test + fun mock_withSettings_serializableMode() { + /* Given */ + val mock = mock( + withSettings().serializable(BASIC) + ) + + /* Then */ + expect(mock).toBeInstanceOf() + } + + @Test + fun mock_withSettings_verboseLogging() { + /* Given */ + val out = mock() + System.setOut(out) + val mock = mock( + withSettings().verboseLogging() + ) + + try { + /* When */ + verify(mock).stringResult() + fail("Expected an exception") + } catch(e: WantedButNotInvoked) { + /* Then */ + verify(out).println("methods.stringResult();") + } + } + + @Test + fun mock_withSettings_invocationListeners() { + /* Given */ + var bool = false + val mock = mock( + withSettings().invocationListeners(InvocationListener { bool = true }) + ) + + /* When */ + mock.stringResult() + + /* Then */ + expect(bool).toHold() + } + + @Test + fun mock_withSettings_stubOnly() { + /* Given */ + val mock = mock( + withSettings().stubOnly() + ) + + /* Expect */ + expectErrorWithMessage("is a stubOnly() mock") on { + + /* When */ + verify(mock).stringResult() + } + } + + @Test + fun mock_withSettings_useConstructor() { + /* Expect */ + expectErrorWithMessage("Unable to create mock instance of type") on { + mock( + withSettings().useConstructor() + ) + } + } + @Test fun stubbingTwiceWithArgumentMatchers() { /* When */ From 26a139fdffab393cd470a3ebaad00660edf89990 Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Thu, 19 Jan 2017 21:14:17 +0100 Subject: [PATCH 7/7] Enhance settings API --- .../com/nhaarman/mockito_kotlin/Mockito.kt | 109 +++++++-- .../src/test/kotlin/test/MockitoTest.kt | 216 +++++++++++++++++- 2 files changed, 305 insertions(+), 20 deletions(-) diff --git a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt index c64a5141..379d2186 100644 --- a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt +++ b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt @@ -26,16 +26,16 @@ package com.nhaarman.mockito_kotlin import com.nhaarman.mockito_kotlin.createinstance.createInstance -import org.mockito.InOrder -import org.mockito.MockSettings -import org.mockito.MockingDetails -import org.mockito.Mockito +import org.mockito.* import org.mockito.invocation.InvocationOnMock +import org.mockito.listeners.InvocationListener +import org.mockito.mock.SerializableMode import org.mockito.stubbing.Answer import org.mockito.stubbing.OngoingStubbing import org.mockito.stubbing.Stubber import org.mockito.verification.VerificationMode import org.mockito.verification.VerificationWithTimeout +import kotlin.DeprecationLevel.WARNING import kotlin.reflect.KClass fun after(millis: Long) = Mockito.after(millis) @@ -74,7 +74,7 @@ inline fun argForWhich(noinline predicate: T.() -> Boolean) = * * @param predicate A function that returns `true` when given [T] matches the predicate. */ -inline fun argWhere(noinline predicate: (T) -> Boolean) = argThat(predicate) +inline fun argWhere(noinline predicate: (T) -> Boolean) = argThat(predicate) /** * For usage with verification only. @@ -120,16 +120,70 @@ inline fun isA(): T = Mockito.isA(T::class.java) ?: createInst fun isNotNull(): T? = Mockito.isNotNull() fun isNull(): T? = Mockito.isNull() -inline fun mock(): T = Mockito.mock(T::class.java)!! -inline fun mock(defaultAnswer: Answer): T = Mockito.mock(T::class.java, defaultAnswer)!! +inline fun mock( + extraInterfaces: Array>? = null, + name: String? = null, + spiedInstance: Any? = null, + defaultAnswer: Answer? = null, + serializable: Boolean = false, + serializableMode: SerializableMode? = null, + verboseLogging: Boolean = false, + invocationListeners: Array? = null, + stubOnly: Boolean = false, + @Incubating useConstructor: Boolean = false, + @Incubating outerInstance: Any? = null +): T = Mockito.mock(T::class.java, withSettings( + extraInterfaces = extraInterfaces, + name = name, + spiedInstance = spiedInstance, + defaultAnswer = defaultAnswer, + serializable = serializable, + serializableMode = serializableMode, + verboseLogging = verboseLogging, + invocationListeners = invocationListeners, + stubOnly = stubOnly, + useConstructor = useConstructor, + outerInstance = outerInstance +))!! + +inline fun mock( + extraInterfaces: Array>? = null, + name: String? = null, + spiedInstance: Any? = null, + defaultAnswer: Answer? = null, + serializable: Boolean = false, + serializableMode: SerializableMode? = null, + verboseLogging: Boolean = false, + invocationListeners: Array? = null, + stubOnly: Boolean = false, + @Incubating useConstructor: Boolean = false, + @Incubating outerInstance: Any? = null, + stubbing: KStubbing.(T) -> Unit +): T = Mockito.mock(T::class.java, withSettings( + extraInterfaces = extraInterfaces, + name = name, + spiedInstance = spiedInstance, + defaultAnswer = defaultAnswer, + serializable = serializable, + serializableMode = serializableMode, + verboseLogging = verboseLogging, + invocationListeners = invocationListeners, + stubOnly = stubOnly, + useConstructor = useConstructor, + outerInstance = outerInstance +)).apply { + KStubbing(this).stubbing(this) +}!! + +@Deprecated("Use mock() with optional arguments instead.", ReplaceWith("mock(defaultAnswer = a)"), level = WARNING) +inline fun mock(a: Answer): T = mock(defaultAnswer = a) + +@Deprecated("Use mock() with optional arguments instead.", ReplaceWith("mock(name = s)"), level = WARNING) +inline fun mock(s: String): T = mock(name = s) + +@Suppress("DeprecatedCallableAddReplaceWith") +@Deprecated("Use mock() with optional arguments instead.", level = WARNING) inline fun mock(s: MockSettings): T = Mockito.mock(T::class.java, s)!! -inline fun mock(s: String): T = Mockito.mock(T::class.java, s)!! - -inline fun mock(stubbing: KStubbing.(T) -> Unit): T { - return mock().apply { - KStubbing(this).stubbing(this) - } -} class KStubbing(private val mock: T) { fun on(methodCall: R) = Mockito.`when`(methodCall) @@ -193,4 +247,29 @@ fun verifyNoMoreInteractions(vararg mocks: T) = Mockito.verifyNoMoreInteract fun verifyZeroInteractions(vararg mocks: Any) = Mockito.verifyZeroInteractions(*mocks) fun whenever(methodCall: T): OngoingStubbing = Mockito.`when`(methodCall)!! -fun withSettings(): MockSettings = Mockito.withSettings()!! + +fun withSettings( + extraInterfaces: Array>? = null, + name: String? = null, + spiedInstance: Any? = null, + defaultAnswer: Answer? = null, + serializable: Boolean = false, + serializableMode: SerializableMode? = null, + verboseLogging: Boolean = false, + invocationListeners: Array? = null, + stubOnly: Boolean = false, + @Incubating useConstructor: Boolean = false, + @Incubating outerInstance: Any? = null +): MockSettings = Mockito.withSettings().apply { + extraInterfaces?.let { extraInterfaces(*it.map { it.java }.toTypedArray()) } + name?.let { name(it) } + spiedInstance?.let { spiedInstance(it) } + defaultAnswer?.let { defaultAnswer(it) } + if (serializable) serializable() + serializableMode?.let { serializable(it) } + if (verboseLogging) verboseLogging() + invocationListeners?.let { invocationListeners(*it) } + if (stubOnly) stubOnly() + if (useConstructor) useConstructor() + outerInstance?.let { outerInstance(it) } +} diff --git a/mockito-kotlin/src/test/kotlin/test/MockitoTest.kt b/mockito-kotlin/src/test/kotlin/test/MockitoTest.kt index ace45f92..ce57d620 100644 --- a/mockito-kotlin/src/test/kotlin/test/MockitoTest.kt +++ b/mockito-kotlin/src/test/kotlin/test/MockitoTest.kt @@ -6,6 +6,7 @@ import com.nhaarman.expect.fail import com.nhaarman.mockito_kotlin.* import org.junit.Test import org.mockito.Mockito +import org.mockito.Mockito.RETURNS_MOCKS import org.mockito.exceptions.base.MockitoAssertionError import org.mockito.exceptions.verification.WantedButNotInvoked import org.mockito.listeners.InvocationListener @@ -40,6 +41,7 @@ import java.io.Serializable * THE SOFTWARE. */ +@Suppress("DEPRECATION") class MockitoTest : TestBase() { @Test @@ -415,7 +417,7 @@ class MockitoTest : TestBase() { @Test fun testMockStubbing_doThrow() { /* Given */ - val mock = mock { mock -> + val mock = mock { on { builderMethod() } doThrow IllegalArgumentException() } @@ -430,7 +432,7 @@ class MockitoTest : TestBase() { @Test fun testMockStubbing_doThrowClass() { /* Given */ - val mock = mock { mock -> + val mock = mock { on { builderMethod() } doThrow IllegalArgumentException::class } @@ -445,7 +447,7 @@ class MockitoTest : TestBase() { @Test fun testMockStubbing_doThrowVarargs() { /* Given */ - val mock = mock { mock -> + val mock = mock { on { builderMethod() }.doThrow(IllegalArgumentException(), UnsupportedOperationException()) } @@ -467,7 +469,7 @@ class MockitoTest : TestBase() { @Test fun testMockStubbing_doThrowClassVarargs() { /* Given */ - val mock = mock { mock -> + val mock = mock { on { builderMethod() }.doThrow(IllegalArgumentException::class, UnsupportedOperationException::class) } @@ -549,7 +551,7 @@ class MockitoTest : TestBase() { fun mock_withSettings_defaultAnswer() { /* Given */ val mock = mock( - withSettings().defaultAnswer(Mockito.RETURNS_MOCKS) + withSettings().defaultAnswer(RETURNS_MOCKS) ) /* When */ @@ -640,6 +642,210 @@ class MockitoTest : TestBase() { } } + @Test + fun mock_withSettingsAPI_extraInterfaces() { + /* Given */ + val mock = mock( + extraInterfaces = arrayOf(ExtraInterface::class) + ) + + /* Then */ + expect(mock).toBeInstanceOf() + } + + @Test + fun mock_withSettingsAPI_name() { + /* Given */ + val mock = mock(name = "myName") + + /* When */ + expectErrorWithMessage("myName.stringResult()") on { + verify(mock).stringResult() + } + } + + @Test + fun mock_withSettingsAPI_defaultAnswer() { + /* Given */ + val mock = mock(defaultAnswer = RETURNS_MOCKS) + + /* When */ + val result = mock.nonDefaultReturnType() + + /* Then */ + expect(result).toNotBeNull() + } + + @Test + fun mock_withSettingsAPI_serializable() { + /* Given */ + val mock = mock(serializable = true) + + /* Then */ + expect(mock).toBeInstanceOf() + } + + @Test + fun mock_withSettingsAPI_serializableMode() { + /* Given */ + val mock = mock(serializableMode = BASIC) + + /* Then */ + expect(mock).toBeInstanceOf() + } + + @Test + fun mock_withSettingsAPI_verboseLogging() { + /* Given */ + val out = mock() + System.setOut(out) + val mock = mock(verboseLogging = true) + + try { + /* When */ + verify(mock).stringResult() + fail("Expected an exception") + } catch(e: WantedButNotInvoked) { + /* Then */ + verify(out).println("methods.stringResult();") + } + } + + @Test + fun mock_withSettingsAPI_invocationListeners() { + /* Given */ + var bool = false + val mock = mock(invocationListeners = arrayOf(InvocationListener { bool = true })) + + /* When */ + mock.stringResult() + + /* Then */ + expect(bool).toHold() + } + + @Test + fun mock_withSettingsAPI_stubOnly() { + /* Given */ + val mock = mock(stubOnly = true) + + /* Expect */ + expectErrorWithMessage("is a stubOnly() mock") on { + + /* When */ + verify(mock).stringResult() + } + } + + @Test + fun mock_withSettingsAPI_useConstructor() { + /* Given */ + expectErrorWithMessage("Unable to create mock instance of type ") on { + mock(useConstructor = true) {} + } + } + + @Test + fun mockStubbing_withSettingsAPI_extraInterfaces() { + /* Given */ + val mock = mock(extraInterfaces = arrayOf(ExtraInterface::class)) {} + + /* Then */ + expect(mock).toBeInstanceOf() + } + + @Test + fun mockStubbing_withSettingsAPI_name() { + /* Given */ + val mock = mock(name = "myName") {} + + /* When */ + expectErrorWithMessage("myName.stringResult()") on { + verify(mock).stringResult() + } + } + + @Test + fun mockStubbing_withSettingsAPI_defaultAnswer() { + /* Given */ + val mock = mock(defaultAnswer = RETURNS_MOCKS) {} + + /* When */ + val result = mock.nonDefaultReturnType() + + /* Then */ + expect(result).toNotBeNull() + } + + @Test + fun mockStubbing_withSettingsAPI_serializable() { + /* Given */ + val mock = mock(serializable = true) {} + + /* Then */ + expect(mock).toBeInstanceOf() + } + + @Test + fun mockStubbing_withSettingsAPI_serializableMode() { + /* Given */ + val mock = mock(serializableMode = BASIC) {} + + /* Then */ + expect(mock).toBeInstanceOf() + } + + @Test + fun mockStubbing_withSettingsAPI_verboseLogging() { + /* Given */ + val out = mock() + System.setOut(out) + val mock = mock(verboseLogging = true) {} + + try { + /* When */ + verify(mock).stringResult() + fail("Expected an exception") + } catch(e: WantedButNotInvoked) { + /* Then */ + verify(out).println("methods.stringResult();") + } + } + + @Test + fun mockStubbing_withSettingsAPI_invocationListeners() { + /* Given */ + var bool = false + val mock = mock(invocationListeners = arrayOf(InvocationListener { bool = true })) {} + + /* When */ + mock.stringResult() + + /* Then */ + expect(bool).toHold() + } + + @Test + fun mockStubbing_withSettingsAPI_stubOnly() { + /* Given */ + val mock = mock(stubOnly = true) {} + + /* Expect */ + expectErrorWithMessage("is a stubOnly() mock") on { + + /* When */ + verify(mock).stringResult() + } + } + + @Test + fun mockStubbing_withSettingsAPI_useConstructor() { + /* Given */ + expectErrorWithMessage("Unable to create mock instance of type ") on { + mock(useConstructor = true) {} + } + } + @Test fun stubbingTwiceWithArgumentMatchers() { /* When */