2016-01-17 22 views
8

Mockito çerçevesini android enstrümantasyon testleri ile kullanmaya çalışıyorum ancak doğru şekilde başlatamıyorum. Aşağıdaki sınama sınıf vardır: Bu testi çalıştırmak çalıştığınızda,Mockito ile Android enstrümantasyon testleri

class MainKontorTest extends ActivityInstrumentationTestCase2<MainActivity> { 

    @Mock Dependency bar; 

    @Override 
    public void setUp() { 
     super.setUp(); 
     MockitoAnnotations.initMocks(this); 
    } 

    public void testSomething() { 
     Foo foo = new Foo(bar); 
    } 
} 

alıyorum aşağıdaki hata ve StackTrace: org.mockito.internal.creation de

java.lang.ExceptionInInitializerError org.mockito.internal.creation.cglib.ClassImposterizer.imposterise adresindeki org.mockito.internal.creation.cglib.ClassImposterizer.imposterise (ClassImposterizer.java:57) adresindeki .cglib.ClassImposterizer.createProxyClass (ClassImposterizer.java:95) (ClassImposterizer.java:49) org.mockito.internal.creation.cglib.Cgl adresinde org.mockito.internal.MockitoCore.mock (MockitoCore.java:59) de org.mockito.internal.util.MockUtil.createMock (MockUtil.java:33) de ibMockMaker.createMock (CglibMockMaker.java:24) de org.mockito.Mockito.mock (Mockito.java:1285) org.mockito.internal.configuration.MockAnnotationProcessor.process de org.mockito.internal.configuration.MockAnnotationProcessor.process (MockAnnotationProcessor.java:33) de (MockAnnotationProcessor. java: org.mockito de org.mockito.internal.configuration.DefaultAnnotationEngine.process (DefaultAnnotationEngine.java:66) de org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor (DefaultAnnotationEngine.java:43) 16) . internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations (enjekte AnnotationEngine.java:71) org.arkadiy.moduledelegationsample en org.mockito.MockitoAnnotations.initMocks de org.mockito.internal.configuration.InjectingAnnotationEngine.process (InjectingAnnotationEngine.java:55) de (MockitoAnnotations.java:108) . ui.main.MainKontorTest.setUp (MainKontorTest.java:20) at junit.framework.TestCase.runBare (TestCase.java:132) at junit.framework.TestResult $ 1.protect (TestResult.java:115) Android'de .support.test.internal.runner.junit3.AndroidTestResult.runProtected (AndroidTestResult.java:77) at junit.framework.TestResult.run (TestResult.java:118) at android.support.test.internal.runner.junit3 .AndroidTestResult.run (AndroidTestResult.java:55) at junit.framework.TestCase.run (TestCase.ja) va: 124) , android.support.test.internal.runner.junit3.NonLeakyTestSuite $ NonLeakyTest.run (NonLeakyTestSuite.java:63) , junit.framework.TestSuite.runTest (TestSuite.java:243) adresinde . android.support.test.internal.runner.junit3.DelegatingTestSuite.run (DelegatingTestSuite.java:103) adresindeki android.support.test.internal.runner.junit3 adresindeki framework.TestSuite.run (TestSuite.java:238) . AndroidTestSuite.run (AndroidTestSuite.java:69) at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run (JUnit38ClassRunner.java:90) at org.junit.runners.Suite.runChild (Suite.java: 128) org.junit.runners.Suite.runChild adresinde (Suite.java:27) org.junit.runners.ParentRunner $ 3.run (ParentRunner.java:290)Org.junit.runners.ParentRunner $ 1schedule (ParentRunner.java:71) adresindeki org.junit.runners.ParentRunner.runChildren (ParentRunner.java:288) adresinden org.junit.runners.ParentRunner.access $ 000 (ParentRunner.java:58) org.junit.runners.ParentRunner $ 2.evaluate (ParentRunner.java:268) org.junit.runners.ParentRunner.run (ParentRunner.java:363) org.junit.runner adresinde .JUnitCore.run (JUnitCore.java:137) , org.junit.runner.JUnitCore.run (JUnitCore.java:115) adresinde, android.support.test.internal.runner.TestExecutor.execute (TestExecutor.java: 54) android.support.test.runner.AndroidJUnitRunner.onStart (AndroidJUnitRunner.java:240) , android.app.Instrumentation $ InstrumentationThread.run (Instrumentation.java:1869) Neden: org.mockito. cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException -> null org.mockito.cglib.core.AbstractClassGenerator.create (AbstractClassGenerator.java:238) adresinden org.mockito.cglib.core.KeyFactory $ Generator .create (KeyFactory.java:145) org.mockito.cglib.core.KeyFactory.create (KeyFactory.java:117) org.mockito.cglib.core.KeyFactory.create adresinde (KeyFactory.java:109) org.mockito.cglib.core.KeyFactory.create adresinde (KeyFactory.java:105) org.mockito.cglib.proxy.Enhancer adresinde bulunabilir. (Enhancer.java:70) ... 40 daha fazla Nedeniyle: java.lang.reflect.InvocationTargetException java.lang.reflect.Method.invoke (Yerel Yöntem) java.lang.reflect.Method.invoke (Method.java:372) org.mockito.cglib.core.AbstractClassGenerator.create de org.mockito.cglib.core.ReflectUtils.defineClass (ReflectUtils.java:385) (AbstractClassGenerator.java:220) ... en 45 daha neden Olduğu: java.lang.UnsupportedOperationException: ... java.lang.ClassLoader.defineClass (ClassLoader.java:300) de sınıf dosyasının bu tür yükleyemiyor daha 49

