2012-05-02 35 views
0

我試圖設置爲Android JUnit測試,我有清單和這樣的設置,但我不斷收到這個errror:無法實例化儀表ComponentInfo

java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo {com.example.Android/android.test.InstrumentationTestRunner}: java.lang.ClassNotFoundException: android.test.InstrumentationTestRunner in loader dalvik.system.PathClassLoader[/data/app/com.example.Android-1.apk:/data/app/com.example.Android-1.apk] 

測試類:

public class ConnectionToSiteTest extends AndroidTestCase{ 
    private final String emailUsername = "[email protected]"; 
    private final String password = "test"; 
    private final String features = "222222"; 
    private String commands; 

    public void testCommandReturn(){ 
     requestToSite(); 

     assertEquals("222222", commands); 
    } 

    private void requestToSite(){ 
     try { 
      // Create params for connection including 3sec timeout 
      // on connection and 5sec timeout on socket 
      HttpParams httpParams = new BasicHttpParams(); 
      int timeoutConnection = 3000; 
      int timeoutSocket = 5000; 

      // Set the timeouts 
      HttpConnectionParams.setConnectionTimeout(httpParams, 
        timeoutConnection); 
      HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket); 
      HttpClient httpClient = new DefaultHttpClient(httpParams); 

      // Create the data entities 
      HttpPost post = new HttpPost("http://example.com/AndroidCommands"); 
      HttpResponse response; 

      // Set POST data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
        2); 
      nameValuePairs.add(new BasicNameValuePair("emailUsername", 
        emailUsername)); 
      nameValuePairs.add(new BasicNameValuePair("password", 
        password)); 
      nameValuePairs.add(new BasicNameValuePair("features", 
        features)); 
      post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Query the server and store the user's ID 
      response = httpClient.execute(post); 

      // Grabs the command string from response headers and processes them 
      commands = (String) response.getHeaders("commands")[0].getValue(); 

     } catch (UnsupportedEncodingException e) { 
     } catch (ClientProtocolException e) { 
     } catch (IOException e) { 
     } 
    } 
} 

的AndroidManifest.xml:

<uses-library android:name="android.test.runner" /> 
<instrumentation android:name="android.test.InstrumentationTestRunner" 
    android:targetPackage="edu.neumont.MillerK.Android" android:label="Tests for My App" /> 
<activity 
     android:name="com.example.Android.ConnectionToSiteTest">    
    </activity> 

測試套件類:

public class AllTests extends TestSuite { 
    public static Test suite() { 
     return new TestSuiteBuilder(AllTests.class).includeAllPackagesUnderHere().build(); 
    } 
} 

我一直在試圖找到相同的異常,它已被修復,但沒有發現任何東西。

回答

1

您確定這些測試是否在正確的Android測試項目中?右鍵單擊項目 - > Android工具 - >添加Android測試項目,即可在Eclipse中添加一個。你所有的測試都應該放在那個項目中。

+0

這是我的問題,感謝您的幫助 – Krmiller92

0

可能是因爲Eclipse沒有正確構建包。當它發生在我身上時,完全一樣的錯誤,我查了一下,很容易找到解決方案。 在我的情況下,我失蹤Android Package Builder under YourProject-> Properties-> Builders。 爲了再次添加它,我只在Eclipse的.project文件中添加了幾行(ApkBuilder)。你可以有一個不同的問題,但檢查您的.project看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<projectDescription> 
    <name>xxx-it</name> 
    <comment></comment> 
    <projects> 
     <project>xxx-project</project> 
    </projects> 
    <buildSpec> 
     <buildCommand> 
      <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name> 
      <arguments> 
      </arguments> 
     </buildCommand> 
     <buildCommand> 
      <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name> 
      <arguments> 
      </arguments> 
     </buildCommand> 
     <buildCommand> 
      <name>org.eclipse.jdt.core.javabuilder</name> 
      <arguments> 
      </arguments> 
     </buildCommand> 
     <buildCommand> 
      <name>com.android.ide.eclipse.adt.ApkBuilder</name> 
      <arguments> 
      </arguments> 
     </buildCommand> 
     <buildCommand> 
      <name>org.eclipse.m2e.core.maven2Builder</name> 
      <arguments> 
      </arguments> 
     </buildCommand> 
    </buildSpec> 
    <natures> 
     <nature>com.android.ide.eclipse.adt.AndroidNature</nature> 
     <nature>org.eclipse.jdt.core.javanature</nature> 
     <nature>org.eclipse.m2e.core.maven2Nature</nature> 
    </natures> 
</projectDescription> 
+0

哦,我認爲在AndroidManifest.xml的包名是正確的。都在清單和儀器標籤下。 –

相關問題