All Projects → tmurakami → dexopener

tmurakami / dexopener

Licence: Apache-2.0 license
An Android library that provides the ability to mock your final classes on Android devices.

Programming Languages

java
68154 projects - #9 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to dexopener

Sinon Jest Cheatsheet
Some examples on how to achieve the same goal with either of both libraries: sinon and jest. Also some of those goals achievable only by one of these tools.
Stars: ✭ 226 (+101.79%)
Mutual labels:  mock, mocking
mockingbird
🐦 Decorator Powered TypeScript Library for Creating Mocks
Stars: ✭ 70 (-37.5%)
Mutual labels:  mock, mocking
Pester
Pester is the ubiquitous test and mock framework for PowerShell.
Stars: ✭ 2,620 (+2239.29%)
Mutual labels:  mock, mocking
Python Mocket
a socket mock framework - for all kinds of socket animals, web-clients included
Stars: ✭ 209 (+86.61%)
Mutual labels:  mock, mocking
springmock
alternative spring mocking infrastructure
Stars: ✭ 22 (-80.36%)
Mutual labels:  mock, mocking
Fake Xrm Easy
The testing framework for Dynamics CRM and Dynamics 365 which runs on an In-Memory context and deals with mocks or fakes for you
Stars: ✭ 216 (+92.86%)
Mutual labels:  mock, mocking
Axios Mock Adapter
Axios adapter that allows to easily mock requests
Stars: ✭ 2,832 (+2428.57%)
Mutual labels:  mock, mocking
Mockito
Most popular Mocking framework for unit tests written in Java
Stars: ✭ 12,453 (+11018.75%)
Mutual labels:  mock, mocking
Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+2978.57%)
Mutual labels:  mock, mocking
Faker
Provides fake data to your Android apps :)
Stars: ✭ 234 (+108.93%)
Mutual labels:  mock, mocking
Ts Auto Mock
Typescript transformer to unlock automatic mock creation for interfaces and classes
Stars: ✭ 204 (+82.14%)
Mutual labels:  mock, mocking
go-github-mock
A library to aid unittesting code that uses Golang's Github SDK
Stars: ✭ 63 (-43.75%)
Mutual labels:  mock, mocking
Mocktopus
Mocking framework for Rust
Stars: ✭ 179 (+59.82%)
Mutual labels:  mock, mocking
Mockery
A mock code autogenerator for Golang
Stars: ✭ 3,138 (+2701.79%)
Mutual labels:  mock, mocking
Mocktail
A mock library for Dart inspired by mockito
Stars: ✭ 172 (+53.57%)
Mutual labels:  mock, mocking
Mockiato
A strict, yet friendly mocking library for Rust 2018
Stars: ✭ 229 (+104.46%)
Mutual labels:  mock, mocking
Duckrails
Development tool to mock API endpoints quickly and easily (docker image available)
Stars: ✭ 1,690 (+1408.93%)
Mutual labels:  mock, mocking
Httpretty
Intercept HTTP requests at the Python socket level. Fakes the whole socket module
Stars: ✭ 1,930 (+1623.21%)
Mutual labels:  mock, mocking
Okhttp Json Mock
Mock your datas for Okhttp and Retrofit in json format in just a few moves
Stars: ✭ 231 (+106.25%)
Mutual labels:  mock, mocking
Http Fake Backend
Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 253 (+125.89%)
Mutual labels:  mock, mocking

DexOpener

Android CircleCI Release Javadoc

An Android library that provides the ability to mock your final classes on Android devices.

Installation

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

repositories {
    google()
    maven { url 'https://jitpack.io' }
}

dependencies {
    androidTestImplementation 'com.github.tmurakami:dexopener:2.0.5'

    androidTestImplementation 'androidx.test:runner:x.y.z'

    // DexOpener is also usable together with the ATSL runner.
    // androidTestImplementation 'com.android.support.test:runner:x.y.z'
}

Usage

Add an AndroidJUnitRunner subclass into your app's androidTest directory.

// Specify your root package as `package` statement.
// The final classes you can mock are only in the package and its subpackages.
package your.root.pkg;

public class YourAndroidJUnitRunner extends AndroidJUnitRunner {
    @Override
    public Application newApplication(ClassLoader cl, String className, Context context)
            throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        DexOpener.install(this); // Call me first!
        return super.newApplication(cl, className, context);
    }
}

Then specify your AndroidJUnitRunner as the default test instrumentation runner in your app's build.gradle.

android {
    defaultConfig {
        minSdkVersion 16 // 16 or higher
        testInstrumentationRunner 'your.root.pkg.YourAndroidJUnitRunner'
    }
}

You can see some examples here.

Tips

Replacing the Application instance for testing

To instantiate your custom android.app.Application object other than default application, pass a string literal of that class name as the second argument to super.newApplication() in your test instrumentation runner.

@Override
public Application newApplication(ClassLoader cl, String className, Context context)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    DexOpener.install(this); // Call me first!
    return super.newApplication(cl, "your.root.pkg.YourTestApplication", context);
}

Do not call Class#getName() to get your Application class name. The following code may cause an IllegalAccessError saying Class ref in pre-verified class ....

return super.newApplication(cl, YourTestApplication.class.getName(), context);

Alternatives

Kotlin all-open compiler plugin

DexOpener will do the following things at runtime:

  1. Remove the final modifier from the classes belonging to the specified package
  2. Create dex files to make the application class loader load those classes

However, they are not so lightweight. If you would like to save even a little testing time of your Kotlin app, you can introduce the all-open compiler plugin instead of DexOpener.

This article would be helpful to know how to open Kotlin classes with that plugin only for testing.

Dexmaker

You can now even stub the final methods of the Android API using dexmaker-mockito-inline library. In addition, dexmaker-mockito-inline-extended library supports for stubbing static methods and spying on an object created by the Android system such as Activity. Here is an introduction article.

Note that these libraries will only work with Android 9+.

MockK

MockK provides inline mocking feature derived from Dexmaker. You would be able to mock the final classes by using mockk-android library. That feature is automatically enabled with Android 9+. You can see the supported features here.

By checking Build.VERSION.SDK_INT in your test instrumentation runner, you can switch that feature and DexOpener according to the OS version of the testing device. See the test instrumentation runner in the mockk example.

Notice

DexOpener contains the classes of the following libraries:

They have been minified with ProGuard and repackaged with Jar Jar Links.

License

Copyright 2016 Tsuyoshi Murakami

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].