Question

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:

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

I suppose, it misses junit.jar file. I added it into my ANT_HOME/lib directory, but it didn't have any effect. ant-junit.jar library was already in "ANT_HOME/lib", I didn't need to copy it over.

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 also let eclipse to generate a build.xml to be and merged it with the file generated by android update project. It didn't make any difference, I receive the same error.

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.

build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="FSTest" default="help">
    <property file="local.properties" />
    <property file="ant.properties" />
    <property environment="env" />
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    </condition>
    <loadproperties srcFile="project.properties" />
    <fail
            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."
            unless="sdk.dir"
    />
    <import file="custom_rules.xml" optional="true" />
    <import file="${sdk.dir}/tools/ant/build.xml" />   
</project>

ant.properties:

tested.project.dir=../FS
external.libs.dir=libs

project.properties:

target=android-11
Was it helpful?

Solution

The problem was in my build.xml file. I modified it according to the example given here: http://www.vogella.com/articles/ApacheAnt/article.html and thereafter it worked.

These lines were the ones that maked the difference:

  <path id="junit.class.path">
    <pathelement location="libs/junit.jar" />
    <pathelement location="${build.dir}" />
  </path>

OTHER TIPS

The JUnit task page lists several ways in which you can make the JUnit jar available to Ant:

http://ant.apache.org/manual/Tasks/junit.html

In particular, option 1 would require adding both "junit.jar" and "ant-junit.jar" to "ANT_HOME/lib". Any of the 5 options listed there should work just fine though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top