提问者:小点点

JUnit测试仅在重命名测试类名时有效


我正在使用junit4运行测试。我的测试类如下所示-

@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>

共1个答案

匿名用户

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

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

这个答案揭示了这些插件之间的差异。

如果您想拆分测试(单元

<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验证(或mvn install)时,它应该识别您现有的ProductIT

有关此类测试默认值和标准的更多信息,请参阅使用Maven测试Java应用程序的指南。