How to verify that a spring-boot web server is running - java

I am writing an integration test for a spring-boot web server
I want to use junit-jupiter (junit5) to check if the server is running using Assumptions.assumeTrue so that the test do not fail if the server is not running...
There seems not to be any API support for such operation, but can it be achieved in some other way? Pinging the server?

You could make use of the actuator module in spring boot. It will provide you with a health check URL and you could make a call to this URL and verify that it returns healthy.
Now that that is out of the way, if you need the server to be running while running your tests, you should probably be using the spring boot test runner, instead of using the JUnit runner. In this case you could still mock some of your spring beans and achieve the same kind of test, but with a spring context running alongside your tests. The downside of this is the overhead of needing to spin up the spring context before running tests, but if you need to use the actual web server, then this is what you should be doing.

Related

Environment and Project setup for Rest Assured and Sprint Boot Application

I have a spring boot microservice which connects to MySQL Database on production. This project also exposes REST endpoints and also consumes a REST endpoint from another microservice. It also connects to Kafka.
Now what I want to understand if I write REST Assured test cases using cucumber then where does this test cases will be kept. In the same spring boot microservice or code or it should be independent of that. If it should be kept independent and then how to achieve that. What is best practices ?
I am thinking of using WireMock to mock the endpoint which this microservice consumes. I am thinking of using EmbeddedKafka to connect it to Kafka.
How all of this thing will happen in CICD ?

Integration Tests for AWS Kinesis consumer built using Spring Boot

We are working on Micro service developed using spring boot which consumes data from Kinesis stream process it and then stores it in DB. We have good JUnit Mockito test coverage .Now we need Integration test on this . Can you please suggest a good framework or set of frameworks which can be used to test the entire application end to end . We have checked so far Citrus, and spring-integration-aws but no luck
Generally you can use localstack to run AWS services like Kinesis locally, and start them up from your integration tests using Docker commands or something like Testcontainers.
Depending on how you have everything setup, you will need to inject a different Amazon Kinesis Client when the tests are running so that they connect to the local AWS services.

Spring Boot IT test after Deploy

I have Spring Boot Integration tests (IT) that connect to a real DB or to a real 3rd parties. I use them during development but I find them quite usefull to check the real behaviour of the application therefore I would like to run them during CI process. The goal is to run them on the environment on which the application is deployed and not on CI machine where Jenkins is running. Is there a way how to achieve this? I know I can use for example SOAP UI maven plugin and execute tests against REST endpoints, but I would prefer to use Spring Boot IT tests already written.
Many thanks
Running tests against your production database is a really bad idea. Please please please reconsider. It is better to have your test database updated to be more like production than to run your tests on production data.
That being said, you can point your database configuration to your production machine via the application.properties file (mongo example):
spring.data.mongodb.uri=mongodb://user:pass#production.myhost.com:27017/mydb
I'm guessing that it defaults to localhost:27017. In your test/resources folder you can setup a differing application.properties. Check out the spring-boot externalized properties details.

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

I have created a rest application using 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.
Is it possible to run this spring-boot application programatically? I cannot use the simple #SpringBootTest-annotation as the spring-boot application is not in the same maven multi module.
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.
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. Since REST applications are meant to be independent.
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.
An integration test module, ala cucumber.io, should be created which will cover the functionality of the running module.
This is the main-point of the accepted answer.

How to run multiple Spring Boot applications easily on Production

I am trying to figure out an easy way to manage many Spring Boot applications on my Production server. Right now I have many fat jars running on different folders where each one has its your own script to start/stop the application and there's an external folder for the configurations (logback, properties, xml). For record those configurations are loaded by command line -Dloader.path to Spring Boot execution.
So how can I avoid conflicts for the same http/https port already running on Production? Does exist any kind of application manager where system administrators could control it? One solution I found was to virtualize Spring Boot applications with Docker, but my environment is Unix Solaris.
Is there any java solution for this scenario?
You can have a look at Spring Cloud which will give you better control and management when running multiple boot applications. All components of Spring Cloud
might not be useful to you, but few of them will help in port resolution, service rerouting and property maintenance. Along with the above you can also try SBA.
Along with the above you can also try Nginx for UI load balancing and reverse proxy.

Categories