Skip to content

Instantly share code, notes, and snippets.

@Piasy
Created October 21, 2016 15:42
Show Gist options
  • Save Piasy/0233399d78e4e7dfd5dfa3fe1786ccce to your computer and use it in GitHub Desktop.
Save Piasy/0233399d78e4e7dfd5dfa3fe1786ccce to your computer and use it in GitHub Desktop.
mockito connected Android test bug repro
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.piasy.mockitodexmakerbug">
<application
android:theme="@style/Theme.AppCompat"
>
<activity android:name=".MyActivity"/>
</application>
</manifest>
apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:24.2.1'
androidTestCompile "junit:junit:4.12"
androidTestCompile "org.mockito:mockito-core:1.10.19"
androidTestCompile "com.github.crittercism.dexmaker:dexmaker-mockito:f8e7c23e4172af8767758c99c9165dd2bd4f0e7f"
androidTestCompile "com.android.support.test:rules:0.5"
androidTestCompile "com.android.support.test:runner:0.5"
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:24.2.1'
androidTestCompile "junit:junit:4.12"
androidTestCompile "org.mockito:mockito-core:2.2.6"
androidTestCompile "com.github.crittercism.dexmaker:dexmaker-mockito:f8e7c23e4172af8767758c99c9165dd2bd4f0e7f"
androidTestCompile "com.android.support.test:rules:0.5"
androidTestCompile "com.android.support.test:runner:0.5"
}
./gradlew :prj:connectedCheck fail stack trace
java.lang.AbstractMethodError: abstract method "org.mockito.plugins.MockMaker$TypeMockability org.mockito.plugins.MockMaker.isTypeMockable(java.lang.Class)"
at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:168)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:162)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
at org.mockito.Mockito.mock(Mockito.java:1632)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)
at org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:39)
at org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:63)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:59)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:43)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:67)
at org.mockito.internal.junit.JUnitRule$DefaultStatement.performEvaluation(JUnitRule.java:79)
at org.mockito.internal.junit.JUnitRule$DefaultStatement.evaluate(JUnitRule.java:72)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:24.2.1'
androidTestCompile "junit:junit:4.12"
androidTestCompile "org.mockito:mockito-core:2.2.6"
androidTestCompile "com.android.support.test:rules:0.5"
androidTestCompile "com.android.support.test:runner:0.5"
}
./gradlew :prj:connectedCheck fail stack trace
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.github.piasy.mockitodexmakerbug.MyPresenter.
Mockito can only non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 0.9
JVM vendor name : The Android Project
JVM vendor version : 2.1.0
JVM name : Dalvik
JVM version : 0.9
JVM info : null
OS name : Linux
OS version : 3.10.73-g76d746e
Underlying exception : java.lang.IllegalStateException: Error invoking java.lang.ClassLoader#findClass
at org.mockito.internal.junit.JUnitRule$DefaultStatement.performEvaluation(JUnitRule.java:79)
at org.mockito.internal.junit.JUnitRule$DefaultStatement.evaluate(JUnitRule.java:72)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
Caused by: java.lang.IllegalStateException: Error invoking java.lang.ClassLoader#findClass
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Resolved.loadClass(ClassInjector.java:389)
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.inject(ClassInjector.java:202)
at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.load(ClassLoadingStrategy.java:188)
at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:73)
at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4525)
at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.mockClass(SubclassBytecodeGenerator.java:80)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$CachedBytecodeGenerator.getOrGenerateMockClass(TypeCachingBytecodeGenerator.java:87)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:34)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMockType(SubclassByteBuddyMockMaker.java:64)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:35)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:22)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:63)
at org.mockito.Mockito.mock(Mockito.java:1632)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)
at org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:39)
at org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:63)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:59)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:43)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:67)
... 27 more
Caused by: java.lang.UnsupportedOperationException: can't load this type of class file
at java.lang.ClassLoader.defineClass(ClassLoader.java:594)
at java.lang.reflect.Method.invoke(Native Method)
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Resolved.loadClass(ClassInjector.java:385)
... 47 more
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MyActivity extends AppCompatActivity {
private MyFragment mTestFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
void startTestFragment(MyPresenter testPresenter) {
mTestFragment = new MyFragment();
mTestFragment.setTestPresenter(testPresenter);
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, mTestFragment)
.commit();
}
MyFragment getTestFragment() {
return mTestFragment;
}
}
import android.support.v4.app.Fragment;
public class MyFragment extends Fragment {
private MyPresenter mTestPresenter;
void setTestPresenter(MyPresenter testPresenter) {
mTestPresenter = testPresenter;
}
@Override
public void onStart() {
super.onStart();
mTestPresenter.attachView(this);
}
@Override
public void onStop() {
super.onStop();
mTestPresenter.detachView();
}
@Override
public void onDestroy() {
super.onDestroy();
mTestPresenter.onDestroy();
}
}
import android.content.Intent;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@RunWith(AndroidJUnit4.class)
public class MyFragmentTest {
@Rule
public MockitoRule mMockitoRule = MockitoJUnit.rule();
@Rule
public ActivityTestRule<MyActivity> mActivityTestRule =
new ActivityTestRule<>(MyActivity.class, true, false);
@Mock
private MyPresenter mTestPresenter;
private MyFragment mTestFragment;
@Before
public void setUp() throws Exception {
MyActivity activity = mActivityTestRule.launchActivity(new Intent());
activity.startTestFragment(mTestPresenter);
mTestFragment = activity.getTestFragment();
Thread.sleep(5_000);
}
@Test
public void onDestroy() throws Exception {
mTestFragment.onStop();
mTestFragment.onDestroy();
verify(mTestPresenter, times(1)).attachView(mTestFragment);
verify(mTestPresenter, times(1)).detachView();
verify(mTestPresenter, times(1)).onDestroy();
verifyNoMoreInteractions(mTestPresenter);
}
}
public class MyPresenter {
private MyFragment mView;
public void attachView(MyFragment view) {
mView = view;
}
public void detachView() {
mView = null;
}
public void onDestroy() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment