我试着用eclipse和ANT运行JUNIT测试,他们都抱怨找不到org.h2Driver类。
我的类路径中有h2-1.3.164.jar,为了证明这一点,这里有系统属性java.class.path 中的类路径
c:trasfererbuild;
C:Program Files (x86)IBMIMSharedpluginsorg.junit4_4.3.1junit.jar;
c:trasfererlibojdbc6.jar;
c:trasfererlibsqljdbc4.jar;
c:trasfererlib-testh2-1.3.164.jar;
c:trasfererlib-testjunit-4.8.2.jar;
c:trasfererlib-testlog4j-1.2.14.jar;
c:trasfererlib-testTestUtils.jar;
c:trasfererlib-testdbunit-2.4.8.jar;
c:trasfererlib-testslf4j-api-1.5.5.jar;
c:trasfererlib-testslf4j-log4j12-1.5.5.jar;
当我用eclipse打开它时,我可以从h2-1.3.164.jar中找到org.h2.Driver类。用于运行测试的Ant任务和用于"构建"类路径的Ant任务
<path id="lib.classpath">
<fileset dir="./lib" includes="*.jar" />
</path>
<path id="test.classpath">
<fileset dir="lib" includes="*.jar" />
<fileset dir="lib-test" includes="*.jar" />
<pathelement location="${dest}" />
<pathelement location="${test-dest}" />
</path>
<target name="run-test" depends="init-test,compile,compile-test">
<property name="classPathToUse" refid="test.classpath" />
<echo>Using classpath: "${classPathToUse}"</echo>
<junit failureProperty="test.failure" fork="on" forkmode="once" outputtoformatters="true" printsummary="on" showoutput="true">
<jvmarg value="-Xmx512m" />
<jvmarg value="-XX:MaxPermSize=128m" />
<jvmarg value="-Duser.timezone=GMT" />
<jvmarg value="-Dexternal-properties=${test.properties}" />
<classpath refid="test.classpath" />
<formatter type="brief" usefile="false" />
<formatter type="xml" />
<batchtest todir="test-results">
<fileset dir="${test-dest}" includes="**/*Test.class" />
</batchtest>
</junit>
<fail message="test failed" if="test.failure" />
</target>
这是堆垛机
[junit] ------------- Standard Error -----------------
[junit] log4j:ERROR Could not find value for key log4j.appender.CONSOLE
[junit] log4j:ERROR Could not instantiate appender named "CONSOLE".
[junit] log4j:WARN No appenders could be found for logger (org.dbunit.DatabaseTestCase).
[junit] log4j:WARN Please initialize the log4j system properly.
[junit] ------------- ---------------- ---------------
[junit] Testcase: testBuildLiityntapisteSQL(org.xyz.rigistry.manager.XXServiceImplTest): Caused an ERROR
[junit] org.h2.Driver
[junit] java.lang.ClassNotFoundException: org.h2.Driver
[junit] at java.lang.Class.forName(Class.java:136)
[junit] at org.dbunit.JdbcDatabaseTester.(JdbcDatabaseTester.java:104)
[junit] at org.dbunit.PropertiesBasedJdbcDatabaseTester.(PropertiesBasedJdbcDatabaseTester.java:68)
[junit] at org.dbunit.DBTestCase.newDatabaseTester(DBTestCase.java:70)
[junit] at org.dbunit.DatabaseTestCase.getDatabaseTester(DatabaseTestCase.java:109)
[junit] at org.dbunit.DatabaseTestCase.setUp(DatabaseTestCase.java:151)
[junit] at fi.transferer.junit.AbstractDatabaseTestCase.setUp(Unknown Source)
[junit] at fi.transferer.junit.BasicDatabaseTestCase.setUp(Unknown Source)
有人能告诉我发生了什么事吗?
我认为您应该简单地将这些jar添加到Eclipse中的项目构建路径中。
您能发布运行junit的ANT构建文件部分吗?有一些选项可以关闭CLASSPATH环境变量的使用。。。。
依赖环境变量会降低构建的可移植性,根据我的经验,这是管理java依赖关系的一种非常不灵活的方式。我注意到您已经遇到了同一个库的多个版本(junit和log4j的两个副本)的问题。
我建议修改您的构建,在build.xml文件的顶部声明类路径,如下所示:
<path id="compile.path">
<fileset dir="c:trasfererlib" includes="*.jar"/>
</path>
<path id="test.path">
<path refid="compile.path"/>
<fileset dir="c:trasfererlib-test" includes="*.jar"/>
</path>
然后,这些类路径可以在各种蚂蚁任务中使用,使用classpathref属性:
<javac srcdir="${src}"
destdir="${build}"
classpathref="compile.path"
debug="on"
source="1.4"
/>
junit具有嵌套的类路径功能(对于添加已编译的类目录很有用):
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement location="${build.tests}"/>
<path refid="test.path"/>
</classpath>
<formatter type="plain"/>
<test name="my.test.TestCase" haltonfailure="no" outfile="result">
<formatter type="xml"/>
</test>
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>