Java 如何将ant JUnit任务用于
我正在尝试将JUnit测试自动化添加到我的CI服务器。我已经开发了所有需要运行的测试,并且可以在eclipse中本地运行它们。我正在使用Spring的测试库和JUnit4。我试图让CI服务器运行这些测试时遇到问题。ant似乎不允许在类路径上使用非jar文件。我的测试类依赖于它通过类路径找到的applicationContext:Java 如何将ant JUnit任务用于,java,hibernate,ant,spring-test,Java,Hibernate,Ant,Spring Test,我正在尝试将JUnit测试自动化添加到我的CI服务器。我已经开发了所有需要运行的测试,并且可以在eclipse中本地运行它们。我正在使用Spring的测试库和JUnit4。我试图让CI服务器运行这些测试时遇到问题。ant似乎不允许在类路径上使用非jar文件。我的测试类依赖于它通过类路径找到的applicationContext: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="test-config
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="test-config.xml")
@TransactionConfiguration(defaultRollback=true,transactionManager="transactionManager")
public class DAOTest {
<jdbc:embedded-database id="myDataSource" type="HSQL">
<jdbc:script location="tables.sql"/>
<jdbc:script location="testData.sql"/>
</jdbc:embedded-database>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.user_query_cache">false</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>resources/com/company/project/data/Session.hbm.xml</value>
<value>resources/com/company/project/data/HistoricalSession.hbm.xml</value>
<value>resources/com/company/project/data/RememberedData.hbm.xml</value>
</list>
</property>
</bean>
然后,该上下文配置文件引用一些要从类路径加载的sql文件:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="test-config.xml")
@TransactionConfiguration(defaultRollback=true,transactionManager="transactionManager")
public class DAOTest {
<jdbc:embedded-database id="myDataSource" type="HSQL">
<jdbc:script location="tables.sql"/>
<jdbc:script location="testData.sql"/>
</jdbc:embedded-database>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.user_query_cache">false</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>resources/com/company/project/data/Session.hbm.xml</value>
<value>resources/com/company/project/data/HistoricalSession.hbm.xml</value>
<value>resources/com/company/project/data/RememberedData.hbm.xml</value>
</list>
</property>
</bean>
最后,Hibernate会话工厂指向hbm.xml文件,当然是从类路径:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="test-config.xml")
@TransactionConfiguration(defaultRollback=true,transactionManager="transactionManager")
public class DAOTest {
<jdbc:embedded-database id="myDataSource" type="HSQL">
<jdbc:script location="tables.sql"/>
<jdbc:script location="testData.sql"/>
</jdbc:embedded-database>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.user_query_cache">false</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>resources/com/company/project/data/Session.hbm.xml</value>
<value>resources/com/company/project/data/HistoricalSession.hbm.xml</value>
<value>resources/com/company/project/data/RememberedData.hbm.xml</value>
</list>
</property>
</bean>
org.hibernate.dialogue.hsql方言
错误的
org.hibernate.cache.ehcache.EhCacheRegionFactory
符合事实的
错误的
错误的
20
参考资料/com/company/project/data/Session.hbm.xml
参考资料/com/company/project/data/HistoricalSession.hbm.xml
参考资料/com/company/project/data/membereddata.hbm.xml
由于我不能按照ant的限制在类路径上提供这些东西,有人知道应该如何连接这些东西吗
编辑:使用build.xml中的代码段进行更新。我已经为resources目录添加了pathelement,我试图引用的所有内容都存在于该目录中,但尚未使其正常工作
我的路径:
<path id="master-classpath">
<pathelement path="${project.classes.dir}" />
<fileset dir="${project.lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<path id="classpath.test">
<path refid="master-classpath"/>
<pathelement path="${project.test.dir}"/>
<fileset dir="${project.testLibs.dir}">
<include name="*.jar"/>
</fileset>
<pathelement path="resources"/>
</path>
测试目标:
<target name="do-test" if="execute.tests">
<pathconvert property="classpathProperty" refid="classpath.test"/>
<echo>Classpath is ${classpathProperty}</echo>
<delete dir="results"/>
<mkdir dir="results"/>
<mkdir dir="results/report"/>
<junit printsummary="yes" showoutput="yes" fork="true" failureproperty="test.failed">
<classpath refid="classpath.test"/>
<formatter type="xml"/>
<batchtest todir="results">
<fileset dir="${project.test.dir}">
<include name="**/*.java"/>
</fileset>
</batchtest>
<test name="com.company.project.test.DAOTest" todir="results"/>
</junit>
<junitreport todir="results/report">
<fileset dir="results" includes="*.xml"/>
<report todir="results/report"/>
</junitreport>
<fail message="test failed" if="test.failed"/>
</target>
类路径为${classpathProperty}
我每次看到的失败总是“类路径资源[文件]无法打开,因为它不存在”我认为您需要找出ant配置中的错误。如果你做得对的话,那一定行得通。将您的任务定义添加到问题中我已从my build.xml添加了详细信息。要在生产环境中运行应用程序,每个hbm.xml文件和applicationContext.xml都列在类路径中。我本想这样做的,但蚂蚁禁止。我一直在琢磨如何绕过这个问题。显示了我不能在类路径中直接列出xml文件本身的意思