![](/img/trans.png)
[英]Maven runs JUnit tests in Eclipse but compilation fails from the command line
[英]During Maven Build Junit Tests runs fine, but why they fails, if I runs JUnit test manually?
我的Maven项目有一些问题。 我已经在我的maven文件夹src / test / java中创建了一些JUnit测试。
如果我在命令行上或在Eclipse IDE中使用“ mvn clean install”创建了maven项目,并且运行配置如“ clean”和“ install”,则所有测试将正常运行,并且不会发生任何错误。
但是,如果我手动运行JUnit测试,则这些Junit测试之一将失败。 但是,为什么Maven无法识别这个事实是什么原因呢? 这是我的maven pom.xml文件的代码片段:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<optimize>true</optimize>
<fork>true</fork>
<source>${source.jdk}</source>
<target>${target.jdk}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifestEntries>
<Source-JDK>${source.jdk}</Source-JDK>
<Target-JDK>${target.jdk}</Target-JDK>
<Project-Build-SourceEncoding>${project.build.sourceEncoding}</Project-Build-SourceEncoding>
<Maven-Build-Timestamp>${maven.build.timestamp}</Maven-Build-Timestamp>
</manifestEntries>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${project.groupId}.${project.artifactId}.MainClass</mainClass>
</manifest>
</archive>
<appendAssemblyId>false</appendAssemblyId>
<classifier>jar-with-dependencies</classifier>
</configuration>
<executions>
<execution>
<id>assembly-jar-Id</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
这是失败的Junit代码:
public void testProperties(){
String build_time = Parameters.BUILD_TIME.getParameter();
Map<String, String> allProperties = MetaInformation.Properties();
for(Map.Entry<String, String> currentEntry : allProperties.entrySet()){
if(currentEntry.getKey().equals(build_time)){
String project_build_value = currentEntry.getValue();
Assert.assertEquals(true, build_time_value != null && build_time_value.equals(""));
}
}
}
属性文件包含以下值:
buildTime=${timestamp}
encoding=${project.build.sourceEncoding}
正在读取值“ buildTime”的Junit测试失败。 “时间戳”是一个值,它基于我的maven项目的pom.xml。 它为我提供以下属性:
<properties>
<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>dd.MM.yyyy HH:mm</maven.build.timestamp.format>
</properties>
$ {maven.build.timestamp}表示时间点,我的maven项目是使用命令mvn clean install构建的。 读取此值的JUnit测试会造成一些麻烦,因为在执行此JUnit测试期间,变量“ project_build_value”为空。 但是,为什么以及如何解决这个问题?
最有可能的是,对您的这种测试取决于另一种测试的执行,该测试产生一些文件,其他资源或将数据存储在某个地方(例如,在数据库中或其他内容中)。 TDD(测试驱动程序开发)指出,您编写的任何测试都应该是它们自己的独立实体,这意味着:您编写的任何测试都应该完全自给自足,并且不依赖于之前执行的其他测试所产生的结果。
简而言之:请确保您的测试在其setUp()
方法中具有所有正确的资源/数据,或者确保在执行测试之前Maven复制了所需的测试资源。
调试依赖于其他测试用例产生的资源/数据的测试用例绝不是一个好主意,因为在某个时间点,您会忘记另一个测试正在干扰您的资源,并且会想知道为什么结果不是这样?你在期待
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.