gpt4 book ai didi

spring - 使用 ant 目标进行 junit 测试和非 junit 测试

转载 作者:行者123 更新时间:2023-12-02 03:08:23 26 4
gpt4 key购买 nike

<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 来执行 ant 脚本。

分析:

1)如果我仅对扩展 Junit 测试的测试类运行批量测试,它执行得非常快。例如:

public class ImpactsParserTest extends TestCase{..

2)现在,如果我有一个测试类,它将 spring junit 测试扩展为:

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

尽管如此,我已在类路径元素中将这些 jar 文件和类文件指定为:

中的记录器 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-services\target\classes"/>
<pathelement location="${APP_DIR}\bgc-esw-web\target\classes" />

...

log4j.properties 存在于 ${TEST_BUILD_DIR}

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

最佳答案

当 JVM 的永久生成堆空间不足时,会发生此错误。虚拟机中的内存被划分为多个区域。 PermGen 就是这些区域之一。它是一个用于(除其他外)加载类文件的内存区域。该内存区域的大小是固定的,即当虚拟机运行时它不会改变。您可以使用命令行开关指定该区域的大小:-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
{
...
}

关于spring - 使用 ant 目标进行 junit 测试和非 junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6368694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com