Add regression tests in preparation for settings API
diff --git a/mockito-kotlin/src/test/kotlin/test/Classes.kt b/mockito-kotlin/src/test/kotlin/test/Classes.kt
index 58dc995..3476585 100644
--- a/mockito-kotlin/src/test/kotlin/test/Classes.kt
+++ b/mockito-kotlin/src/test/kotlin/test/Classes.kt
@@ -60,6 +60,17 @@
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<T> {
diff --git a/mockito-kotlin/src/test/kotlin/test/MockitoTest.kt b/mockito-kotlin/src/test/kotlin/test/MockitoTest.kt
index 65670f2..ace45f9 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.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 @@
val mock = mock<Methods>()
doAnswer { "Test" }
- .whenever(mock)
- .stringResult()
+ .whenever(mock)
+ .stringResult()
expect(mock.stringResult()).toBe("Test")
}
@@ -480,6 +487,160 @@
}
@Test
+ fun mock_withCustomName() {
+ /* Given */
+ val mock = mock<Methods>("myName")
+
+ /* Expect */
+ expectErrorWithMessage("myName.stringResult()") on {
+ verify(mock).stringResult()
+ }
+ }
+
+ @Test
+ fun mock_withCustomDefaultAnswer() {
+ /* Given */
+ val mock = mock<Methods>(Mockito.RETURNS_SELF)
+
+ /* When */
+ val result = mock.builderMethod()
+
+ /* Then */
+ expect(result).toBe(mock)
+ }
+
+ @Test
+ fun mock_withCustomDefaultAnswer_parameterName() {
+ /* Given */
+ val mock = mock<Methods>(defaultAnswer = Mockito.RETURNS_SELF)
+
+ /* When */
+ val result = mock.builderMethod()
+
+ /* Then */
+ expect(result).toBe(mock)
+ }
+
+ @Test
+ fun mock_withSettings_extraInterfaces() {
+ /* Given */
+ val mock = mock<Methods>(
+ withSettings().extraInterfaces(ExtraInterface::class.java)
+ )
+
+ /* Then */
+ expect(mock).toBeInstanceOf<ExtraInterface>()
+ }
+
+ @Test
+ fun mock_withSettings_name() {
+ /* Given */
+ val mock = mock<Methods>(
+ withSettings().name("myName")
+ )
+
+ /* When */
+ expectErrorWithMessage("myName.stringResult()") on {
+ verify(mock).stringResult()
+ }
+ }
+
+ @Test
+ fun mock_withSettings_defaultAnswer() {
+ /* Given */
+ val mock = mock<Methods>(
+ withSettings().defaultAnswer(Mockito.RETURNS_MOCKS)
+ )
+
+ /* When */
+ val result = mock.nonDefaultReturnType()
+
+ /* Then */
+ expect(result).toNotBeNull()
+ }
+
+ @Test
+ fun mock_withSettings_serializable() {
+ /* Given */
+ val mock = mock<Methods>(
+ withSettings().serializable()
+ )
+
+ /* Then */
+ expect(mock).toBeInstanceOf<Serializable>()
+ }
+
+ @Test
+ fun mock_withSettings_serializableMode() {
+ /* Given */
+ val mock = mock<Methods>(
+ withSettings().serializable(BASIC)
+ )
+
+ /* Then */
+ expect(mock).toBeInstanceOf<Serializable>()
+ }
+
+ @Test
+ fun mock_withSettings_verboseLogging() {
+ /* Given */
+ val out = mock<PrintStream>()
+ System.setOut(out)
+ val mock = mock<Methods>(
+ 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<Methods>(
+ withSettings().invocationListeners(InvocationListener { bool = true })
+ )
+
+ /* When */
+ mock.stringResult()
+
+ /* Then */
+ expect(bool).toHold()
+ }
+
+ @Test
+ fun mock_withSettings_stubOnly() {
+ /* Given */
+ val mock = mock<Methods>(
+ 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<ThrowingConstructor>(
+ withSettings().useConstructor()
+ )
+ }
+ }
+
+ @Test
fun stubbingTwiceWithArgumentMatchers() {
/* When */
val mock = mock<Methods> {