2017-12-18 343 views
0

我有几个我的弹簧启动应用程序实例,它们并行地使用DB进行一些工作。每个实例都在单独的JVM中运行。
这是一种用Java编写测试用于在一个JVM上测试的方法吗?如下:在测试中实例化多个弹簧启动应用程序

  1. 设置一些嵌入式数据库为测试目的,甚至只是嘲笑它。我的春节,启动应用程序
  2. 开始2-5情况下等待一段时间
  3. 停止所有实例开始
  4. 验证DB,检查所有的条件都得到满足。

每个实例都有自己的上下文和类路径。
我认为我可以通过一些shell脚本实现,但我想用Java编写它。
这里最好的方法是什么?

回答

1

您可以使用不同的端口多次运行它们。

我做了类似的

@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源东西。

+0

谢谢!有用。实例化Spring引导应用程序的2个副本,并且运行时没有问题。 – aleksei

+0

你可能知道的一件事。我试图区分我的正在运行的应用程序的痕迹,我卡在这里。我试图添加像这样的属性 - “”logging.pattern.level = App-1“'/'”logging.pattern.level = App-2“”,但它总是显示第一个'SpringApplicationBuilder.properties' 。你知道它为什么会发生,什么是区分的正确方法? – aleksei

+0

可能是2件事 - 记录器在配置属性应用之前被初始化。 OR属性源的优先级高于SpringApplicationBuilder的属性。我看到某处用于检索值但是现在找不到的源的顺序。尝试谷歌。 – StanislavL

相关问题