尝试在Android Studio上运行单元测试时出现NoClassDefFoundError [英] NoClassDefFoundError while trying to run unit tests on Android Studio

查看:202
本文介绍了尝试在Android Studio上运行单元测试时出现NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的Android应用程序创建一些简单的测试,但是在使测试无法正常运行方面遇到了问题.在尝试运行测试时,我不断收到NoClassDefFound错误,但是我似乎无法弄清楚是什么原因造成的.如果能让我睁开双眼看一下,我将不胜感激.

I'm trying to create some simple tests for my Android app but I'm running into issues with getting the tests to even run. I keep getting a NoClassDefFound error while I'm trying to run the test, however I can't seem to figure out what the cause of it is. I'd appreciate it if I could get a fresh pair of eyes to look at this.

这是该结构的图片:

这是我不断得到的错误:

Here's the error that I keep getting:

junit.framework.AssertionFailedError: Exception in constructor: testScanResultNotNull (java.lang.NoClassDefFoundError: nl.hanze.myhealth.ScanResultActivity
at nl.hanze.myhealth.CameraTest.<init>(CameraTest.java:14)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at junit.runner.BaseTestRunner.getTest(BaseTestRunner.java:118)
at android.test.AndroidTestRunner.getTest(AndroidTestRunner.java:149)
at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:57)
at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:80)
at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:443)
at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:424)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:370)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4435)
at android.app.ActivityThread.access$1300(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

CameraTest

CameraTest

package nl.hanze.myhealth;

import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;

    public class CameraTest extends ActivityInstrumentationTestCase2<ScanResultActivity> {

        ScanResultActivity mActivity;

        public CameraTest(){
            super(ScanResultActivity.class);
        }

        @Override
        protected void setUp()throws Exception{
            super.setUp();
            mActivity = getActivity();
        }

        @SmallTest
        public void testScanResultNotNull(){
            boolean test = mActivity.generateScanResult();
            assertNotNull(test);
        }

        @Override
        protected void tearDown() throws Exception{
            super.tearDown();
        }
    }

ScanResultActivity

ScanResultActivity

package nl.hanze.myhealth;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.Random;


    public class ScanResultActivity extends Activity {

        Random rand = new Random();

        public ScanResultActivity(){

        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_scan_result);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.scan_result, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        protected boolean generateScanResult(){
            boolean positive_scanresult;
            int max = 10;
            int min = 1;
            int randomNum = rand.nextInt((max - min) + 1) + min;
            if(randomNum >= 6){
                positive_scanresult = true;
            }else{
                positive_scanresult = false;
            }
            return positive_scanresult;
        }
    }

推荐答案

1)将测试放入软件包 nl.hanze.myhealth.tests . 您可以使用Android Studio或手动进行以下操作:

1) Put your tests into package nl.hanze.myhealth.tests. You can do so using Android Studio, or manually:

  • 在... \ app \ src \ androidTest \ java \ nl \ hanze \ myhealth \
  • 内创建一个新文件夹 tests
  • 在... \ app \ src \ androidTest \ java \ nl \ hanze \ myhealth \ tests \
  • 中移动ApplicationTest.java和CameraTest.java
  • 将每个Java测试文件中的程序包名称重命名为
    软件包nl.hanze.myhealth.tests;
  • create a new folder tests inside ...\app\src\androidTest\java\nl\hanze\myhealth\
  • move ApplicationTest.java and CameraTest.java inside ...\app\src\androidTest\java\nl\hanze\myhealth\tests\
  • rename the package name in each of your java test files to
    package nl.hanze.myhealth.tests;

2)在应用程序的 build.gradle 中,您需要具备以下条件:

2) In you application's build.gradle you need to have:

defaultConfig {
    applicationId "nl.hanze.myhealth"
    testApplicationId "nl.hanze.myhealth.tests"
    ...
}

重建并运行.那应该足够了.

Rebuild and run. That should be enough.

这篇关于尝试在Android Studio上运行单元测试时出现NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