"包不存在"使用Ant构建JUnit测试时出错

时间:2016-07-11 14:06:06

标签: java eclipse unit-testing ant

我正在尝试使用Ant执行单元测试。但是,我收到错误“包不存在”,即使我已经包含了包的类路径。

Eclipse中的文件夹结构

MyProject
  - src
    - com.project.core
      - MyApp.java
  - lib (contains all jar files and class files)

UnitTests
  -  src
    - com.project.core.tests
      - MyAppTest.java
  - lib (contains all jar files)
  - build.xml

的build.xml

<?xml version="1.0"?>
<project name="Learning TestNG" basedir="." default="build">

<property name="src.dir" value="C:/UnitTests/src/com/Project/core/tests/"/>
<property name="build.dir" value="build"/>
<property name="src.jars" value="C:/MyProject/lib"/>
<property name="unit.test.jars" value="C:/UnitTests/lib"/>


<!--Define classpath  -->

<path id="master-classpath">

    <fileset dir="${src.jars}">
        <include name="**/*.jar"/>
    </fileset>

    <fileset dir="${unit.test.jars}">
        <include name="**/*.jar"/>
    </fileset>

</path>

<!--Print out path  -->

<pathconvert pathsep="${line.separator}|   |-- "             
            property="echo.path.compile"             
            refid="master-classpath">
</pathconvert>

<echo>${echo.path.compile}</echo>

<!-- Main Build  -->

<target name="build" description="Compile source tree java files">
    <mkdir dir="${build.dir}"/>

    <javac destdir="${build.dir}" source="1.7" target="1.7" debug="on" includeantruntime="false" >
        <classpath refid="master-classpath"/>
        <src path="${src.dir}"/>
    </javac>
</target>
</project>

错误

[javac] Compiling 1 source file to C:/UnitTests/build
[javac] C:\MyProject\src\com\project\core\MyApp.java:14: error: package com.project.example does not exist
[javac] import com.project.example.AddNumbers;

当我正确地将类路径设置为.class文件时,我需要理解为什么会出现此错误?我尝试了一切。也许这是一个Ant问题?

1 个答案:

答案 0 :(得分:0)

更改src.dir

<property name="src.dir" value="C:/UnitTests/src"/>

源目录应该从包的根目录开始。