Best Mockito-kotlin code snippet using org.mockito.kotlin.UseConstructor.parameterless
LiquidLegionsV2MillTest.kt
Source:LiquidLegionsV2MillTest.kt
...372 private val mockComputationLogEntries: SystemComputationLogEntriesCoroutineImplBase =373 mockService()374 private val mockComputationStats: ComputationStatsCoroutineImplBase = mockService()375 private val mockCryptoWorker: LiquidLegionsV2Encryption =376 mock(useConstructor = UseConstructor.parameterless()) {377 on { combineElGamalPublicKeys(any()) }.thenAnswer {378 val cryptoRequest: CombineElGamalPublicKeysRequest = it.getArgument(0)379 CombineElGamalPublicKeysResponse.newBuilder()380 .apply {381 elGamalKeysBuilder.apply {382 generator =383 ByteString.copyFromUtf8(384 cryptoRequest.elGamalKeysList385 .sortedBy { key -> key.generator.toStringUtf8() }386 .joinToString(separator = "_") { key -> key.generator.toStringUtf8() }387 )388 element =389 ByteString.copyFromUtf8(390 cryptoRequest.elGamalKeysList...
MockingTest.kt
Source:MockingTest.kt
1package test2import com.nhaarman.expect.expect3import com.nhaarman.expect.expectErrorWithMessage4import com.nhaarman.expect.fail5import org.mockito.kotlin.UseConstructor.Companion.parameterless6import org.mockito.kotlin.UseConstructor.Companion.withArguments7import org.mockito.kotlin.doReturn8import org.mockito.kotlin.mock9import org.mockito.kotlin.verify10import org.mockito.kotlin.whenever11import org.junit.Test12import org.mockito.Mockito13import org.mockito.exceptions.verification.WantedButNotInvoked14import org.mockito.invocation.DescribedInvocation15import org.mockito.kotlin.argumentCaptor16import org.mockito.listeners.InvocationListener17import org.mockito.mock.SerializableMode.BASIC18import java.io.PrintStream19import java.io.Serializable20import java.util.*21class MockingTest : TestBase() {22 private lateinit var propertyInterfaceVariable: MyInterface23 private lateinit var propertyClassVariable: MyClass24 @Test25 fun localInterfaceValue() {26 /* When */27 val instance: MyInterface = mock()28 /* Then */29 expect(instance).toNotBeNull()30 }31 @Test32 fun propertyInterfaceVariable() {33 /* When */34 propertyInterfaceVariable = mock()35 /* Then */36 expect(propertyInterfaceVariable).toNotBeNull()37 }38 @Test39 fun localClassValue() {40 /* When */41 val instance: MyClass = mock()42 /* Then */43 expect(instance).toNotBeNull()44 }45 @Test46 fun propertyClassVariable() {47 /* When */48 propertyClassVariable = mock()49 /* Then */50 expect(propertyClassVariable).toNotBeNull()51 }52 @Test53 fun untypedVariable() {54 /* When */55 val instance = mock<MyClass>()56 expect(instance).toNotBeNull()57 }58 @Test59 fun deepStubs() {60 val cal: Calendar = mock(defaultAnswer = Mockito.RETURNS_DEEP_STUBS)61 whenever(cal.time.time).thenReturn(123L)62 expect(cal.time.time).toBe(123L)63 }64 @Test65 fun testMockStubbing_lambda() {66 /* Given */67 val mock = mock<Open>() {68 on { stringResult() } doReturn "A"69 }70 /* When */71 val result = mock.stringResult()72 /* Then */73 expect(result).toBe("A")74 }75 @Test76 fun testMockStubbing_normalOverridesLambda() {77 /* Given */78 val mock = mock<Open>() {79 on { stringResult() }.doReturn("A")80 }81 whenever(mock.stringResult()).thenReturn("B")82 /* When */83 val result = mock.stringResult()84 /* Then */85 expect(result).toBe("B")86 }87 @Test88 fun mock_withCustomDefaultAnswer_parameterName() {89 /* Given */90 val mock = mock<Methods>(defaultAnswer = Mockito.RETURNS_SELF)91 /* When */92 val result = mock.builderMethod()93 /* Then */94 expect(result).toBe(mock)95 }96 @Test97 fun mock_withSettingsAPI_extraInterfaces() {98 /* Given */99 val mock = mock<Methods>(100 extraInterfaces = arrayOf(ExtraInterface::class)101 )102 /* Then */103 expect(mock).toBeInstanceOf<ExtraInterface>()104 }105 @Test106 fun mock_withSettingsAPI_name() {107 /* Given */108 val mock = mock<Methods>(name = "myName")109 /* When */110 expectErrorWithMessage("myName.stringResult()") on {111 verify(mock).stringResult()112 }113 }114 @Test115 fun mock_withSettingsAPI_defaultAnswer() {116 /* Given */117 val mock = mock<Methods>(defaultAnswer = Mockito.RETURNS_MOCKS)118 /* When */119 val result = mock.nonDefaultReturnType()120 /* Then */121 expect(result).toNotBeNull()122 }123 @Test124 fun mock_withSettingsAPI_serializable() {125 /* Given */126 val mock = mock<Methods>(serializable = true)127 /* Then */128 expect(mock).toBeInstanceOf<Serializable>()129 }130 @Test131 fun mock_withSettingsAPI_serializableMode() {132 /* Given */133 val mock = mock<Methods>(serializableMode = BASIC)134 /* Then */135 expect(mock).toBeInstanceOf<Serializable>()136 }137 @Test138 fun mock_withSettingsAPI_verboseLogging() {139 /* Given */140 val out = mock<PrintStream>()141 System.setOut(out)142 val mock = mock<Methods>(verboseLogging = true)143 try {144 /* When */145 verify(mock).stringResult()146 fail("Expected an exception")147 } catch (e: WantedButNotInvoked) {148 /* Then */149 argumentCaptor<DescribedInvocation>().apply {150 verify(out).println(capture())151 expect(lastValue.toString()).toBe("methods.stringResult();")152 }153 }154 }155 @Test156 fun mock_withSettingsAPI_invocationListeners() {157 /* Given */158 var bool = false159 val mock = mock<Methods>(invocationListeners = arrayOf(InvocationListener { bool = true }))160 /* When */161 mock.stringResult()162 /* Then */163 expect(bool).toHold()164 }165 @Test166 fun mock_withSettingsAPI_stubOnly() {167 /* Given */168 val mock = mock<Methods>(stubOnly = true)169 /* Expect */170 expectErrorWithMessage("is a stubOnly() mock") on {171 /* When */172 verify(mock).stringResult()173 }174 }175 @Test176 fun mock_withSettingsAPI_useConstructor() {177 /* Given */178 expectErrorWithMessage("Unable to create mock instance of type ") on {179 mock<ThrowingConstructor>(useConstructor = parameterless()) {}180 }181 }182 @Test183 fun mock_withSettingsAPI_useConstructorWithArguments_failing() {184 /* Given */185 expectErrorWithMessage("Unable to create mock instance of type ") on {186 mock<ThrowingConstructorWithArgument>(useConstructor = withArguments("Test")) {}187 }188 }189 @Test190 fun mock_withSettingsAPI_useConstructorWithArguments() {191 /* When */192 val result = mock<NonThrowingConstructorWithArgument>(useConstructor = withArguments("Test")) {}193 /* Then */194 expect(result).toNotBeNull()195 }196 @Test197 fun mockStubbing_withSettingsAPI_extraInterfaces() {198 /* Given */199 val mock = mock<Methods>(extraInterfaces = arrayOf(ExtraInterface::class)) {}200 /* Then */201 expect(mock).toBeInstanceOf<ExtraInterface>()202 }203 @Test204 fun mockStubbing_withSettingsAPI_name() {205 /* Given */206 val mock = mock<Methods>(name = "myName") {}207 /* When */208 expectErrorWithMessage("myName.stringResult()") on {209 verify(mock).stringResult()210 }211 }212 @Test213 fun mockStubbing_withSettingsAPIAndStubbing_name() {214 /* Given */215 val mock = mock<Methods>(name = "myName") {216 on { nullableStringResult() } doReturn "foo"217 }218 /* When */219 val result = mock.nullableStringResult()220 /* Then */221 expect(result).toBe("foo")222 }223 @Test224 fun mockStubbing_withSettingsAPI_defaultAnswer() {225 /* Given */226 val mock = mock<Methods>(defaultAnswer = Mockito.RETURNS_MOCKS) {}227 /* When */228 val result = mock.nonDefaultReturnType()229 /* Then */230 expect(result).toNotBeNull()231 }232 @Test233 fun mockStubbing_withSettingsAPI_serializable() {234 /* Given */235 val mock = mock<Methods>(serializable = true) {}236 /* Then */237 expect(mock).toBeInstanceOf<Serializable>()238 }239 @Test240 fun mockStubbing_withSettingsAPI_serializableMode() {241 /* Given */242 val mock = mock<Methods>(serializableMode = BASIC) {}243 /* Then */244 expect(mock).toBeInstanceOf<Serializable>()245 }246 @Test247 fun mockStubbing_withSettingsAPI_verboseLogging() {248 /* Given */249 val out = mock<PrintStream>()250 System.setOut(out)251 val mock = mock<Methods>(verboseLogging = true) {}252 try {253 /* When */254 verify(mock).stringResult()255 fail("Expected an exception")256 } catch (e: WantedButNotInvoked) {257 /* Then */258 argumentCaptor<DescribedInvocation>().apply {259 verify(out).println(capture())260 expect(lastValue.toString()).toBe("methods.stringResult();")261 }262 }263 }264 @Test265 fun mockStubbing_withSettingsAPI_invocationListeners() {266 /* Given */267 var bool = false268 val mock = mock<Methods>(invocationListeners = arrayOf(InvocationListener { bool = true })) {}269 /* When */270 mock.stringResult()271 /* Then */272 expect(bool).toHold()273 }274 @Test275 fun mockStubbing_withSettingsAPI_stubOnly() {276 /* Given */277 val mock = mock<Methods>(stubOnly = true) {}278 /* Expect */279 expectErrorWithMessage("is a stubOnly() mock") on {280 /* When */281 verify(mock).stringResult()282 }283 }284 @Test285 fun mockStubbing_withSettingsAPI_useConstructor() {286 /* Given */287 expectErrorWithMessage("Unable to create mock instance of type ") on {288 mock<ThrowingConstructor>(useConstructor = parameterless()) {}289 }290 }291 private interface MyInterface292 private open class MyClass293}...
DevelopersMessageListenerPactTest.kt
Source:DevelopersMessageListenerPactTest.kt
...18import de.dkutzer.buggy.planning.entity.*19import mu.KotlinLogging20import org.junit.jupiter.api.Test21import org.junit.jupiter.api.extension.ExtendWith22import org.mockito.kotlin.UseConstructor.Companion.parameterless23import org.mockito.kotlin.UseConstructor.Companion.withArguments24import org.junit.jupiter.api.BeforeEach25import org.junit.jupiter.api.extension.Extensions26import org.mockito.Mockito.mock27import org.mockito.Mockito.verify28import org.mockito.kotlin.*29import org.springframework.http.MediaType30import java.util.*31private val logger = KotlinLogging.logger {}32@PactTestFor(providerName = "dkutzer-msdemo-buggy-developers-messaging")33@Extensions(34 ExtendWith(PactConsumerTestExt::class)35)36@PactDirectory("../pacts")...
Mocking.kt
Source:Mocking.kt
...21/** Creates a mock for a gRPC coroutine service [T], allowing for immediate stubbing. */22inline fun <reified T : AbstractCoroutineServerImpl> mockService(23 stubbing: KStubbing<T>.(T) -> Unit24): T =25 mock(useConstructor = UseConstructor.parameterless()) { mock ->26 on { context }.thenCallRealMethod()27 stubbing(mock)28 }29/** Creates a mock for a gRPC coroutine service [T]. */30inline fun <reified T : AbstractCoroutineServerImpl> mockService(): T =31 mock(useConstructor = UseConstructor.parameterless()) { on { context }.thenCallRealMethod() }...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!