在集成测试中启动两个 spring 引导应用程序
Launching two spring boot apps in integration test
我有一个 spring 引导集成测试。目前通过位于 src/test/resources.
的测试服务器应用程序 (MockServerApp.java) 进行测试 运行
我还想在启动集成测试之前启动我的 spring 启动应用程序 (App.java),这样它就可以连接并执行我将添加到集成测试而不是添加到集成测试中的 soap 调用手动 运行 主应用服务器,然后启动我的集成测试
我的 MockServerApp 配置为端口 9119。
我的应用程序(主应用程序)配置为端口 28433。
它们都在本地主机上 运行。
起初我尝试运行使用SpringBootTest
同时使用这两个应用程序
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,classes = {App.class,MockServerApp.class
})
public class BillingMediatorIT {
@Test
public void testOne(){
}
}
但这只会在端口 9119 上启动模拟服务器。
然后我尝试在这样的集成测试之前启动它
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,classes = {App.class,MockServerApp.class
})
public class BillingMediatorIT {
static ConfigurableApplicationContext context;
@BeforeClass
static public void setup(){
SpringApplication springApplication = new SpringApplicationBuilder()
.sources(App.class)
.build();
context = springApplication.run();
}
@Test
public void testOne(){
}
}
这给了我一个错误,虽然说地址已经在使用中。
我的test.properties
server.ssl.enabled=false
logging.config=classpath:logback-spring.xml
logging.file=messages
logging.file.max-size=50MB
logging.level.com.nulogix=DEBUG
billing.engine.address=127.0.0.1
billing.engine.port=9119
billing.engine.api.version=0.97
billing.engine.core.version=0.97
billing.engine.core.name=Patient_Responsibility
我的app.properties
server.port=28433
server.ssl.enabled=true
server.ssl.key-store=/Users/asluborski/Documents/mock.jks
server.ssl.key-store-password=Nul0gix
logging.config=classpath:logback-spring.xml
logging.file=messages
logging.file.max-size=50MB
logging.level.com.nulogix=DEBUG
billing.engine.address=127.0.0.1
billing.engine.port=9119
billing.engine.api.version=0.97
billing.engine.core.version=0.97
billing.engine.core.name=Patient_Responsibility
因此,在尝试并更改两个属性文件中的 server.ports 之后,我意识到集成测试仍然是 运行 我的 MockServerApp,尽管我的 spring 引导测试正在引导朝向主应用 class。我试过使用 属性 来源,但没有用。
我应该补充的是,我在 src/test/resources 和 src/main/resources 中的属性都被命名为 application.properties。我不确定这是否会导致冲突,但我只是从 main 复制了属性,然后更改了文件中的 server.port 。我不确定如何更改 .properties 的名称。
根据您的情况,这里有两条可能有所帮助的建议:
如果您只需要一个控制器进行通信,那么只需在您的测试目录中设置一个(即与其他测试相同的位置)并编写与该控制器通信的测试。您使用 SpringRunner/SpringBootTest 将打开此控制器,就好像它是用于测试目的的主应用程序的一部分。请注意,它将 运行 在与您的主应用程序相同的端口上,因此请确保它具有不同的端点以避免任何冲突。
如果你的 Mocked Server 更复杂,端点的配置是一个问题,或者你觉得你可能正在向这些服务添加多个连接(想想 nginx 服务器、数据库、消息传递等)以后测试,那我建议看Testcontainers。这允许您在 docker 容器中使用图像,您可以在集成测试中将其连接为资源。
我有一个 spring 引导集成测试。目前通过位于 src/test/resources.
的测试服务器应用程序 (MockServerApp.java) 进行测试 运行我还想在启动集成测试之前启动我的 spring 启动应用程序 (App.java),这样它就可以连接并执行我将添加到集成测试而不是添加到集成测试中的 soap 调用手动 运行 主应用服务器,然后启动我的集成测试
我的 MockServerApp 配置为端口 9119。 我的应用程序(主应用程序)配置为端口 28433。 它们都在本地主机上 运行。
起初我尝试运行使用SpringBootTest
同时使用这两个应用程序@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,classes = {App.class,MockServerApp.class
})
public class BillingMediatorIT {
@Test
public void testOne(){
}
}
但这只会在端口 9119 上启动模拟服务器。
然后我尝试在这样的集成测试之前启动它
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,classes = {App.class,MockServerApp.class
})
public class BillingMediatorIT {
static ConfigurableApplicationContext context;
@BeforeClass
static public void setup(){
SpringApplication springApplication = new SpringApplicationBuilder()
.sources(App.class)
.build();
context = springApplication.run();
}
@Test
public void testOne(){
}
}
这给了我一个错误,虽然说地址已经在使用中。
我的test.properties
server.ssl.enabled=false
logging.config=classpath:logback-spring.xml
logging.file=messages
logging.file.max-size=50MB
logging.level.com.nulogix=DEBUG
billing.engine.address=127.0.0.1
billing.engine.port=9119
billing.engine.api.version=0.97
billing.engine.core.version=0.97
billing.engine.core.name=Patient_Responsibility
我的app.properties
server.port=28433
server.ssl.enabled=true
server.ssl.key-store=/Users/asluborski/Documents/mock.jks
server.ssl.key-store-password=Nul0gix
logging.config=classpath:logback-spring.xml
logging.file=messages
logging.file.max-size=50MB
logging.level.com.nulogix=DEBUG
billing.engine.address=127.0.0.1
billing.engine.port=9119
billing.engine.api.version=0.97
billing.engine.core.version=0.97
billing.engine.core.name=Patient_Responsibility
因此,在尝试并更改两个属性文件中的 server.ports 之后,我意识到集成测试仍然是 运行 我的 MockServerApp,尽管我的 spring 引导测试正在引导朝向主应用 class。我试过使用 属性 来源,但没有用。
我应该补充的是,我在 src/test/resources 和 src/main/resources 中的属性都被命名为 application.properties。我不确定这是否会导致冲突,但我只是从 main 复制了属性,然后更改了文件中的 server.port 。我不确定如何更改 .properties 的名称。
根据您的情况,这里有两条可能有所帮助的建议:
如果您只需要一个控制器进行通信,那么只需在您的测试目录中设置一个(即与其他测试相同的位置)并编写与该控制器通信的测试。您使用 SpringRunner/SpringBootTest 将打开此控制器,就好像它是用于测试目的的主应用程序的一部分。请注意,它将 运行 在与您的主应用程序相同的端口上,因此请确保它具有不同的端点以避免任何冲突。
如果你的 Mocked Server 更复杂,端点的配置是一个问题,或者你觉得你可能正在向这些服务添加多个连接(想想 nginx 服务器、数据库、消息传递等)以后测试,那我建议看Testcontainers。这允许您在 docker 容器中使用图像,您可以在集成测试中将其连接为资源。