package test import com.nhaarman.expect.expect import com.nhaarman.expect.expectErrorWithMessage import com.nhaarman.expect.fail import org.mockito.kotlin.UseConstructor.Companion.parameterless import org.mockito.kotlin.UseConstructor.Companion.withArguments import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.mockito.kotlin.whenever import org.junit.Test import org.mockito.Mockito import org.mockito.exceptions.verification.WantedButNotInvoked import org.mockito.invocation.DescribedInvocation import org.mockito.kotlin.argumentCaptor import org.mockito.listeners.InvocationListener import org.mockito.mock.SerializableMode.BASIC import java.io.PrintStream import java.io.Serializable import java.util.* class MockingTest : TestBase() { private lateinit var propertyInterfaceVariable: MyInterface private lateinit var propertyClassVariable: MyClass @Test fun localInterfaceValue() { /* When */ val instance: MyInterface = mock() /* Then */ expect(instance).toNotBeNull() } @Test fun propertyInterfaceVariable() { /* When */ propertyInterfaceVariable = mock() /* Then */ expect(propertyInterfaceVariable).toNotBeNull() } @Test fun localClassValue() { /* When */ val instance: MyClass = mock() /* Then */ expect(instance).toNotBeNull() } @Test fun propertyClassVariable() { /* When */ propertyClassVariable = mock() /* Then */ expect(propertyClassVariable).toNotBeNull() } @Test fun untypedVariable() { /* When */ val instance = mock() expect(instance).toNotBeNull() } @Test fun deepStubs() { val cal: Calendar = mock(defaultAnswer = Mockito.RETURNS_DEEP_STUBS) whenever(cal.time.time).thenReturn(123L) expect(cal.time.time).toBe(123L) } @Test fun testMockStubbing_lambda() { /* Given */ val mock = mock() { on { stringResult() } doReturn "A" } /* When */ val result = mock.stringResult() /* Then */ expect(result).toBe("A") } @Test fun testMockStubbing_normalOverridesLambda() { /* Given */ val mock = mock() { on { stringResult() }.doReturn("A") } whenever(mock.stringResult()).thenReturn("B") /* When */ val result = mock.stringResult() /* Then */ expect(result).toBe("B") } @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_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 = Mockito.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 */ argumentCaptor().apply { verify(out).println(capture()) expect(lastValue.toString()).toBe("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 = parameterless()) {} } } @Test fun mock_withSettingsAPI_useConstructorWithArguments_failing() { /* Given */ expectErrorWithMessage("Unable to create mock instance of type ") on { mock(useConstructor = withArguments("Test")) {} } } @Test fun mock_withSettingsAPI_useConstructorWithArguments() { /* When */ val result = mock(useConstructor = withArguments("Test")) {} /* Then */ expect(result).toNotBeNull() } @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_withSettingsAPIAndStubbing_name() { /* Given */ val mock = mock(name = "myName") { on { nullableStringResult() } doReturn "foo" } /* When */ val result = mock.nullableStringResult() /* Then */ expect(result).toBe("foo") } @Test fun mockStubbing_withSettingsAPI_defaultAnswer() { /* Given */ val mock = mock(defaultAnswer = Mockito.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 */ argumentCaptor().apply { verify(out).println(capture()) expect(lastValue.toString()).toBe("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 = parameterless()) {} } } private interface MyInterface private open class MyClass }