简体   繁体   English

是否可以以编程方式启动外部 spring-boot 应用程序

[英]Is it possible to start up an external spring-boot application programmatically

I have created a rest application using spring-boot 2.0.3.我使用 spring-boot 2.0.3 创建了一个休息应用程序。 From an other maven module (in a different multi module pom), I have an integration test that tests that data sent to the running rest application is processed.从另一个 maven 模块(在不同的多模块 pom 中),我有一个集成测试,用于测试发送到正在运行的 rest 应用程序的数据是否得到处理。

Is it possible to run this spring-boot application programatically?是否可以以编程方式运行这个 spring-boot 应用程序? I cannot use the simple @SpringBootTest-annotation as the spring-boot application is not in the same maven multi module.我不能使用简单的 @SpringBootTest-annotation,因为 spring-boot 应用程序不在同一个 maven 多模块中。

I would recommend using mockito to mock a request to your rest endpoint with some data, and testing that your other application tries to send the correct data to the endpoint.我建议使用 mockito 用一些数据模拟对您的休息端点的请求,并测试您的其他应用程序是否尝试将正确的数据发送到端点。

This way both applications are tested independently and do not have dependencies at each other.这样,两个应用程序都是独立测试的,彼此之间没有依赖关系。 This provides the benefit of being able to substitute one of these applications with another if necessary.这提供了能够在必要时用另一个应用程序替换这些应用程序之一的好处。 Also it provides a good separation.它还提供了良好的分离。 This is important because when you (or someone else) wants to use your REST application they do not use your other application so it is very important that the REST application is tested with static data written in your tests and is not dependent on the output of another application.这很重要,因为当您(或其他人)想要使用您的 REST 应用程序时,他们不会使用您的其他应用程序,因此使用测试中编写的静态数据测试 REST 应用程序并且不依赖于输出另一个应用程序。 Since REST applications are meant to be independent.因为 REST 应用程序是独立的。

However, when you do want to test it this way you could try to include your other application in the classpath.但是,当您确实想以这种方式对其进行测试时,您可以尝试将其他应用程序包含在类路径中。

The thing is "loose coupling".问题是“松耦合”。 It is technical possible, but not recommended.技术上可行,但不推荐。 The build itself has numerous tests using where mocking with Mockito is essential.构建本身有许多测试,使用 Mockito 模拟是必不可少的。

An integration test module, ala cucumber.io, should be created which will cover the functionality of the running module.应该创建一个集成测试模块 ala Caribbean.io,它将涵盖运行模块的功能。

This is the main-point of the accepted answer.这是已接受答案的要点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM