依次执行/运行SpringBootTest作为单独的过程

问题描述

我有两个依赖于两个不同属性文件的Spring Boot Test类。我想按两个独立的Java流程顺序并独立地执行它们。您能否让我知道如何实现这一目标?使用pom.xml配置,它按顺序运行,但是从新属性文件(即application_2.properties)更新环境变量()(未更新UserDataHolder.WRAPENCVALUE)。下面是代码段。

@RunWith(SpringJUnit4ClassRunner.class)
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT,classes=PiiApplication.class)
@ComponentScan("com.test")
@TestPropertySource("classpath:application_1.properties")
class PiiApplicationTest {
  @Test
  void contextLoads() {
  }
}




@RunWith(SpringJUnit4ClassRunner.class)
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT,classes=PiiApplication.class)
@ComponentScan("com.test")
@TestPropertySource("classpath:application_2.properties")
class PiiApplicationWithEncTest {
 @Test
 void contextLoads() {
 }
}

@Slf4j
public class UserDataHolder {

    private static ThreadLocal<Map<String,Object>> _userData = ThreadLocal.withInitial(() -> new HashMap<>());

    private static Environment env = AppContext.getBean(Environment.class);

    public static final String ENCKEY = "enckey";
    
    static {
        if(null == _userData.get().get(ENCKEY)) {
            _userData.get().put(ENCKEY,KeyManager.getKey());
        }
    }

    private static final boolean WRAPENCVALUE = null != env.getProperty("app.pii.settings.wrapencvale")? Boolean.parseBoolean(env.getProperty("app.pii.settings.wrapencvale")):false;
    private static final String ENCKEYVALUE = null != env.getProperty("app.pii.settings.enckey")?env.getProperty("app.pii.settings.enckey"):"enc";
    private static final String ENCWRAPPER = null != env.getProperty("app.pii.settings.encwrapper")?env.getProperty("app.pii.settings.encwrapper"):"ENC(";
    
    //other methods
    
}

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <configuration>
        <runOrder>alphabetical</runOrder>
        <argLine>-Xmx1024m -XX:MaxPermSize=256m ${jacoco.agent.it.arg}
        </argLine>
        <!-- Specific to generate mapping between tests and covered code -->
        <properties>
            <property>
                <name>listener</name>
                <value>org.sonar.java.jacoco.JUnitListener</value>
            </property>
        </properties>
        <!-- Let's put failsafe reports with surefire to have access to tests 
            failures/success reports in sonar -->
        <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
    </configuration>
    <executions>
       <execution>
          <id>integration-test</id>
          <goals>
        <goal>integration-test</goal>
          </goals>
        </execution>
      <execution>
          <id>verify</id>
          <goals>
        <goal>verify</goal>
          </goals>
        </execution>
    </executions>
</plugin>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...