项目中会有很多的配置直接在启动命令行中,比如Idea的VM options,
-D
,-XX
等配置
,这些配置往往影响到项目的运行,比如apollo配置,但是在使用springboot和junit左单元测试时,每次开启一个新的测试用例都需要将命令复制一遍,很麻烦
这时可以使用maven-surefire-plugin
进行通用配置
具体用法
pom.xml
此例:比如说读取apollo配置
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- 主要添加下面的plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-Denv=FAT
-Dapollo.cluster=test
-Dapollo.meta=http://127.0.01:8080
</argLine>
</configuration>
</plugin>
java代码测试
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan(basePackages = {"cn.samubo.boot"})
@SpringBootTest(classes = Application.class)
public class BaseTest {
@Autowired
private ApplicationContext applicationContext;
@Before
public void init() {
}
@Test
public void testEnv() {
Environment environment = applicationContext.getEnvironment();
System.out.println("env: " + environment.getProperty("env"));
System.out.println("apollo.cluster:" + environment.getProperty("apollo.cluster"));
System.out.println("apollo.meta:" + environment.getProperty("apollo.meta"));
}
}
此时就可以读取出来值了
maven-surefire-plugin:
还有很多配置:
https://www.shuzhiduo.com/A/xl56w8m9dr/