【问题标题】:During Maven Build Junit Tests runs fine, but why they fails, if I runs JUnit test manually?在 Maven 构建期间,Junit 测试运行良好,但是如果我手动运行 JUnit 测试,为什么它们会失败?
【发布时间】:2014-08-11 11:30:11
【问题描述】:

我的 Maven 项目有一些问题。我在我的 maven 文件夹 src/test/java 中创建了一些 JUnit 测试。

如果我在命令行或 Eclipse IDE 中使用“mvn clean install”创建了我的 maven 项目,并使用“clean”和“install”等运行配置,所有测试都将运行良好并且不会出现错误。

但如果我手动运行 JUnit 测试,这些 Junit 测试之一会失败。但是原因是什么,为什么 maven 不承认这个事实呢?这是我的 maven pom.xml 文件的代码 sn-p:

    <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”为空。但是为什么以及如何解决这个问题呢?

【问题讨论】:

  • 我使用 run 作为“Junit”来启动我的 Junit 测试课程。当我收到错误时,它是从 Assert 类创建的:预期 但是:
  • 我们将需要您失败的测试的代码,并且可能需要它正在测试的代码。
  • 我添加了一些新信息

标签: java maven


【解决方案1】:

您的这个测试很可能取决于另一个测试的执行,该测试会生成一些文件、其他资源或将数据存储在某处(例如数据库或其他地方)。 TDD(测试驱动开发)规定你编写的任何测试都应该是它们自己独立的实体,这意味着:你编写的任何测试都应该是完全自给自足的,而不是依赖于之前执行的其他测试产生的东西。

简单地说:确保您的测试在其setUp() 方法中生成了所有正确的资源/数据,或者在您的测试执行之前,Maven 复制了所需的测试资源。

调试依赖于其他测试用例生成的资源/数据的测试用例绝不是一个好主意,因为在某个时间点,您会忘记另一个测试正在干预您的资源,并且会想知道为什么结果不是你所期望的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-08
    • 2013-08-23
    • 2016-03-09
    • 2018-06-18
    • 2010-09-30
    • 1970-01-01
    • 2011-08-30
    相关资源
    最近更新 更多