Skip to content
Antonio Martinović edited this page Mar 5, 2021 · 20 revisions

MockK development wiki

Keboard

Fresh development setup

  • Clone repo by executing git clone git@github.com:mockk/mockk.git
  • [Optional] Install Android SDK and Android NDK
  • Build project by executing ./gradlew build or use IntelliJ IDEA integrated tools to build and test

To run all tests, execute:

./gradlew test

or to run a specific test:

./gradlew :mock-jvm:test --tests "io.mockk.gh*"

This will run all tests of project mock-jvm that are in package io.mockk.gh For more rules you can reference Gradle docs

Troubleshooting

With few known approaches troubleshooting may be a lot easier.

Transformation dumping

To see results of class transformation(and untransformation) add following system variable:

java  -Dio.mockk.classdump.path=some-directory ...

After running your code transformed class files should appear in some-directory.

Enabling trace logs

Put logback-test.xml in your resources(test-resources) folder:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <statusListener class="ch.qos.logback.core.status.NopStatusListener"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>

    <root level="trace">
        <appender-ref ref="STDOUT"/>
    </root>

</configuration>

Accessing information in IDEA Debugger

Mock to stub map

((MockKGateway.implementation() as JvmMockKGateway).stubRepo.stubs as JvmWeakConcurrentMap).map

This particular gives access to all stubs, but you may access different other objects from JvmMockKGateway

Proxy maker object

(((MockKGateway.implementation() as JvmMockKGateway).mockFactory as JvmMockFactory).proxyMaker as io.mockk.proxy.jvm.ProxyMaker)

Dispatcher object

Class.forName("io.mockk.proxy.jvm.dispatcher.JvmMockKDispatcher").getDeclaredFields()[0].apply{ isAccessible = true }.get(Class.forName("io.mockk.proxy.jvm.dispatcher.JvmMockKDispatcher"))

Handlers in watch

Self call eliminator object

(Class.forName("io.mockk.proxy.jvm.advice.SelfCallEliminator").declaredFields[0].apply { isAccessible = true }.get(Class.forName("io.mockk.proxy.jvm.advice.SelfCallEliminator")) as ThreadLocal<*>).get()

Topics

  • Basics
  • Code transformation
    • ByteBuddy inlining
    • ByteBuddy subclassing
    • DEX Maker inlining
    • DEX Maker subclassing
  • Call recording
    • Recording states
    • Signatures
    • Autohinting
    • Chain detection
  • Verification
    • verify
    • verifyAll
    • verifyOrder
    • verifySequence
    • verify(timeout=X)

Main modules

JVM

  • io.mockk:mockk (mockk/jvm)
    JVM MockK implementation
    • io.mockk:mockk-dsl-jvm (dsk/jvm)
      JVM support functions for DSL
    • io.mockk:mockk-common (mockk/common)
      common MockK implementation
      • io.mockk:mockk-dsl (dsl/common)
        common MockK DSL interface
    • io.mockk:mockk-agent-jvm (agennt/jvm)
      call interception for JVM
      • io.mockk:mockk-agent-common (agent/common)
        common call interception code(Android and JVM)
      • io.mockk:mockk-agent-api (agent/common)
        common API(Android and JVM) for call interception
      • org.objenesis:objenesis (2.6)
        library to instantiate objects
      • net.bytebuddy:byte-buddy (1.9.3)
        library to modify byte-code
      • net.bytebuddy:byte-buddy-agent (1.9.3)
        library to modify byte-code on flight

Android instrumented tests

  • io.mockk:mockk-android (mockk/android)
    Android implementation is based on JVM(with JVM agent exclusion)
    • io.mockk:mockk (mockk/jvm)
      JVM MockK implementation
      • io.mockk:mockk-common (mockk/common)
        common MockK implementation
        • io.mockk:mockk-dsl (dsl/common)
          common MockK DSL interface
      • io.mockk:mockk-dsl-jvm (dsl/jvm)
        JVM support functions for DSL
    • io.mockk:mockk-agent-android (agent/android)
      call interception for Android (drop-in replacement for mockk-agent-jvm)
      • io.mockk:mockk-agent-common (agent/common)
        common call interception code(Android and JVM)
      • io.mockk:mockk-agent-api (agent/common)
        common API(Android and JVM) for call interception
      • org.objenesis:objenesis (2.6)
        library to instantiate objects
      • com.linkedin.dexmaker:dexmaker (2.12.1)
        library to modify dex-code