【问题标题】:Junit test works only when renamed test class nameJunit 测试仅在重命名测试类名称时有效
【发布时间】:2021-12-09 15:18:35
【问题描述】:

我正在使用 junit4 运行测试。我的测试类如下 test->java->com.example->ProductIT.java 当我在我的项目上运行 mvn clean install 时,无法识别以下测试并且无法运行。 但是,当我将测试类重命名为 ProductTest 时,它可以工作。 我不明白其中的区别。为什么我重命名时它会起作用?

@ContextConfiguration(locations = {"classpath:META-INF/spring/test-config.xml"})
@RunWith(SpringJunit4ClassRunner.class)
public class ProductIT{
   @Autowried
   private ProductServiceImpl serviceImpl;
   
   @Test
   public void test() throws Exception{
      assertNotNull(serviceImpl)
   }

}

pom.xml

<dependencyManagement>
   <dependency>
       <groupId>org.springframework.batch</groupId>
       <artifactId>spring-batch-core></artifactId>
       <version>4.2.4-RELEASE</version>
   </dependency>
   <dependency>
       <groupId>org.springframework.batch</groupId>
       <artifactId>spring-batch-infrastructure></artifactId>
       <version>4.2.4-RELEASE</version>
   </dependency>
</dependencyManagement>

<dependencies>

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-batch></artifactId>
<dependency>

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter></artifactId>
<dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test></artifactId>
     <scope>test</scope>
<dependency>
</dependencies>

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
</build>

【问题讨论】:

    标签: spring-boot junit integration-testing spring-test


    【解决方案1】:

    *IT 后缀(用于集成测试)是 Maven Failsafe 插件的默认测试标识符。 Spring Boot 项目默认不包含此插件,您必须将其显式添加到您的项目中。

    *Test 后缀是 Maven Surefire 插件的默认测试标识符,用作默认生命周期的 test 阶段的一部分:mvn test

    answer 更清楚地说明了这些插件之间的差异。

    如果您想拆分测试(单元和集成)并单独运行它们(第一个单元,然后是集成),请将 Maven Failsafe 插件添加到您的项目中:

    <project>
      
      <!-- other dependencies -->
     
      <build>
          <!-- further plugins -->
          <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.0.0-M5</version>
            <executions>
              <execution>
                <goals>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

    ...当您运行mvn verify(或mvn install)时,它应该识别您现有的ProductIT

    有关此类测试默认值和标准的更多信息,请参阅guide on testing Java applications with Maven

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      • 2013-12-24
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多