In the last week, I had a problem after including some new libraries in the project that I was working on. There was no exception occurring, but the Web Sockets stopped to work.
Unfortunately, we just realized the problem after some days. This gave me some hours of work (and some gray hairs), trying to figure out manually what commit breaks the web socket. Actually, it was my mistake, I didn’t write any integration test for the Web Socket.
After discovering the problem. (an integration problem with the lib spring-cloud-sleuth),
I decided to write a test for it. I don’t want to have the same problem again!
I had never written tests for Web Sockets before, and I didn’t find any clear and good example for it. Then, I decided to share my solution here, maybe it will help someone that needs it.
This is the websocket configuration using Spring:
And here I put the test using Stomp Client. Basically, the test connects to the Web Socket,
then subscribes to the topic and sends a message. I used the class BlockingQueue, in order to wait for the asynchronous response.
Always write integration tests. It can prevent bugs when you upgrade libraries, or when you include new ones. It also can save some hours and headaches in the future.
If you want, you can clone the complete project from my github: