Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在一个JUnit测试中运行两个Spring Boot微服务?_Spring_Spring Boot_Junit_Spring Boot Test - Fatal编程技术网

如何在一个JUnit测试中运行两个Spring Boot微服务?

如何在一个JUnit测试中运行两个Spring Boot微服务?,spring,spring-boot,junit,spring-boot-test,Spring,Spring Boot,Junit,Spring Boot Test,我有两个Spring Boot REST微服务,我们称它们为A和B。我们还假设A对B进行REST调用 我想在一个JUnit测试中在A和B之间切换。也就是说,我想写一个测试来确保从a到B的通信和配置是有序的 我知道我可以在Spring的“内置”测试web服务器中使用测试中的注释运行单个微服务 是否可以以类似的方式在单个测试中同时运行A和B 我想通过从JUnit测试中删除@SpringBootTest并分别使用SpringApplicationBuilder实例化两个微服务,我已经成功了 A是dem

我有两个Spring Boot REST微服务,我们称它们为A和B。我们还假设A对B进行REST调用

我想在一个JUnit测试中在A和B之间切换。也就是说,我想写一个测试来确保从a到B的通信和配置是有序的

我知道我可以在Spring的“内置”测试web服务器中使用测试中的注释运行单个微服务


是否可以以类似的方式在单个测试中同时运行A和B

我想通过从JUnit测试中删除
@SpringBootTest
并分别使用
SpringApplicationBuilder
实例化两个微服务,我已经成功了

A是
demo1应用程序
,B是
demo2应用程序

@Test
public void test1() {
    HashMap<String, Object> props1 = new HashMap<>();
    props1.put("server.port", 8092);

    SpringApplicationBuilder builder1 = new SpringApplicationBuilder(Demo1Application.class);
    builder1
        .properties(props1)
        .run(new String[]{""});

    HashMap<String, Object> props2 = new HashMap<>();
    props2.put("server.port", 8093);

    SpringApplicationBuilder builder2 = new SpringApplicationBuilder(Demo2Application.class);
    builder2
    .properties(props2)
        .run(new String[]{""});
}
@测试
公共void test1(){
HashMap props1=新HashMap();
props1.put(“server.port”,8092);
SpringApplicationBuilder构建器1=新的SpringApplicationBuilder(Demo1Application.class);
建设者1
.properties(props1)
.run(新字符串[]{”“});
HashMap props2=新的HashMap();
props2.put(“server.port”,8093);
SpringApplicationBuilder builder2=新的SpringApplicationBuilder(Demo2Application.class);
建设者2
.properties(props2)
.run(新字符串[]{”“});
}

我认为JUnit的重点是单元测试而不是集成测试。谢谢@snap。我同意JUnit专注于单元测试,但这并不妨碍它成为所有其他测试的有用驱动程序,例如:使用Selenium的端到端测试。我问题的重点是如何在一个测试中运行Spring的测试web服务器的两个实例。