Alternative to wiremock for spring boot app integration testing - spring-boot

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 ?

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

Will Spring Boot support WAR deployment of Spring Webflux applications in the future?

I know that Spring Boot, in contrast to Spring Framework, does not support WAR deployment for Spring WebFlux applications. My question is simple: will it ever in the future?
My use case is this: we have a lot of customers that still live in the traditional "we deploy everything on application server X" world. So although we would like to push standalone JARs, they are not ready (yet). We heavily use Spring Boot, and would really like to continue to do so, so abandoning that is not an option.
We are building reactive applications and would like to use Spring WebFlux for that, but we still need to deploy to application servers, so that is not an option. In the meantime we avoid Spring WebFlux and simply use Controllers, which works, but is not as elegant. Hence my question.
There are no plans to support Spring WebFlux with war deployments. However, you can use reactive return types, Reactor's Mono and Flux and those from RxJava, with Spring MVC packaged and deployed as war. That will allow you to build an entire reactive pipeline as you would with WebFlux, but deployed to an application server. It doesn't give you all of the benefits of full-blown reactive (no event loop-based concurrency, for example), but it can be a good middle ground for those in your situation.

Spring Test Driven Development Mocking

TDD veterans seem to suggest that we must avoid mocking 3rd party code such as any framework code. Any non-trivial Spring based project will have dozens of Spring provided Beans injected and used. If Mocking 3rd party code is bad, what is the best way to write Unit Tests when the class depends on Spring provided Beans?
Don't mock then, use the real classes! Just as you wouldn't mock the String class.
That said, if you are developing a web application or a REST client, you should be aware that Spring provides classes that mock the web application server, for testing web applications, and the HTTP client, for testing REST clients.

Spring Cloud Contract testing without Spring Framework (Boot)

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)

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.

Resources