【问题标题】:ActiveProfile is not working in Junit5 test [duplicate]ActiveProfile 在 Junit5 测试中不起作用 [重复]
【发布时间】:2020-06-06 21:32:26
【问题描述】:

我想设置 Spring Profile 以测试不同的 JUnit 测试集。我试过这个:

application.properties:

spring.profiles.active=dev

测试 1

@IfProfileValue(name = "spring.profiles.active", values = {"test"})
@ActiveProfiles(profiles = {"test"})
public class SecondGenericTest {

    @Test
    public void secGenericTest() {

        System.out.println("Performing test ... for profile test");
    }
}

测试 2

@IfProfileValue(name = "spring.profiles.active", values = {"test"})
@ActiveProfiles(profiles = {"test"})
public class SecondGenericTest {

    @Test
    public void secGenericTest() {

        System.out.println("Performing test ... for profile test");
    }
}

也尝试设置到 InteliJ Active 配置文件中:

但是每次都执行两个测试。是否有一些解决方案可以使用正确的活动配置文件运行测试?

POM 配置:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-bean-validators</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>2.5.4</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
        <dependency>
            <groupId>plugin</groupId>
            <artifactId>org.plugin</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.10.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.10.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>woodstox-core-asl</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>
        <dependency>
            <groupId>io.reactivex.rxjava2</groupId>
            <artifactId>rxjava</artifactId>
        </dependency>
        <!--  Dependency for for Netty. Remove it when WebFlux is not used -->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-transport-native-epoll</artifactId>
            <version>4.1.45.Final</version>
            <classifier>linux-x86_64</classifier>
        </dependency>
        <!-- Packages for testing -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <version>2.0.4.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-engine</artifactId>
            <version>1.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.6.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>test</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.3</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Dependencies>jdk.unsupported</Dependencies>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>12</release>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
            </plugin>
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <repositories>
        <repository>
            <id>repository.spring.release</id>
            <name>Spring GA Repository</name>
            <url>http://repo.spring.io/release</url>
        </repository>
        <repository>
            <id>sonatype-releases</id>
            <url>https://oss.sonatype.org/content/repositories/releases/</url>
        </repository>
        <repository>
            <id>java.net</id>
            <url>https://maven.java.net/content/repositories/public/</url>
        </repository>
        <repository>
            <id>JBoss repository</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
        <repository>
            <id>jvnet-nexus-snapshots</id>
            <name>jvnet-nexus-snapshots</name>
            <url>https://maven.java.net/content/repositories/snapshots/</url>
        </repository>
    </repositories>

【问题讨论】:

  • 您在几个小时前问过完全相同的问题,但它已被关闭为重复。不要像这样向这个网站发送垃圾邮件。
  • 链接中的解决方案不起作用,所以我按照 SO 的建议打开了新问题。
  • 试试@IfProfileValue(name = "spring.profiles.active", value = "test")

标签: java spring spring-boot junit junit5


【解决方案1】:

正如 Sam Brannen 在answer 中所解释的:

如 Javadoc 中所述,@IfProfileValue 用于表示一个 为特定的测试配置文件或环境启用了测试。

@ActiveProfiles 用于声明哪个活动bean 加载 ApplicationContext 时应使用定义配置文件 用于测试类。

换句话说,您使用@IfProfileValue 来控制是否进行测试 类或测试方法将被执行或跳过,你使用 @ActiveProfiles 设置活动的 bean 定义配置文件 用于为您的测试加载 ApplicationContext。

所以在你的情况下@ActiveProfile 应该可以解决问题。

JUnit 5 还允许您向测试添加标签,该功能可以帮助您仅运行某些测试。

您可以通过使用@Tag("...") 注释来做到这一点,并将其设置为configuration 的参数,方法是将测试类型更改为您选择的标签

你也可以通过 maven 配置标签来构建/发布

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M3</version>
        <configuration>
            <!-- include tags -->
            <groups>integration, feature-168</groups>
            <!-- exclude tags -->
            <excludedGroups>slow</excludedGroups>
        </configuration>
    </plugin>

【讨论】:

  • @ActiveProfile?我明白了 - 无法解析符号“ActiveProfile”
  • @ActiveProfiles,你也应该使用 spring runner 运行你的测试,使用:@ExtendWith(SpringExtension.class)
  • 我尝试使用@ExtendWith(SpringExtension.class) @ActiveProfiles(profiles = {"test"}),但再次运行每个 JUnit;知道为什么可能会丢失吗?
  • 如果您只想排除某些测试,您可以使用 junit5 标签功能
  • 你能分享更多这是什么吗?
【解决方案2】:

我认为正确的语法应该是

@IfProfileValue(name = "spring.profiles.active", value = "test")

@IfProfileValue 注释附带的类的默认实现是 ProfileValueSource 类,它在系统属性中查找 - 因此您需要通过 JVM 属性设置配置文件,即 --Dspring.profiles.active="test"

很遗憾,@IfProfileValue 不考虑通过@ActiveProfiles@Profile 指定的值

Javadocs:

虽然@IfProfileValue 和@Profile 注解都涉及 配置文件,它们没有直接关系。 @Profile 涉及 bean 在环境中配置的定义配置文件;然而, @IfProfileValue 用于启用或禁用测试。

可以看到默认实现类at Github

在系统属性中查找键

@Override
public String get(String key) {
    Assert.hasText(key, "'key' must not be empty");
    return System.getProperty(key);
}

所以我们可以看到实际上我们不必专门设置spring profile,它可以是任何环境变量,可以通过System.getProperty(key);调用来解析,例如test.active.profile

或者您可以创建自己的 ProfileValueSource 实现:

正如@IfProfile 在系统属性中默认显示的那样,您需要提供ProfileValueSource 的实现以从任何属性源读取。请注意,下面的示例仍会查看系统属性 - 这只是自定义解析器的示例。

public class MyProfileValueSource implements ProfileValueSource {
  private final Properties testProperties;

  public MyProfileValueSource() {

      ClassPathResource resource = new ClassPathResource("test.properties");
      if (resource.exists()) {
          try {
              this.testProperties = PropertiesLoaderUtils.loadProperties(resource);
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      } else {
          testProperties = new Properties();
      }
  }

  @Override
  public String get(String key) {
      return testProperties.getProperty(key, System.getProperty(key));
  }

然后在你的测试类中引用它

@ExtendWith(SpringRunner.class)
@ContextConfiguration(classes = MyApplicationConfig.class)
@ProfileValueSourceConfiguration(MyProfileValueSource.class)
public class ServiceTests {

  @Test
  @IfProfileValue(name = "test.enabled", value = "true")
  public void test() {

  }
}

您可以提供您需要的任何ProfileValueSource 实现(即从任何其他属性源读取)

【讨论】:

  • 我试过@SpringJUnitConfig @ActiveProfiles("test"),但是JUnit测试被执行了。
  • 这需要时间。顺便说一下,我上面贴的差不多。我将添加 maven 依赖项。
  • ActiveProfiles 只会控制哪些 bean 被加载到上下文中。如果您需要定义要运行的测试,请使用 @IfProfileValue。
  • @PeterPenzov 请参阅上面的更新答案。
猜你喜欢
  • 1970-01-01
  • 2013-08-29
  • 1970-01-01
  • 2021-08-05
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 2018-05-12
  • 2017-10-27
相关资源
最近更新 更多