nasıl Mockito'yu Enstrümantasyon testleri ile kullanabilir miyim? Yeni Rule api ile Mockito kullanmayı denedim ama hata aynıydı.

+1

APK'larınızı not kullanarak oluşturmuyorsanız, http://stackoverflow.com/questions/29290795/attempt-to-mockito-mock-any-class-generates-exceptionininitializererror/41350001#41350001 adresindeki cevabıma bakın. daha fazla bilgi –

cevap

9

Mockito'nun yarattığı alaylar, sınıf dosyaları oluşturulur; Bununla birlikte, Mockito bir JVM'de kullanılmak üzere tasarlanmıştır, bu yüzden .class dosyalarından çıktığında cglib'u kullanır. Android Instrumentation test vakaları cihazlar veya emülatörler üzerinde çalışır, bu yüzden Dalvik .dex dosyalarına ihtiyaç duyarlar.

Sınıf yolunuzda DexMaker'u dahil ettiğinizden emin olmanız gerekir. dexmaker-mockito Maven projesi doğru görünüyor, ancak bu noktada birkaç versiyon olan Mockito 1.10.5'e bağlı.

Bir yan not olarak, Android Testing Support Library'u kullanmadığınız sürece, JUnit3 semantiği kullanmanız gerekir. @Rule alanlarını veya özel test koşucularını kullanamazsınız; Ayrıca setUp ve tearDown'u (@Before ve @After ek açıklamalarının aksine) geçersiz kılmanız ve testlerinizi testFooBar olarak adlandırmanız gerekir (@Test ek açıklamalarının aksine).

+0

Hangi ek açıklamaların kullanılacağını, sınıfın nasıl tanımlanacağını, test yöntemlerini vb. – user1743524

+0

APK'larınızı not kullanarak oluşturmadıysanız, bu sizin için çalışmayabilir. Daha fazla bilgi için http://stackoverflow.com/questions/29290795/attempt-to-mockito-mock-any-class-generates-exceptionininitializererror/41350001#41350001 adresindeki cevabımı inceleyin –

+0

Neden ** @ Rule * kullanamıyoruz? * JUnit 4 ile ek açıklama mı? –