Start an embedded gRPC server while running integration tests with Springboot - spring-boot

I have built a Springboot application which has a gRPC server as one of its dependencies. While running the integration test, I am able to use embedded Kafka, embedd MySQL etc using TestContainers, but how can I create an embedded gRPC server while running my integration test suite?
I have the .proto contract definition file. I would use Wiremock to mock the request and responses to the gRPC server, but I am unable to start the embedded gRPC server yet.
It will be really great if I could find some help here.
Come to think of it, my question can be generalized to starting a generic embedded HTTP server in Springboot integration test, using TestContainers or otherwise.

I had a similar problem with IT testing Java gRPC service integrations, as using InProcessServer is quite cumbersome and quite hard to use when you need to return different responses for different tests. Also when using it you cannot really test your Stub beans and their configuration, since you can't pass InProcessServer host as a variable.
For this purpose I've created a gRPC Java mocking tool gRPC Mock. It has two integration modules - one for spring-boot and one for JUnit5. It follows a similar DSL type of structure to HTTP mocking service WireMock for creating the stubs. You can visit the GitHub page for some examples.

Related

Best way to mock HTTP server in Spring Boot with Kotlin?

I want to test an HTTP repository, and to do so I need to mock an HTTP server. I found this resource that goes over on one way to do so in Spring Boot, however, it's from 2020. I am not saying it's necessarily a bad or outdated approach, but I wanted to know if there is a more preferable or a Kotlin specific way to mock HTTP server now? Any help would be appreciated.
I would recommend using Wiremock to mock HTTP servers in your Spring Boot tests. Reasons:
well maintained and actively developed library (As of today: 5k stars on GitHub and last release April 29th 2022)
Spring boot has integration with this library (just add org.springframework.cloud:spring-cloud-contract-wiremock to your dependencies)
The team behind Spring Cloud Contract have created a library to support running WireMock using the “ambient” HTTP server. It also simplifies some aspects of configuration and eliminates some common issues that occur when running Spring Boot and WireMock together.
More info you can find here:
Spring Cloud Contract WireMock
Spring Boot Integration Tests With WireMock and JUnit 5
And here is GitHub repo with an example:
spring-boot-wiremock

Testing REST API provider response without mock

Currently, I am working on a project on Spring Boot where we are integrating with external REST API. As part of our integration suite test, we are doing the mock test of the actual external API which executes as part of the CI/CD.
My question is in production it calls the actual API so, how we can do that in the test environment. I don't think we need to make the actual external provider call during multiple integration test which will load the external API, also at the same time would like to test with actual REST response from the service.
Any suggestions?
If the public API has a swagger description, you could use the Atlassian Pact Swagger Validator. I describe the workflow in this talk: https://www.youtube.com/watch?v=79GKBYSqMIo#t=39m10s
Another alternative would be to create a mock API for the external service. There are some free services like https://mockfirst.com, https://www.mockable.io/, etc. where you can do that.

Alternative to wiremock for spring boot app integration testing

Currently I have one of the spring boot apps which is using wiremock for mocking the external services dependency in integration testing. However I have experienced that those tests are very fragile and unpredictable and takes lot of time to execute.
I want to know if there are better ways to mock external services dependency for integration testing purposes in spring boot application ?

Spring Cloud Netflix - how to access Eureka/Ribbon from traditional web app?

Everything I found on the internet about Spring Cloud Netflix is about running microservices from Boot applications using #EnableEurekaClients and so on.
Now I'm trying to connect my logging microservice within a traditional war application (springmvc, jaxws etc) - piece of legacy which can not be converted to Boot or modified in any way (by technical task).
I've created a new maven module "log-server-client" that knows nothing about upper web layer and intended to be used as a simple dependency in any maven project.
How should I configure access to Spring Cloud Netflix for this simple dependency? At least, how to configure Eureka and Ribbon?
I just extracted some lines of code from RestTemplate and created my custom JmsTemplate (microservice works with jms remoting with apache camel and activemq), exactly how it is done in RestTemplate, but this code stil lacks connection to infrastructure
afaik, we can create a global singleton bean, run a separate thread from this bean, and run Boot app from this thread, but don't you think that it is very ugly and can lead to problems? How it really should be used?
Great question!
One approach is to use a "sidecar". This seems to be a companion Spring Boot application that registers with the Eureka Server on behalf of your traditional web app.
See e.g.:
http://www.java-allandsundry.com/2015/09/spring-cloud-sidecar.html
http://cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html#_polyglot_support_with_sidecar
Another approach is to use the following library:
"A small lib to allow registration of legacy applications in Eureka service discovery."
https://github.com/sawano/eureka-legacy-registrar
This library can be used outside of Spring Boot.

How to deploy a spring integration component?

I've developed a spring integration component that is to sit on a server and process messages coming in over an inbound RMI channel do some processing/filtering and then send out some messages over a different RMI channel.
Currently, for testing, I've been running it using a Main class that just loads the context, which sets up the inbound RMI gateway and it's working fine. But I don't think that this is appropriate for a production environment.
What is the best way to deploy this type of project to a server?
If I were working in a .Net I'd be deploying this type of application as a windows service, is that what I should be doing here?
The application that is sending me data is hosted in Tomcat, would it be a good idea to also run this application within the same Tomcat container (Current requirements are for both components to be on the same machine this may change)? And if so how?
I'm currently looking into Spring Boot, am I on the right path?
I think the best would be Spring Boot, as it's made to easily allow running different types of applications. Also, you don't need Tomcat if you can run the same component with a simple Main and not using UI. Spring Boot, also, has a sample using Spring Integration here, so you should be up and running in no time.

Resources