Spring Cloud Contract testing without Spring Framework (Boot) - spring

I would like to know if it is possible to use Spring Cloud Contracts with other frameworks not only Spring Boot? An example of another framework I'd like to test Spring Cloud Contract is KumuluzEE.

Are you asking about the consumer side or the producer side?
On the consumer side you can use the JUnit rule (http://cloud.spring.io/spring-cloud-static/Edgware.RELEASE/multi/multi__spring_cloud_contract_stub_runner.html#_stub_runner_junit_rule) .
On the producer side you can use the EXPLICIT mode (e.g. http://cloud.spring.io/spring-cloud-static/Edgware.RELEASE/multi/multi__contract_dsl.html#_working_with_context_paths).
That way the generated tests will assume that you're sending a request to a real running application. So in the base class (or before even running these tests) you'd have to start your app and then point to the URL (like here https://github.com/marcingrzejszczak/the-legacy-app/blob/master/stubs/src/test/java/com/example/contracts/BaseClass.java#L15)

Related

Spring integration testing for REST call of some other Service

I've been searching it on the net , but most of the examples i found have the returning logic of rest call within the same project , but what if you want to test a rest call of some other service which you are using it in your project(calling a REST api from a REST API)
is there any way to integrate this . Integration testing for a REST call which is of some external service
can the normal Integration testing approach could work in this case.
Have you considered using Spring Cloud Contract (http://cloud.spring.io/spring-cloud-contract/) ? It's a project design specifically for that purpose.
You have the producer of the API and its consumers. The idea of Spring Cloud Contract and Consumer Driven Contract approach is such, that the consumers suggest how the API of the producer should look like. They can prototype the API without writing any production code on the producer side. The prototyping takes place in a form of a "contract". It can be a Groovy or a YAML file (you can of course extend the framework). Processing of the contract results in a creation of a WireMock stub that the consumers can leverage in their integration tests. In other words, it's as if the producers would prepare a small, fake implementation of their code for testing perspective. So the consumers can run their integration tests against a stub of the producer side. The stub was generated from the contract. Let's say that the consumer X wants to use an API in such a way that if a GET request is sent to /foo it will respond with text bar. Then a stub that responds with a bar text, when hit at the /foo endpoint will be generated.
Now, the producers reuse the same contracts to generate tests to verify if their API meets the requirement of what's there in the contract. Remember the GET # /foo will respond with bar example? If the producer tries to build its project and doesn't have such an endpoint, its build will be broken. The Spring Cloud Contract framework generates the tests that assert whether the API is working the way it should. Only after the producer fixes the missing implementation will the build pass.
This is the consumer driven contract approach. You can also do the producer driven approach where the producer of the API just defines the contracts without communicating with the consumers how exactly each of them is using the API.
Valuables links:
Spring Cloud Contract page: http://cloud.spring.io/spring-cloud-contract/
Spring Cloud Contract workshops: http://cloud-samples.spring.io/spring-cloud-contract-samples/
Contract Tests in the Enterprise presentation: https://www.youtube.com/watch?v=ZyHG-VOzPZg
Why Contract Tests matter presentation: https://www.youtube.com/watch?v=TvpkZu1e2Dc
Note: I'm the maintainer of Spring Cloud Contract.

Consuming CometD messages with spring webflux

We have a system which publishes messages via cometD.
I wrote a simple java program to establish a connection to the server
then use the bearer token returned from the login to and consume messages
But I want to move this to a Webflux project, but unsure where to start, I can see there is out of box JMS-webflux wrapper, can I use this or is it best to build something similar ?
I'm very new to cometD and have only used simple webflux components,
I can also just use Spring boot, but ideally just like a JmsReceiver or similar
Thanks

Can you use (any) Spring's functionality outside of Spring (Boot)?

I have just built a RESTful web service with Spring Boot.
I now want to utilise the RESTful web service and start making calls to it by building a java console application (eventually adding GUI and security).
I was wondering if I can use any of the Spring functionality outside of the Spring (Boot) environment and use it in my java console application? For example, can I use Spring's RestTemplate in my non-Spring java application to make the REST api calls? I am new to Spring and I want to stick as close to Spring as possible. I think you can't, but I just want to make sure.
If not possible, I know you can create non-web application with Spring. Is it possible to integrate a GUI? Might not be best practice, just exploring what is possible and conventional.
Spring Boot is not coupled, in any way, to an application type. You can run command-line only apps, batch apps, web apps or any other kind of apps with it. You can even benefit from Spring Boot's auto-configuration.
In the case of the RestTemplate you may want to import spring-web directly rather than spring-boot-starter-web. Or you could add the starter and exclude the embedded container (spring-boot-starter-tomcat). Spring Boot will auto-adapt and not start an embedded web server in that case.

Implementing Spring Integration logging-channel-adapter for monitoring all the applications

I want to implement logging-channel-adapter in java class which monitors all the inbounds and outbounds of all the applications. If there is any another way of logging spring integration calls please advice.
Note : I am using spring boot.
Spring integration provides option to configure global Wire Tap. That is what you want.
Reaction on comment:
Here is example project I created.

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.

Resources