Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ActiveProfile在Junit5测试中不工作_Java_Spring_Spring Boot_Junit_Junit5 - Fatal编程技术网

Java ActiveProfile在Junit5测试中不工作

Java ActiveProfile在Junit5测试中不工作,java,spring,spring-boot,junit,junit5,Java,Spring,Spring Boot,Junit,Junit5,我想设置Spring概要文件,以便测试不同的JUnit测试集。我试过这个: application.properties: spring.profiles.active=dev 测试1 @IfProfileValue(name = "spring.profiles.active", values = {"test"}) @ActiveProfiles(profiles = {"test"}) public class SecondGenericTest { @Test pub

我想设置Spring概要文件,以便测试不同的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活动配置文件:

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

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>

org.springframework.boot
spring启动程序父级
2.2.4.1发布
伊奥·斯普林福克斯
springfox招摇过市用户界面
2.9.2
伊奥·斯普林福克斯
springfox-Swagger 2
2.9.2
伊奥·斯普林福克斯
springfoxbean验证程序
2.9.2
org.mariadb.jdbc
mariadb java客户端
2.5.4
org.projectlombok
龙目
1.18.12
插件
org.plugin
1
javax.servlet
javax.servlet-api
假如
com.fasterxml.jackson.core
杰克逊注释
2.10.2
编译
com.fasterxml.jackson.jaxrs
jackson jaxrs json提供程序
2.10.2.1
org.codehaus.woodstox
woodstox core asl
4.4.1
javax.xml.bind
jaxb api
2.3.1
com.sun.xml.bind
jaxb内核
2.3.0.1
com.sun.xml.bind
jaxb impl
2.3.2
javax.activation
激活
1.1.1
org.springframework.boot
弹簧靴起动器
org.springframework.boot
SpringBootStarterWeb
爪哇
JavaEEAPI
8.0.1
假如
org.springframework.boot
弹簧启动机amqp
com.fasterxml.jackson.core
杰克逊数据绑定
2.10.2
org.springframework.boot
弹簧启动启动器验证
org.springframework.boot
弹簧启动器webflux
org.springframework.boot
弹簧启动安全
org.springframework.session
春季会议核心
io.reactivex.rxjava2
rxjava
伊奥·内蒂
netty传输本地epoll
4.1.45.最终版本
linux-x86_64
org.junit.jupiter
朱尼特木星发动机
5.6.0
测试
org.junit.jupiter
JUnitJupiter api
5.6.0
测试
org.mockito
莫基托磁芯
3.2.4
测试
org.springframework.boot
弹簧起动试验
测试
org.junit.jupiter
朱尼特朱庇特酒店
5.6.0
测试
org.springframework.restdocs
SpringRestMVC
2.0.4.1发布
测试
org.junit.platform
junit平台引擎
1.6.0
org.junit.platform
junit平台共享
1.6.0
org.junit.platform
junit平台发射器
1.6.0
测试
测试
org.springframework.boot
springbootmaven插件
maven战争插件
3.2.3
jdk.com不受支持
org.apache.maven.plugins
maven编译器插件
3.8.1
12
${project.build.sourceEncoding}
org.apache.maven.plugins
maven资源插件
3.1.0
${project.build.sourceEncoding}
org.apache.maven.plugins
maven surefire插件
3.0.0-M4
UTF-8
repository.spring.release
Spring GA存储库
http://repo.spring.io/release
sonatype版本
https://oss.sonatype.org/content/repositories/releases/
java.net
https://maven.java.net/content/repositories/public/
JBoss存储库
http://repository.jboss.org/nexus/content/groups/public/
jvnet-nexus快照
jvnet-nexus快照
https://maven.java.net/content/repositories/snapshots/

如Sam Brannen在本节中所述:

正如Javadoc中所述,
@IfProfileValue
用于指示 为特定测试配置文件或环境启用测试

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

换句话说,您使用
@
<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>
@IfProfileValue(name = "spring.profiles.active", value = "test")
@Override
public String get(String key) {
    Assert.hasText(key, "'key' must not be empty");
    return System.getProperty(key);
}
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() {

  }
}