2015-05-26 7 views
6

मैं अपनी नई परियोजना में roboelectric को एकीकृत करने की कोशिश कर रहा हूं। मैं लिंक का अनुसरण करते थे समझते हैं और impliment roboelectricग्रेडल के साथ एंड्रॉइड स्टूडियो के साथ रोबियोलेक्ट्रिक को एकीकृत करना हमेशा पैकेज देता है org.junit त्रुटि मौजूद नहीं है

http://pivotallabs.com/setting-up-robolectric-in-android-studio-1-1-on-os-x/

http://nenick-android.blogspot.in/2015/02/android-studio-110-beta-4-and.html

http://raptordigital.blogspot.in/2014/02/test-driven-development-with.html

लिए लेकिन मैं अपने परीक्षण वर्ग चल

Error:Gradle: Execution failed for task ':app:compileDebugJava'. 
> Compilation failed; see the compiler error output for details. 
E:\junitTest\android-studio-robolectric-example-master\JunitTestApp\app\src\test\java\com\inapp\junittestapp\SampleTest.java 
Error:(7, 17) Gradle: error: package org.junit does not exist 
Error:(8, 24) Gradle: error: package org.junit.runner does not exist 
Error:(9, 23) Gradle: error: package org.robolectric does not exist 
Error:(10, 34) Gradle: error: package org.robolectric.annotation does not exist 
Error:(11, 31) Gradle: error: package org.robolectric.shadows does not exist 
Error:(13, 27) Gradle: error: package org.hamcrest does not exist 
Error:(13, 1) Gradle: error: static import only from classes and interfaces 
Error:(14, 27) Gradle: error: package org.hamcrest does not exist 
Error:(14, 1) Gradle: error: static import only from classes and interfaces 
Error:(15, 24) Gradle: error: package org.junit does not exist 
Error:(15, 1) Gradle: error: static import only from classes and interfaces 
Error:(16, 30) Gradle: error: package org.robolectric does not exist 
Error:(16, 1) Gradle: error: static import only from classes and interfaces 
Error:(17, 30) Gradle: error: package org.robolectric does not exist 
Error:(17, 1) Gradle: error: static import only from classes and interfaces 
Error:(18, 30) Gradle: error: package org.robolectric does not exist 
Error:(18, 1) Gradle: error: static import only from classes and interfaces 
Error:(25, 2) Gradle: error: cannot find symbol class RunWith 
Error:(26, 2) Gradle: error: cannot find symbol class Config 
Error:(30, 6) Gradle: error: cannot find symbol class Test 
Error:(32, 38) Gradle: error: cannot find symbol method  setupActivity(Class<MainActivity>) 
Error:(36, 9) Gradle: error: cannot find symbol method clickOn(Button) 
Error:(38, 41) Gradle: error: cannot find symbol variable ShadowAlertDialog 
Error:(39, 42) Gradle: error: cannot find symbol method notNullValue() 
Error:(41, 9) Gradle: error: cannot find symbol class ShadowAlertDialog 
Error:(41, 47) Gradle: error: cannot find symbol method shadowOf(AlertDialog) 
Error:(42, 61) Gradle: error: cannot find symbol method is(String) 

अपने परीक्षण नीचे जोड़ी पर निम्न त्रुटि मिल रही हूँ कक्षा कोड

import android.app.AlertDialog; 
import android.widget.Button; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.robolectric.RobolectricTestRunner; 
import org.robolectric.annotation.Config; 
import org.robolectric.shadows.ShadowAlertDialog; 

import static org.hamcrest.CoreMatchers.is; 
import static org.hamcrest.CoreMatchers.notNullValue; 
import static org.junit.Assert.assertThat; 
import static org.robolectric.Robolectric.clickOn; 
import static org.robolectric.Robolectric.setupActivity; 
import static org.robolectric.RobolectricBase.shadowOf; 


@RunWith(RobolectricTestRunner.class) 
@Config(manifest = "app/src/main/AndroidManifest.xml", emulateSdk = 18) 
public class SampleTest { 

    @Test 
    public void buttonTapDisplaysAnAlertDialog() { 
     MainActivity helloActivity = setupActivity(MainActivity.class); 

     Button button = (Button) helloActivity.findViewById(R.id.button); 

     clickOn(button); 

     AlertDialog latestAlertDialog = ShadowAlertDialog.getLatestAlertDialog(); 
     assertThat(latestAlertDialog, is(notNullValue())); 

     ShadowAlertDialog shadowAlertDialog = shadowOf(latestAlertDialog); 
     assertThat(shadowAlertDialog.getTitle().toString(), is("Hi")); 
    } 

} 

