gpt4 book ai didi

java - Android 在构造函数 NoClassDefFoundError 中测试异常

转载 作者:行者123 更新时间:2023-11-29 20:45:51 25 4
gpt4 key购买 nike

在解决了我的项目结构和 android 测试问题之后,似乎又出现了另一个问题,当我尝试运行我的测试时,我得到了这个异常:

junit.framework.AssertionFailedError: Exception in constructor: testRegisterNaming (java.lang.NoClassDefFoundError: es.unizar.vv.mobile.catmdedit.app.view.LibraryActivity
at es.unizar.vv.mobile.catmdedit.app.LibraryTest.<init>(LibraryTest.java:16)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at junit.framework.TestSuite.createTest(TestSuite.java:61)
at junit.framework.TestSuite.addTestMethod(TestSuite.java:294)
at junit.framework.TestSuite.addTestsFromTestCase(TestSuite.java:150)
at junit.framework.TestSuite.<init>(TestSuite.java:129)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite.<init>(NonLeakyTestSuite.java:34)
at android.support.test.internal.runner.junit3.AndroidTestSuite.<init>(AndroidTestSuite.java:43)
at android.support.test.internal.runner.junit3.AndroidJUnit3Builder.runnerForClass(AndroidJUnit3Builder.java:50)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runner.Computer.getRunner(Computer.java:38)
at org.junit.runner.Computer$1.runnerForClass(Computer.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:98)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84)
at org.junit.runners.Suite.<init>(Suite.java:79)
at org.junit.runner.Computer.getSuite(Computer.java:26)
at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:598)
at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:578)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:542)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:269)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)
)
at junit.framework.Assert.fail(Assert.java:50)
at junit.framework.TestSuite$1.runTest(TestSuite.java:97)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:115)
at junit.framework.TestResult.runProtected(TestResult.java:133)
at android.support.test.internal.runner.junit3.DelegatingTestResult.runProtected(DelegatingTestResult.java:90)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:49)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:63)
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:270)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

testRegisterNaming 甚至不是构造函数,但在堆栈跟踪中它将 LibraryTest.java:16 标记为异常源

这是测试套件代码:

public class LibraryTest extends ActivityInstrumentationTestCase2<LibraryActivity> {

private LibraryActivity libraryActivity;

public LibraryTest() {
super(LibraryActivity.class); //this is line 16
}

@Override
public void setUp() throws Exception {
super.setUp();
libraryActivity = getActivity();
}

public void testRegisterNaming() {
Register r = new Register("Foobar", RegType.TextReg);
assertEquals("Foobar",r.getName());
}
}

就我在 Android 中处理过的测试而言,这不应该在任何地方失败,但事实上……我看不出它会在哪里失败!

此外,build.gradle 文件:

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.2'
}
}

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.0"

defaultConfig {
applicationId "es.unizar.vv.mobile.catmdedit.app"
minSdkVersion 16
targetSdkVersion 16
versionCode 1
versionName "1.0"
testApplicationId "es.unizar.vv.mobile.catmdedit.app"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'LICENSE.txt'
}

sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'

java {
srcDir 'src/main/java'
}
resources {
srcDir 'src/main/resources'
}
res.srcDirs = ['res']
}

androidTest.setRoot('src/androidTest')

androidTest {
java{
srcDirs 'src/androidTest/java'
}
}
}
}

dependencies {
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}

最佳答案

关注这篇文章junit.framework.AssertionFailedError: Exception in constructor: (java.lang.NoClassDefFoundError)我尝试添加带有排除项的 contrib 库:

dependencies {
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.0') {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
}

同时,将所有测试类移动到 *.app.test 包中,设置

testApplicationId "es.unizar.vv.mobile.catmdedit.app.test"

现在它可以正常工作了!

关于java - Android 在构造函数 NoClassDefFoundError 中测试异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30382129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com