春季-蚂蚁目标的junit测试和非junit测试

前端之家收集整理的这篇文章主要介绍了春季-蚂蚁目标的junit测试和非junit测试 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

  1. <junit printsummary="on" fork="yes" forkmode="once"
  2. haltonerror="false" haltonfailure="false"
  3. failureproperty="junit.failure" showoutput="false" maxmemory="1024m">
  4. <classpath>
  5. <path refid="CLASSPATH_JUNIT"/>
  6. <dirset dir="${TEST_BUILD_DIR}"/>
  7. </classpath>
  8. <batchtest fork="no" todir="${TEST_BUILD_DIR}">
  9. <fileset dir="${COMP_TEST_SRC}">
  10. <include name="**/*Test.java" />
  11. <include name="**/Test*.java" />
  12. <exclude name="**/EswTestCase.java" />
  13. </fileset>
  14. </batchtest>
  15. <formatter type="xml" />
  16. </junit>

这会花费大量时间来生成xml报告,并且会出现以下错误

  1. Caught an exception while logging the end of the build. Exception was:
  2. java.lang.OutOfMemoryError: PermGen space

为什么要花很长时间才能生成XML?如何解决错误并使应用程序快速运行.我最多只能有10个测试文件.我使用命令promt执行蚂蚁脚本.

分析:

1)如果我仅对扩展了Junit测试的测试进行批处理测试,则执行速度非常快.例如:

public class ImpactsParserTest extends
TestCase{..

2)现在,如果我有将弹簧单元测试扩展为的测试类:

public class AddressLookupServiceTest
extends EswTestCase{..

public class EswTestCase extends
AbstractDependencyInjectionSpringContextTests{..

这会导致junit目标运行非常缓慢,并导致内存不足错误.为什么会这样发生?

3)当我使batchtest fork =“ yes”而不是no时,构建速度很快并且不会抛出内存不足.但是,它会引发如下错误

  1. java.lang.NoClassDefFoundError
  2. at org.apache.log4j.Logger.getLogger(Logger.java:118)
  3. ..
  4. java.lang.NoClassDefFoundError: com.bgc.ordering.wizard.back.services.EswTestCase

即使,我已经在classpath元素中将这些jar文件和类文件指定为:

和记录器罐中

  1. <path id="CLASSPATH_JUNIT">
  2. <fileset dir="${BUILD_LIBS_HOME}">
  3. <include name="*.jar" />
  4. </fileset>
  5. <pathelement location="${TEST_CLASSES_DIR}" />
  6. <pathelement location="${TEST_BUILD_DIR}" />
  7. <pathelement location="${COMP_BUILD}" />
  8. <pathelement location="${COMP_CLASSES}" />
  9. <path location="${APP_DIR}\bgc-esw-services\target\classes"/>
  10. <pathelement location="${APP_DIR}\bgc-esw-web\target\classes" />

$4.{TEST_BUILD_DIR}上的log4j.properties

使用:apache-ant-1.8.1和junit-3.8.1.jar

最佳答案
当JVM永久生成堆中的空间不足时,会发生此错误.虚拟机中的内存分为多个区域.这些区域之一是PermGen.这是一个用于(除其他外)加载类文件的内存区域.此内存区域的大小是固定的,即在VM运行时不会改变.您可以使用命令行开关指定该区域的大小:-XX:MaxPermSize.在Sun VM上,默认值为64 Mb.要解决此问题,您可以为其提供更高的值,例如256mb.

我的猜测是,您不仅要运行单元测试,还要运行集成测试,例如您有一个与Spring关联的类,并且需要它们的依赖关系.这就是为什么要使用EswTestCase.如果您只想编写单元测试,建议您实例化您的类,并模拟与您未直接测试的其他类的依赖关系.这将最大限度地减少内存占用,因为您不必创建Spring应用程序上下文.

这就是JavaDoc关于AbstractDependencyInjectionSpringContextTests的说法:

Really for integration testing,not
unit testing. You should not normally
use the Spring container for unit
tests: simply populate your POJOs in
plain JUnit tests!

从Spring 3.0开始,旧版JUnit 3.8基类层次结构(即AbstractDependencyInjectionSpringContextTests,AbstractTransactionalDataSourceSpringContextTests等)已正式弃用,并将在以后的版本中删除.建议您使用Spring TestContext Framework编写集成测试.而不是使用AbstractDependencyInjectionSpringContextTests扩展EswTestCase,应使用注释.

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration
  3. public class EswTestCase
  4. {
  5. ...
  6. }

猜你在找的Spring相关文章