Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了执行Android JUnit测试时出现NoClassDefFoundError大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
首先,有类似的问题,如 herehere,但我既没有一些外部库,也没有一个lib文件夹,也没有一些包括罐子或东西,所以我想知道我在运行Android Junit测试时做错了什么.

我的项目结构如下所示:

如您所见,我有一个单独的Android JUnit测试项目.测试类看起来像这样

public class PersistenceManager {
    private static final String FILename = "kpzwien_storage";

    public static void persist(String data,Context context) throws IOException  {
        FiLeoutputStream fos = context.openFiLeoutput(FILename,Context.MODE_PRIVATE);
        fos.write(data.getBytes());
        fos.close();
    }

    public static String read(Context context) throws IOException  {
        FileInputStream fis = context.openFileInput(FILEName);
        StringBuffer fileContent = new StringBuffer("");

        byte[] buffer = new byte[1024];
        while (fis.read(buffer) != -1) {
            fileContent.append(new String(buffer));
        }

        return fileContent.toString();
    }
}

这是它的测试用例:

public class PersistenceManagerTest extends AndroidTESTCase {

    private final String FILename_PREFIX = "test.";

    @Before
    public void setUp() {
        mockContentResolver resolver = new mockContentResolver();
        RenamingDelegaTingContext renamingDelegaTingContext = new RenamingDelegaTingContext(new mockContext(),getContext(),FILename_PREFIX);
        Context context = new IsolatedContext(resolver,renamingDelegaTingContext);

        setContext(context);
    }

    public void testPersistAndRead() throws IOException {
        String testData = "foobar";

        PersistenceManager.persist(testData,getContext());

        String result = PersistenceManager.read(getContext());

        assertEquals(testData,result);
    }
}

测试项目的清单如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://scheR_421_11845@as.android.com/apk/res/android"
    package="net.devgems.android.kurzparkzonewien.androidtests"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />

    <instrumentation
        android:name="android.test.instrumentationTestRunner"
        android:targetPackage="net.devgems.android.kurzparkzonewien.activities" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@String/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>
</manifest>

targetPackage是instrumentation对象将运行的应用程序,该应用程序由元素在其清单文件中分配的包名称标识. (资料来源:http://developer.android.com/guide/topics/manifest/instrumentation-element.html

我主项目中包的名称

所以它正是targetPackage指向的地方.如果我作为“Android JUnit测试”运行,我总是得到一个NoClassDefFoundError,但为什么?有任何想法吗?我正在使用ADT 20.0.3,Eclipse Juno.

最后这里是logcat@L_616_12@:

解决方法

我找到了解决方案.但它实际上是某种解决方法,因为我无法想象我通常需要像这样修补一下(也许有人知道更好的解决方案?):

我的主项目是@L_99_2@maven项目,@L_607_18@个项目中我必须设置从/ bin …到/ target / classes,target / test-classes和target / generated-sources配置@L_616_12@文件夹.我已经这样做了,因为否则我的JUnit测试从主项目找不到最新的代码更改,只要我不运行mvn install ..如果@L_616_12@文件夹是target / …,我不需要运行mvn安装以在我的测试中进行最新更改.
现在的问题是Android测试项目在存储在主项目的目标中时找不到编译的源.当我将主项目中的@L_616_12@文件夹设置为bin并运行mvn install时,Android测试项目现在可以找到这些类.我必须这样做一次.当我将其设置回目标时,我的Android测试仍然运行并且我的主要JUnit测试运行.当我更改主项目中方法时,测试项目立即识别出更改,这意味着它不使用二进制jar或其他东西(这确实非常好,然我不明白它为什么现在找到它,然我把它设定回目标?).

[updatE]
将这两行添加到主项目的pom to build部分可以解决问题,因此我不再需要解决方法了.

<outputDirectory>bin/classes</outputDirectory>
    <testOutputDirectory>bin/test-classes</testOutputDirectory>

大佬总结

以上是大佬教程为你收集整理的执行Android JUnit测试时出现NoClassDefFoundError全部内容,希望文章能够帮你解决执行Android JUnit测试时出现NoClassDefFoundError所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。