SpringBoot(SpringCloud) Junit Test 测试类指定使用的yml配置文件

    最近遇到了一个需求,需要在 JunitTest 中,解析excel,并导入到数据库。项目的系统已经微服务化了,拥有 poi 的 jar 包的微服务 和 拥有数据库访问API 的微服务不是同一个。微服务项目的 appplication.yml 拥有多个不同的 profile (application-dev.yml application-test.yml  等等),在自己写的 JunitTest 类中,需要显示的指定生效的 profile。

    话不多说,直接上代码。

@RunWith(SpringRunner.class)
/**  指定当前生效的配置文件( active profile),如果是 appplication-dev.yml 则 dev   **/
@ActiveProfiles("dev")

/** 指定  @SpringBootApplication  启动类 和 端口  **/
@SpringBootTest(classes = FileManagementServiceApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ExcelParseTest {

	private Logger logger = LoggerFactory.getLogger( ExcelParseTest.class );



	@Value("${logging.level.root}")
	private String level ;

	@LocalServerPort
	private String port;

	@Test
	public void myTest(){
		System.out.println("11111");
		System.out.println("\n level :"+  level +"\n");
		System.out.println("\n port :"+  port +"\n");

    }
}

运行效果,junit 测试类 成功的获取了 application-dev.yml 中配置得 日志级别 和 端口

2018-12-04 09:46:57,983 60777 INFO  [main] org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer - Tomcat started on port(s): 8814 (http)
2018-12-04 09:46:57,984 60778 INFO  [main] o.s.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration - Updating port to 8814
2018-12-04 09:46:58,055 60849 INFO  [main] hzjtest.ExcelParseTest - Started ExcelParseTest in 57.618 seconds (JVM running for 63.995)
11111

 level :INFO


 port :8814

猜你喜欢

转载自blog.csdn.net/u012688704/article/details/84783191