• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1430
  • Last Modified:

Running Android JUnit tests with Ant: ClassNotFoundException: org.junit.Test

I am attempting to get my JUnit tests for an Android application running using Ant. While using Eclipse, it is no problem, all is compiled and can be run with no problem.

At the moment I am receiving a ClassNotFoundException to org.junit.Test.

I am running the following commands

ant clean debug - success
ant uninstall - success
ant installt - success
ant test - failure

error log:

     [echo] Running tests ...
     [exec] INSTRUMENTATION_RESULT: shortMsg=java.lang.ClassNotFoundException
     [exec] INSTRUMENTATION_RESULT: longMsg=java.lang.ClassNotFoundException: org.junit.Test

I suppose, it misses junit.jar file. I added it into my ANT_HOME/lib directory, but it didn't have any effect.

I also tried to add it in the classpath of my test project and marked it to be exported. Also in that case the result remains unchanged.

I am sure that I am missing something very easy here. If further configuration details are needed, I'll be glad to provide them. I appreciate your help.
  • 3
1 Solution
mccarlIT Business Systems Analyst / Software DeveloperCommented:
What version of junit.jar did you place into the ANT_HOME\lib directory?

Also, can you post the contents of your build.xml file?
AndroidAndroidAuthor Commented:
Thank you for your reply, mccarl! I inserted JUnit 4.11 .jar.

My build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="FSTest" default="help">
    <property file="local.properties" />
    <property file="ant.properties" />
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    <loadproperties srcFile="project.properties" />
            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
    <import file="custom_rules.xml" optional="true" />
    <import file="${sdk.dir}/tools/ant/build.xml" />  
Your build file includes other files, so it's difficult to tell what's going on. Why don't you export the ant file (via the Export... choice of Eclipse) and modify it according to your needs?
AndroidAndroidAuthor Commented:
Thank you for the suggestion, girionis. Sadly, using the generated build.xml could not help me further. The error remained.
However, after adding the following lines to my build.xml, I got rid of the error message:

 <path id="junit.class.path">
    <pathelement location="lib/junit.jar" />
    <pathelement location="${build.dir}" />
AndroidAndroidAuthor Commented:
With this solution, I can run the tests with ant.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now