dimanche 28 août 2016

Mockito exception when Espresso added to project

I was successfully using Mockito in my project, then I decided to add some Espresso testing (As described here:http://ift.tt/2bKskRH) and I am stuck - when I add espresso to my build.gradle and I get problems with Mockito.

My dependencies in build.gradle when Mockito is working OK, e.g. my tests which use Mockito pass:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.4.0'
    testCompile 'junit:junit:4.12'
    //Mockito framework
    testCompile 'org.mockito:mockito-core:1.10.19'
    // http://ift.tt/2bSacZx
    testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.6.4'
    testCompile 'org.powermock:powermock-api-mockito:1.5.5'
    androidTestCompile ('com.android.support:support-annotations:23.4.0')
    //androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.2')
}

When I uncomment the last line to include com.android.support.test.espresso:espresso-core:2.2.2 and I run my tests again I get the following error for some of them:

java.lang.IllegalAccessError: class org.mockito.internal.creation.jmock.ClassImposterizer$1 cannot access its superclass org.mockito.internal.creation.cglib.MockitoNamingPolicy

    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at org.mockito.internal.creation.jmock.ClassImposterizer.<clinit>(ClassImposterizer.java:38)
    at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23)
    at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:43)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
    at org.mockito.Mockito.spy(Mockito.java:1368)
    at todolistj.todolist.unittest.CustomClass.size(CustomClass.java:54)

All the tests that fail are failing either on Mockito.spy or on Mockito.mock methods. Something like:

CustomClass1 li1 = Mockito.spy(new CustomClass1(somevar));

or

View vm= Mockito.mock(View.class);

Any help would be appreciated!

Aucun commentaire:

Enregistrer un commentaire