और मेरे ऐप Gradle फ़ाइल

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 22 
    buildToolsVersion "21.1.2" 

    defaultConfig { 
     applicationId "com.example.junittestapp" 
     minSdkVersion 15 
     targetSdkVersion 22 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    sourceSets { main { java.srcDirs = ['src/main/java', 'src/test/java/'] } } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    compile 'com.android.support:support-v4:22.1.1' 
    testCompile 'junit:junit:4.12' 
    testCompile 'org.robolectric:robolectric:2.4' 
} 

मैं भी

https://discuss.gradle.org/t/why-cant-my-simplest-one-line-build-gradle-compile-tests-with-junit-jar/1868

मुझे लगता है कि मैं कुछ याद आती है इस ब्लॉग में एक ही त्रुटि पाई है। किसी भी तरह की सहायता को आभार समझेंगे।

+0

आप एंड्रॉयड स्टूडियो में इकाई परीक्षण सक्षम था? –

+0

हाँ धीरे-धीरे धीरे-धीरे अंदर ?? –

+0

कोई फ़ाइल> सेटिंग्स> Gradle-> प्रायोगिक: यूनिट टेस्टिंग सक्षम –

उत्तर

13

आपकी समस्या यह है कि आप एक sourceSet है मुख्य रूप में घोषित किया है कि आवेदन के लिए और परीक्षण के लिए कोड शामिल है। ': एप्लिकेशन: compileDebugJava' में विफल रहता है, क्योंकि यह (- बार यह testCompile में इंजेक्ट किया है) junit.jar उस पल में नहीं है यही कारण है कि Gradle कार्य है। दो अलग-अलग स्रोत घोषित करेंसेट और सबकुछ काम करना चाहिए।

sourceSets { 
    main { 
     java.srcDirs = ['src/main/java'] 
    } 
    robolectric { 
     java.srcDir file('src/test/java/') 
     resources.srcDir file('src/test/resources') 
    } 
} 
2

एक ही समस्या, मेरे मामले में मैं गलत निर्देशिका में परीक्षण वर्ग रखा था।

\ जावा src \ androidTest

[पैकेज] \

बजाय

src \ परीक्षण \ जावा [पैकेज] \

मुझे लगता है कि आप इसे कॉन्फ़िगर अन्य निर्देशिकाओं के साथ काम करने के लिए कर सकते हैं, लेकिन डाल एंड्रॉइड में परीक्षण और यूआई/सिस्टम परीक्षणों में यूनिट परीक्षण ठीक दिखता है

3

यदि आप एंड्रॉइड में अपने यूनिट परीक्षण चलाने की कोशिश करके यहां पहुंचे हैं तो आप स्रोतों में अपनी टेस्ट फाइलों के स्थान को अलग से निर्दिष्ट करके इस समस्या को ठीक कर सकते हैं:

sourceSets { 
    main { java.srcDirs = ['src/main/java'] } 
    test { java.srcDirs = ['src/test/java'] } 
} 

जहां भी आपकी स्रोत निर्देशिका है, आप इसका उपयोग करने के लिए संशोधित कर सकते हैं। नोट यह build.gradle (ऐप लेवल)

+0

धन्यवाद पढ़ें, यह मुझे सही पर डाल पथ। – waynesford

4

में एक ही त्रुटि में है। मेरे मामले का समाधान है: main.java.srcDirs से 'src/test/java' हटाएं।

से पहले:

main.java.srcDirs = ['src/main/java', 'src/test/java'] 

बाद:

main.java.srcDirs = ['src/main/java'] 

साथ एंड्रॉयड स्टूडियो 2.0 पूर्वावलोकन 4. और वास्तव में नवीनतम एंड्रॉयड स्टूडियो के साथ ठीक परीक्षण किया गया, यह robolectric स्थापित करने के लिए वास्तव में आवश्यक नहीं है SourceSets के अंदर संपत्ति (जैसा @falvick उल्लेख किया गया है)।

+0

यह मेरे लिए काम करता है, धन्यवाद जिंग ली – Victor

संबंधित मुद्दे