加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:19:37 所属栏目:大数据 来源:网络整理
导读:junit printsummary="on" fork="yes" forkmode="once" haltonerror="false" haltonfailure="false" failureproperty="junit.failure" showoutput="false" maxmemory="1024m" classpath path refid="CLASSPATH_JUNIT"/ dirset dir="${TEST_BUILD_DIR}"/ /clas

<junit printsummary="on" fork="yes" forkmode="once" 
haltonerror="false" haltonfailure="false" 
    failureproperty="junit.failure" showoutput="false" maxmemory="1024m">
    <classpath>
        <path refid="CLASSPATH_JUNIT"/> 
        <dirset dir="${TEST_BUILD_DIR}"/>
    </classpath>            
    <batchtest fork="no"  todir="${TEST_BUILD_DIR}">
         <fileset dir="${COMP_TEST_SRC}">                   
              <include name="**/*Test.java" />
              <include name="**/Test*.java" />
              <exclude name="**/EswTestCase.java" />
         </fileset>             
    </batchtest>    
    <formatter type="xml" />            
</junit>

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

Caught an exception while logging the end of the build.  Exception was:
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时,构建速度很快并且不会抛出内存不足.但是,它会引发如下错误:

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

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

和记录器罐中

<path id="CLASSPATH_JUNIT">
   <fileset dir="${BUILD_LIBS_HOME}">       
       <include name="*.jar" /> 
   </fileset>
   <pathelement location="${TEST_CLASSES_DIR}" />
   <pathelement location="${TEST_BUILD_DIR}" />
   <pathelement location="${COMP_BUILD}" />     
   <pathelement location="${COMP_CLASSES}" />   
   <path location="${APP_DIR}bgc-esw-servicestargetclasses"/> 
   <pathelement location="${APP_DIR}bgc-esw-webtargetclasses" />

$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,应使用注释.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class EswTestCase 
{
    ...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读