48

I saw that I'm not the only one having this problem but I don't find a correct answer. I have an android project that I want to test. I create a junit test class for each class of my project. My problem is when I run my test, I have the following error :

java.lang.NoClassDefFoundError: android/content/Context

This is my class test :

public class DevicesBDDTest extends TestCase {

    DevicesBDD bdd;

    /**
     * @throws java.lang.Exception
     */
    protected static void setUpBeforeClass() throws Exception {
    }

    /**
     * @throws java.lang.Exception
     */
    protected static void tearDownAfterClass() throws Exception {
    }

    protected void setUp() throws Exception {
        super.setUp();
        Context ctx = mock(Context.class);
        final MaBaseSQLiteInterface mockMaBaseSQLite = mock(MaBaseSQLiteInterface.class);
        bdd = new DevicesBDD(ctx){
            @Override
            public MaBaseSQLiteInterface createMaBaseSQlite(Context context) {
                return mockMaBaseSQLite;
            }
        };
    }


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

    public void test() {
        assertEquals(1, 1);
    }
}

My class DevicesBDD has needs an object Context, therefore I create a mock (with mockito). I tried with a object MockContext too, but it's doesn't work.

This is my Java Build Path :

  • mockito-all-1.9.5.jar
  • Android 2.1 -> android.jar
  • Android Dependencies -> annotations.jar
  • Junit 3 -> junit.jar
helene
  • 1,201
  • 2
  • 18
  • 30
  • Context may be not available in the classs path, try add its jar file in the class path. also Context is a abstract class, you can extend it in to a anonymous class and use that... give more detail on the error and structure. – Dipak Jan 08 '13 at 11:57
  • try using EasyMock.Create instead of mock – Dipak Jan 08 '13 at 12:08
  • In build path, I have : - mockito-all-1.9.5.jar - Android 2.1 (with android.jar) - Andoird Dependencies - JUnit3 Normally, the class Context should be on library Android, no? – helene Jan 08 '13 at 13:42
  • @user1836315 Are you trying to build android junit, or normal unit test, that you try to run on your workstation? – Boris Strandjev Jan 08 '13 at 14:11
  • @BorisStrandjev, Sorry I'm not sure I understand your question. I have an other test class (in the same project) witch test an other simple class which hasn't android object. I can run this test class without problem – helene Jan 08 '13 at 14:17
  • @BorisStrandjev, Your question is if I run my class with Junit test or Android Junit test ? For this case, I run with Junit test. If I try to run with Android Junit test, I have the error : java.lang.NoClassDefFoundError: org.mockito.Mockito – helene Jan 08 '13 at 14:23
  • @user1836315 usually unit tests of Adnroid applications are meant to be run in a context of android device. This is why the android sdk related classes will not be automatically on your build path when you run ordinary junit tests. Thus `Context` is not recognized – Boris Strandjev Jan 08 '13 at 14:29
  • 1
    OK. I have succeed to resolve my problem by running as Android Junit test and hadding mockito-all-1.9.5.jar on a folder libs/ on my android test projet. # I have a new error which ma be caused by mockito : java.lang.ExceptionInInitializerError at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:85) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62) ... – helene Jan 08 '13 at 15:08

4 Answers4

87

Not sure if I had the same problem as you but I am using gradle and for some reason the tests just wouldn't run anymore, with the same error as you had. I tried cleaning and rebuilding but to no avail. After hours of frustration and trying to find an answer I came across the simple solution in a GitHub thread:

I resolved this issue by removing the .gradle folder in my project and rebuilding the project.

(thanks to vpetrov)

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
LoPoBo
  • 1,767
  • 1
  • 15
  • 26
32

You can run ./gradlew clean test in the terminal.

rcde0
  • 4,192
  • 3
  • 21
  • 31
20

Fixed the issue by following these steps --

1.Open module level build.gradle file, go to dependencies, go to this line --

testImplementation 'junit:junit:4.12'

2.Change the junit version to anything else below it (like 4.10)

testImplementation 'junit:junit:4.10'

3.Sync project

4.The issue fixed at this point in my case

5.Set the junit version back to what it was before (4.12 in my case) if you want

testImplementation 'junit:junit:4.12'

6.Sync project

Changing the junit version and syncing project worked in my case.

Mohit Atray
  • 452
  • 4
  • 15
0

Robolectric version 4.4 seems not to support jdk 14. So I could switch to jdk 13 or update robolectric to 4.5-alpha-1

Cavitedev
  • 613
  • 4
  • 17