Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception running JUnit tests with AndroidStudio: java.lang.NoClassDefFoundError: android/os/Parcelable

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:

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.

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.

like image 432
Alex Black Avatar asked Mar 25 '15 20:03

Alex Black


1 Answers

Just ran into this using IntelliJ, but I'd imagine this would work for Android Studio as well.

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. Test Configuration Screenshot

like image 178
Stevie Kideckel Avatar answered Nov 03 '22 21:11

Stevie Kideckel