I've created unit tests as per http://tools.android.com/tech-docs/unit-testing-support, but when I run them I get an exception:
我根据http://tools.android.com/tech-docs/unit-testing-support创建了单元测试,但是当我运行它们时,我得到一个异常:
java.lang.NoClassDefFoundError: android/os/Parcelable
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
at java.lang.Class.getDeclaredMethods(Class.java:1860)
at org.junit.internal.MethodSorter.getDeclaredMethods(MethodSorter.java:54)
at org.junit.internal.runners.JUnit38ClassRunner.<init>(JUnit38ClassRunner.java:74)
at org.junit.internal.builders.JUnit3Builder.runnerForClass(JUnit3Builder.java:11)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:41)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.ClassNotFoundException: android.os.Parcelable
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 33 more
Here is a snippet of one of my tests:
这是我的一个测试的片段:
package com.aadhk.woinvoice.bean;
import com.aadhk.woinvoice.util.Constant;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
public class InvoiceCalculatorTest extends TestCase {
private static final double EPSILON = 1e-4;
public void testNoTaxNoDiscount() {
Invoice invoice = new Invoice();
List<InvoiceItem> items = new ArrayList<>();
InvoiceItem item = new InvoiceItem();
I'm using Android Studio 1.1, with Android Plugin Version 1.1.3 and Gradle version 2.2.1.
我正在使用Android Studio 1.1,Android插件版本1.1.3和Gradle版本2.2.1。
My code in question does implement the Parceable interface, but I was hoping there was a way to run these tests. The tests themselves do not exercise or use Parceable in any way.
我的代码确实实现了Parceable接口,但我希望有一种方法可以运行这些测试。测试本身不以任何方式运动或使用Parceable。
2 个解决方案
#1
0
Just ran into this using IntelliJ, but I'd imagine this would work for Android Studio as well.
刚刚使用IntelliJ遇到了这个问题,但我想这也适用于Android Studio。
I solved the issue by editing the run/debug configuration for my test and checking "Use alternative JRE" and trying a different platform of Android. I'd recommend trying out different ones and seeing if it resolves the issue.
我通过编辑测试的运行/调试配置并检查“使用替代JRE”并尝试使用不同的Android平台解决了这个问题。我建议尝试不同的,看看它是否能解决问题。
#2
-1
正如大卫罗森所说
Local Unit Tests run on your laptop or desktop in the JVM there. Hence, they do not have access to the native Android classes like
Parcelable
and if you run a local unit test against a class that uses those methods you will get errors.本地单元测试在您的笔记本电脑或台式机上运行。因此,他们无法访问像Parcelable这样的原生Android类,如果对使用这些方法的类运行本地单元测试,则会出现错误。
So I suggest to put your test class in the path app/src/androidTest/java/your-project-package/your-test-class
in your project directory. Also make sure to run your test on the device or emulator.
因此,我建议将测试类放在项目目录中的app / src / androidTest / java / your-project-package / your-test-class路径中。还要确保在设备或模拟器上运行测试。
#1
0
Just ran into this using IntelliJ, but I'd imagine this would work for Android Studio as well.
刚刚使用IntelliJ遇到了这个问题,但我想这也适用于Android Studio。
I solved the issue by editing the run/debug configuration for my test and checking "Use alternative JRE" and trying a different platform of Android. I'd recommend trying out different ones and seeing if it resolves the issue.
我通过编辑测试的运行/调试配置并检查“使用替代JRE”并尝试使用不同的Android平台解决了这个问题。我建议尝试不同的,看看它是否能解决问题。
#2
-1
正如大卫罗森所说
Local Unit Tests run on your laptop or desktop in the JVM there. Hence, they do not have access to the native Android classes like
Parcelable
and if you run a local unit test against a class that uses those methods you will get errors.本地单元测试在您的笔记本电脑或台式机上运行。因此,他们无法访问像Parcelable这样的原生Android类,如果对使用这些方法的类运行本地单元测试,则会出现错误。
So I suggest to put your test class in the path app/src/androidTest/java/your-project-package/your-test-class
in your project directory. Also make sure to run your test on the device or emulator.
因此,我建议将测试类放在项目目录中的app / src / androidTest / java / your-project-package / your-test-class路径中。还要确保在设备或模拟器上运行测试。