在测试中实例化多个弹簧启动应用程序
问题描述:
我有几个我的弹簧启动应用程序实例,它们并行地使用DB进行一些工作。每个实例都在单独的JVM中运行。
这是一种用Java编写测试用于在一个JVM上测试的方法吗?如下:在测试中实例化多个弹簧启动应用程序
- 设置一些嵌入式数据库为测试目的,甚至只是嘲笑它。我的春节,启动应用程序
- 的
- 开始2-5情况下等待一段时间
- 停止所有实例开始
- 验证DB,检查所有的条件都得到满足。
每个实例都有自己的上下文和类路径。
我认为我可以通过一些shell脚本实现,但我想用Java编写它。
这里最好的方法是什么?
答
您可以使用不同的端口多次运行它们。
我做了类似的
@RunWith(SpringJUnit4ClassRunner.class)
public class ServicesIntegrationTest {
private RestTemplate restTemplate = new RestTemplate();
@Test
public void runTest() throws Exception {
SpringApplicationBuilder uws = new SpringApplicationBuilder(UserWebApplication.class)
.properties("server.port=8081",
"server.contextPath=/UserService",
"SOA.ControllerFactory.enforceProxyCreation=true");
uws.run();
SpringApplicationBuilder pws = new SpringApplicationBuilder(ProjectWebApplication.class)
.properties("server.port=8082",
"server.contextPath=/ProjectService",
"SOA.ControllerFactory.enforceProxyCreation=true");
pws.run();
String url = "http://localhost:8081/UserService/users";
ResponseEntity<SimplePage<UserDTO>> response = restTemplate.exchange(
url,
HttpMethod.GET,
null,
new ParameterizedTypeReference<SimplePage<UserDTO>>() {
});
here源东西。
谢谢!有用。实例化Spring引导应用程序的2个副本,并且运行时没有问题。 – aleksei
你可能知道的一件事。我试图区分我的正在运行的应用程序的痕迹,我卡在这里。我试图添加像这样的属性 - “”logging.pattern.level = App-1“'/'”logging.pattern.level = App-2“”,但它总是显示第一个'SpringApplicationBuilder.properties' 。你知道它为什么会发生,什么是区分的正确方法? – aleksei
可能是2件事 - 记录器在配置属性应用之前被初始化。 OR属性源的优先级高于SpringApplicationBuilder的属性。我看到某处用于检索值但是现在找不到的源的顺序。尝试谷歌。 – StanislavL