### Publish Mockito-Kotlin to Maven Local Source: https://github.com/mockito/mockito-kotlin/blob/main/README.md Run this Gradle command to install the Mockito-Kotlin maven artifacts into your local repository. ```bash ./gradlew publishToMavenLocal ``` -------------------------------- ### Basic Mockito-Kotlin Test Example Source: https://github.com/mockito/mockito-kotlin/blob/main/README.md A typical test using Mockito-Kotlin, demonstrating mocking a class, stubbing a method, and verifying a call. ```kotlin @Test fun doAction_doesSomething(){ /* Given */ val mock = mock { on { getText() } doReturn "text" } val classUnderTest = ClassUnderTest(mock) /* When */ classUnderTest.doAction() /* Then */ verify(mock).doSomething(any()) } ``` -------------------------------- ### Scoped Mock for Object with JUnit Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Manage the lifecycle of a mocked object within a JUnit test class using `@Before` and `@After` annotations to ensure proper setup and cleanup. ```kotlin lateinit var myObjectMock: MockedObject @Before fun setUp() { myObjectMock = mockObject(MyObject) } @After fun tearDown() { myObjectMock.close() } ``` -------------------------------- ### Mocking with Mockito-Kotlin vs. MockK Source: https://github.com/mockito/mockito-kotlin/wiki/Comparison-to-MockK Demonstrates the basic syntax for creating mocks in Mockito-Kotlin and MockK. ```kotlin mock() ``` ```kotlin mockk() ``` -------------------------------- ### Build Mockito-Kotlin Project Source: https://github.com/mockito/mockito-kotlin/blob/main/README.md Run this Gradle command to build and test the Mockito-Kotlin project. ```bash ./gradlew build ``` -------------------------------- ### Run Mockito-Kotlin Test Suite Source: https://github.com/mockito/mockito-kotlin/blob/main/README.md Execute this Gradle command to run the test suite for Mockito-Kotlin. ```bash ./gradlew check ``` -------------------------------- ### Verification with Mockito-Kotlin vs. MockK Source: https://github.com/mockito/mockito-kotlin/wiki/Comparison-to-MockK Illustrates the syntax for verifying method calls on mocks in Mockito-Kotlin and MockK. ```kotlin verify(mock).foo() ``` ```kotlin verify { mock.foo() } ``` -------------------------------- ### Run Benchmarks with Gradle Source: https://github.com/mockito/mockito-kotlin/blob/main/benchmarks/README.md Execute the benchmarks using Gradle. The `--rerun` flag ensures tasks are re-executed. Customize iteration and warmup counts using system properties. ```bash ./gradlew :benchmarks:test --rerun ``` ```bash ./gradlew :benchmarks:test --rerun \ -Dbenchmark.iterations=5000 \ -Dbenchmark.warmup=500 ``` -------------------------------- ### Mocking Extension Functions Source: https://github.com/mockito/mockito-kotlin/wiki/Comparison-to-MockK Shows how to mock extension functions using Mockito-Kotlin and MockK. ```kotlin mockExtensionFun(MyObject::extensionFun) ``` ```kotlin mockkStatic(MyObject::extensionFun) ``` -------------------------------- ### Create Mock Instance Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Use the `mock()` function to create mock instances. The type can often be inferred automatically, or specified explicitly using generic type arguments. ```kotlin val myMock: MyClass = mock() val myMock = mock() ``` -------------------------------- ### Mocking Constructors Source: https://github.com/mockito/mockito-kotlin/wiki/Comparison-to-MockK Demonstrates the syntax for mocking class constructors in Mockito-Kotlin and MockK. ```kotlin mockConstruction ``` ```kotlin mockkConstructor(MyClass::class) ``` -------------------------------- ### Stubbing with Mockito-Kotlin vs. MockK Source: https://github.com/mockito/mockito-kotlin/wiki/Comparison-to-MockK Shows how to stub method calls on mocks using Mockito-Kotlin and MockK. ```kotlin whenever(mock.foo()).thenReturn(x) or mock { on { foo() } doReturn x } ``` ```kotlin every { mock.foo() } return x ``` -------------------------------- ### DSL Stubbing with KStubbing Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Utilize DSL stubbing syntax within the `mock()` function for a more fluent way to define stubbing behavior using lambda expressions. ```kotlin val myMock = mock { on { stringValue() } doReturn "test" } ``` -------------------------------- ### Gradle Version Catalog for Mockito Source: https://github.com/mockito/mockito-kotlin/wiki/Android-Support Define versions and libraries for Mockito and Mockito-Kotlin in your Gradle version catalog. ```gradle [versions] mockito = "5.23.0" mockitoKotlin = "6.3.0" [libraries] mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" } mockito-android = { module = "org.mockito:mockito-android", version.ref = "mockito" } mockito-kotlin = { module = "org.mockito.kotlin:mockito-kotlin", version.ref = "mockitoKotlin" } ``` -------------------------------- ### Create and Push Annotated Git Tag Source: https://github.com/mockito/mockito-kotlin/blob/main/RELEASING.md Use this command to create an annotated Git tag for a release. Push the tag to the remote repository to trigger a release to Maven Central. Ensure the tag message clearly indicates the release version. ```bash git tag -a -m "Release 3.4.5" 3.4.5 git push origin 3.4.5 ``` -------------------------------- ### Stubbing Existing Mock with KStubbing Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Apply DSL stubbing to an already created mock instance using the `stub` extension function. ```kotlin myMock.stub { on { stringValue() } doReturn "test" } ``` -------------------------------- ### Verify Basic Method Call Source: https://github.com/mockito/mockito-kotlin/wiki/Verifying-&-Capturing Use this to verify that a specific method was called on a mock object with given arguments. ```kotlin verify(myClass).doSomething("test") ``` -------------------------------- ### Mocking Objects/Companion Objects Source: https://github.com/mockito/mockito-kotlin/wiki/Comparison-to-MockK Compares the methods for mocking top-level objects or companion objects in Mockito-Kotlin and MockK. ```kotlin mockObject(MyObject) ``` ```kotlin mockkObject(MyObject) ``` -------------------------------- ### Troubleshooting Mockito-Android Native Library Loading Source: https://github.com/mockito/mockito-kotlin/wiki/Android-Support This log output indicates a failure to load the `libdexmakerjvmtiagent.so` library, often resolved by ensuring `android:extractNativeLibs="true"` is set in the `AndroidManifest.xml`. ```log D nativeloader: Load libdexmakerjvmtiagent.so using class loader ns clns-9 (caller=): dlopen failed: library "libdexmakerjvmtiagent.so" not found W e.myapplication: Agent attach failed (result=1) : Unable to dlopen libdexmakerjvmtiagent.so: dlopen failed: library "libdexmakerjvmtiagent.so" not found ... E MockMakerMultiplexer: Could not init mockmaker com.android.dx.mockito.inline.InlineDexmakerMockMaker E MockMakerMultiplexer: java.lang.reflect.InvocationTargetException ... E MockMakerMultiplexer: Caused by: java.io.IOException: Unable to dlopen libdexmakerjvmtiagent.so: dlopen failed: library "libdexmakerjvmtiagent.so" not found ``` -------------------------------- ### Verify with Any Argument Matcher Source: https://github.com/mockito/mockito-kotlin/wiki/Verifying-&-Capturing Verify a method call where any argument of the expected type is acceptable. No type needs to be specified for `any()`. ```kotlin verify(myClass).doSomething(any()) ``` -------------------------------- ### DSL Stubbing for Mocked Object Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Apply stubbing to a mocked object using the `KStubbing` DSL syntax directly within the `mockObject` call. ```kotlin mockObject(MyObject) { on { foo(any()) } doReturn /* ... */ }.use { /* ... */ } ``` -------------------------------- ### Verify with ArgForWhich Natural Language Matcher Source: https://github.com/mockito/mockito-kotlin/wiki/Verifying-&-Capturing Use `argForWhich` for a more readable way to specify custom argument matching conditions using a lambda. ```kotlin verify(myClass).setItems(argForWhich { size == 2 } ) ``` -------------------------------- ### Test Mockito-Kotlin with Specific Kotlin Version Source: https://github.com/mockito/mockito-kotlin/blob/main/README.md Use this Gradle command to test Mockito-Kotlin with a different Kotlin version locally. Replace '1.2.3' with the desired version. ```bash ./gradlew check -PtestKotlinVersion=1.2.3 ``` -------------------------------- ### Verify Property Setting Source: https://github.com/mockito/mockito-kotlin/wiki/Verifying-&-Capturing Verify that a property was set on a mock object. You can directly verify the assignment or use `whenever` with `any()` for more complex scenarios. ```kotlin interface Foo { var bar : String } @Test fun test() { val foo = mock() foo.bar = "test" verify(foo).bar = "test" // or you can use a setter argument doAnswer { it.getArgument(0) }.whenever(foo).bar = any() } ``` -------------------------------- ### Add Mockito-Kotlin Dependency to Gradle (Kotlin DSL) Source: https://github.com/mockito/mockito-kotlin/blob/main/README.md Add this dependency to your build.gradle.kts file for Gradle users using Kotlin DSL. Replace 'x.x.x' with the latest version. ```kotlin testImplementation("org.mockito.kotlin:mockito-kotlin:x.x.x") ``` -------------------------------- ### Add Mockito-Kotlin Dependency to Gradle Source: https://github.com/mockito/mockito-kotlin/blob/main/README.md Add this dependency to your build.gradle file for Gradle users. Replace 'x.x.x' with the latest version. ```groovy testImplementation "org.mockito.kotlin:mockito-kotlin:x.x.x" ``` -------------------------------- ### InOrder Verification with Lambda Source: https://github.com/mockito/mockito-kotlin/wiki/Verifying-&-Capturing Perform in-order verification of method calls on multiple mock objects using a lambda for concise syntax. ```kotlin val a = ... val b = ... inOrder(a, b) { verify(a).doSomething() verify(b).doSomething() } ``` -------------------------------- ### Add Mockito-Kotlin Dependency Source: https://github.com/mockito/mockito-kotlin/wiki/Home Include this dependency in your project's build file for testing with Mockito-Kotlin. ```kotlin dependencies { testImplementation("org.mockito.kotlin:mockito-kotlin:6.3.0") } ``` -------------------------------- ### Pass Mock as Parameter Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing A mock instance can be passed directly as a parameter to a method. ```kotlin myClass.test(mock()) ``` -------------------------------- ### Mock Extension Function with Matchers Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing When using matchers with mocked extension functions, all arguments, including the receiver, must use matchers. ```kotlin mockExtensionFun(String::hasPrefix).use { whenever(any().hasPrefix(eq("foo"))).thenReturn(true) println("bar".hasPrefix("foo")) // "true" } ``` -------------------------------- ### AndroidManifest.xml Configuration for Mockito-Android Source: https://github.com/mockito/mockito-kotlin/wiki/Android-Support Ensure native libraries are extracted by setting `android:extractNativeLibs="true"` in the application tag for Mockito-Android compatibility. ```xml ``` -------------------------------- ### Mock Class Construction Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Mock the construction of class instances using `mockConstruction`. The returned handle must be closed to avoid leaking mocking state. ```kotlin mockConstruction().use { val m = MyClass() // `m` is a mock } ``` -------------------------------- ### Verify with Subclass Argument Matcher Source: https://github.com/mockito/mockito-kotlin/wiki/Verifying-&-Capturing Use `any()` to restrict the `any` matcher to a specific subclass when verifying method calls. ```kotlin verify(myClass).doSomething(any()) ``` -------------------------------- ### Gradle Dependencies for Mockito-Kotlin Source: https://github.com/mockito/mockito-kotlin/wiki/Android-Support Add Mockito-Kotlin for JVM tests and Mockito-Android for instrumented tests to your app's build file. ```gradle dependencies { testImplementation(libs.mockito.kotlin) androidTestImplementation(libs.mockito.android) } ``` -------------------------------- ### Classic Stubbing with whenever Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Use `whenever` for stubbing method calls, as `when` is a reserved keyword in Kotlin. This is the standard Mockito-Kotlin approach for basic stubbing. ```kotlin whenever(mock.stringValue()).thenReturn("test") ``` -------------------------------- ### Verify with Check for Assertions Source: https://github.com/mockito/mockito-kotlin/wiki/Verifying-&-Capturing Use `check` to perform assertions directly on the received argument within the verification process. Ensure the assertions throw an error if they fail. ```kotlin verify(myClass).setItems(check { assertThat(it.size, is(2)) assertThat(it[0], is("test")) }) ``` -------------------------------- ### Verify with ArgThat Custom Matcher Source: https://github.com/mockito/mockito-kotlin/wiki/Verifying-&-Capturing Use `argThat` to verify method calls with arguments that satisfy a custom boolean condition. The condition is defined as a lambda. ```kotlin verify(myClass).setItems(argThat { size == 2 } ) ``` -------------------------------- ### Stubbing During Construction Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Apply stubbing to mocks created via `mockConstruction` by providing a `MockInitializer` lambda that configures the mock and its context. ```kotlin mockConstruction { mock, context -> println(context.arguments) whenever(mock.foo()).thenReturn(42) }.use { /* ... */ } ``` -------------------------------- ### Mock Companion Object Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Mock companion objects using `mockObject`. The returned handle implements `AutoCloseable` and must be closed to prevent state leakage. ```kotlin mockObject(MyClass.Companion).use { /* ... */ } ``` -------------------------------- ### Mock Singleton Object Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Mock singleton objects using `mockObject`. The returned handle implements `AutoCloseable` and must be closed to prevent state leakage. ```kotlin mockObject(MyObject).use { /* ... */ } ``` -------------------------------- ### Mock Extension Function Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Mock top-level extension functions using `mockExtensionFun`. The returned handle must be closed to prevent state leakage. ```kotlin mockExtensionFun(String::isHello).use { whenever("test".isHello()).thenReturn(true) println("test".isHello()) // "true" } ``` -------------------------------- ### Stubbing Mocked Object Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Stub methods on a mocked object using `whenever` and verify interactions using `verify`, similar to regular mocks. ```kotlin whenever(MyObject.foo(any())).thenReturn( /* ... */ ) verify(MyObject).foo(any()) ``` -------------------------------- ### Verify Suspend Functions with Mockito-Kotlin Source: https://github.com/mockito/mockito-kotlin/wiki/Coroutines Use `verifyBlocking` to verify calls to suspend functions. This ensures proper handling of suspendable operations during verification. ```kotlin verifyBlocking(myMock) { suspendFoo() } ``` -------------------------------- ### Mock Static Methods (Java) Source: https://github.com/mockito/mockito-kotlin/wiki/Mocking-&-Stubbing Mock static methods from Java classes using `mockStatic`. The returned handle must be closed to prevent state leakage. ```kotlin mockStatic().use { whenever(MyJavaClass.foo()).thenReturn( /* ... */ ) } ``` -------------------------------- ### Capture Arguments with ArgumentCaptor Source: https://github.com/mockito/mockito-kotlin/wiki/Verifying-&-Capturing Use `argumentCaptor` to capture argument values passed to a mocked method for subsequent assertions. This is useful for verifying multiple calls or complex argument states. ```kotlin val myCaptor = argumentCaptor() verify(myClass, times(2)).setItems(myCaptor.capture()) assertEquals(2, myCaptor.allValues.size) assertEquals("test", myCaptor.firstValue) ``` -------------------------------- ### Stub Suspend Functions with Mockito-Kotlin Source: https://github.com/mockito/mockito-kotlin/wiki/Coroutines Use `whenever` or `KStubbing` to stub suspend functions directly. No explicit coroutine context like `runTest` is required. ```kotlin whenever { myMock.suspendFoo() } doReturn value ``` ```kotlin mock { on { suspendFoo() } doReturn value } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.